Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

96
LINES

< > BotCompany Repo | #1027059 // Find clear print log bug [Android Send Audio Raspi v3]

JavaX source code (Android) - run with: the app

!7

set flag Android. set flag AndroidOnly. set flag NoAWT.

import android.media.*;
import android.content.*;

static int bufferSize = 8192;

// raspi home, X20
static LS ips = ll("192.168.2.165", "192.168.43.244");

static AudioRecord recorder;
static new L<StefansOS_ConnectToServer> senders;
static new Q qSendAudio;
static new Q qVoiceOut;

p {
  for (S ip : ips) {
    print("Connecting to " + ip);
    new StefansOS_ConnectToServer sender;
    sender.server = ip;
    sender.onLine = voidfunc(S s) {
      final new Matches m;
      if (startsWith_trim(s, "voiceOutput:", m))
        addToQAndPrintSize(+qVoiceOut, r {
          androidSayInEnglish(m.rest());
        });
    };
    sender.startWithSubs("voiceOutput");
    senders.add(sender);
  }
  
  BroadcastReceiver brr = new {
    @Override
    public void onReceive(Context context, Intent intent) {
      int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,-1);
      print(+state);
      if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) {
        print("Bluetooth Connected");
        androidContext().unregisterReceiver(this);
        thread { startRecording(); }
      }
    }
  };
  
  AudioManager am = cast androidContext().getSystemService(Context.AUDIO_SERVICE);
  androidContext().registerReceiver(brr, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
  print("Starting Bluetooth");
  am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
  am.setBluetoothScoOn(true);
  am.setMode(AudioManager.MODE_IN_CALL);
  am.startBluetoothSco();
  print("Can BT record from mic? " + am.isBluetoothScoAvailableOffCall());
}

svoid startRecording {
  print("Making recorder");
  
  recorder = new AudioRecord.Builder()
    .setAudioSource(
      //MediaRecorder.AudioSource.VOICE_COMMUNICATION
      MediaRecorder.AudioSource.MIC
    )
    .setAudioFormat(new AudioFormat.Builder()
      .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
      .setSampleRate(16000)
      .setChannelMask(AudioFormat.CHANNEL_IN_MONO)
      .build())
   .setBufferSizeInBytes(bufferSize)
   .build();
  print("Made recorder");
  
  recorder.startRecording();
  print("Started recording");
  
  byte[] buf = new[bufferSize];
  
  while licensed {
    //printWithTime("Read bytes: " + recorder.read(buf, 0, l(buf), AudioRecord.READ_BLOCKING);
    fS line = "audio: " + base64(buf);
    addToQAndPrintSize(+qSendAudio, r {
      for (StefansOS_ConnectToServer sender : senders) pcall {
        sender.sendLine(line);
      }
    });
  }
  
  /*recorder.release();
  print("Released recorder");*/
}

svoid cleanMeUp {
  if (recorder != null) recorder.release();
  dispose senders;
}

Author comment

Began life as a copy of #1027055

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027059
Snippet name: Find clear print log bug [Android Send Audio Raspi v3]
Eternal ID of this version: #1027059/1
Text MD5: 82d2133dad092f3f85fb1aa9c6edc628
Author: stefan
Category: javax / android
Type: JavaX source code (Android)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-16 15:55:12
Source code size: 2743 bytes / 96 lines
Pitched / IR pitched: No / No
Views / Downloads: 196 / 220
Referenced in: [show references]