Libraryless. Click here for Pure Java version (6136L/41K/139K).
!7 // TODO: better cleanup set flag Android. set flag NoAWT. import android.media.*; import android.content.*; static int bufferSize = 8192; static int maxAudioQueueSize = 50; // raspi home, X20 static LS ips = ll("192.168.2.165", "192.168.43.244"); static AudioManager am; static AudioRecord recorder; static new L<StefansOS_ConnectToServer> senders; static new L<Q> qSendAudio; static new Q qVoiceOut; static bool connected; p { am = (AudioManager) androidContext().getSystemService(Context.AUDIO_SERVICE); printStreamInfos(); androidSay_keepEngine = true; //set androidSay_keepEngine_verbose; set androidSay_keepEngine_inCall; 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)) say(m.rest()); }; sender.tryToConnect = () -> !connectToServer_anyConnected(senders); sender.startWithSubs("voiceOutput"); senders.add(sender); qSendAudio.add(new Q); } 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); printStreamInfos(); thread { startRecording(); } } } }; 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 { int n = recorder.read(buf, 0, l(buf), AudioRecord.READ_BLOCKING); //printWithTime("Read bytes: " + n); fS line = "audio: " + base64(buf); for i over qSendAudio: { final Q q = qSendAudio.get(i); final StefansOS_ConnectToServer sender = senders.get(i); if (!sender.connected()) continue; addToQWithMaxSize(maxAudioQueueSize, q, r { sender.sendLine(line) }); } if (connectToServer_anyConnected(senders) != connected) { connected = !connected; say(connected ? "Connected" : "Lost connection"); } } /*recorder.release(); print("Released recorder");*/ } svoid cleanMeUp { if (recorder != null) recorder.release(); dispose senders; } svoid printStreamInfos { print("Stream muted: " + am.isStreamMute(AudioManager.STREAM_MUSIC) + ", vol: " + am.getStreamVolume(AudioManager.STREAM_MUSIC)); } svoid say(S s) { print("> " + s); addToQWithMaxSize_verbose(5, +qVoiceOut, r { androidSayInEnglish(s); }); }
Began life as a copy of #1027051
download show line numbers debug dex old transpilations
Travelled to 8 computer(s): bhatertpkbcr, mqqgnosmbjvj, odkhaoglnmdk, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1027055 |
Snippet name: | Android Raspi Bridge v3 [for Bluetooth headset, with voice output, basically works] |
Eternal ID of this version: | #1027055/29 |
Text MD5: | f8b1995256cf813c953b2374fdffe7e1 |
Transpilation MD5: | 5221962aec736205af5718d54bbb33e6 |
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-17 10:04:12 |
Source code size: | 3606 bytes / 124 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 342 / 668 |
Version history: | 28 change(s) |
Referenced in: | #1027059 - Find clear print log bug [Android Send Audio Raspi v3] #1027093 - Android Raspi Bridge [voice output only] |