Libraryless. Click here for Pure Java version (5515L/37K/127K).
1 | !7 |
2 | |
3 | set flag Android. set flag AndroidOnly. set flag NoAWT. |
4 | |
5 | import android.media.*; |
6 | import android.content.*; |
7 | |
8 | static int bufferSize = 8192; |
9 | |
10 | // raspi home, X20 |
11 | static LS ips = ll("192.168.2.165", "192.168.43.244"); |
12 | |
13 | static AudioRecord recorder; |
14 | static new L<StefansOS_ConnectToServer> senders; |
15 | static new Q q; |
16 | |
17 | p { |
18 | for (S ip : ips) { |
19 | print("Connecting to " + ip); |
20 | new StefansOS_ConnectToServer sender; |
21 | sender.server = ip; |
22 | sender.start(); |
23 | senders.add(sender); |
24 | } |
25 | |
26 | BroadcastReceiver brr = new { |
27 | @Override |
28 | public void onReceive(Context context, Intent intent) { |
29 | int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,-1); |
30 | print(+state); |
31 | if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) { |
32 | print("Bluetooth Connected"); |
33 | androidContext().unregisterReceiver(this); |
34 | thread { startRecording(); } |
35 | } |
36 | } |
37 | }; |
38 | |
39 | AudioManager am = cast androidContext().getSystemService(Context.AUDIO_SERVICE); |
40 | androidContext().registerReceiver(brr, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)); |
41 | print("Starting Bluetooth"); |
42 | am.setStreamSolo(AudioManager.MODE_IN_CALL, true); |
43 | am.setBluetoothScoOn(true); |
44 | am.setMode(AudioManager.MODE_IN_CALL); |
45 | am.startBluetoothSco(); |
46 | print("Can BT record from mic? " + am.isBluetoothScoAvailableOffCall()); |
47 | } |
48 | |
49 | svoid startRecording { |
50 | print("Making recorder"); |
51 | |
52 | recorder = new AudioRecord.Builder() |
53 | .setAudioSource( |
54 | //MediaRecorder.AudioSource.VOICE_COMMUNICATION |
55 | MediaRecorder.AudioSource.MIC |
56 | ) |
57 | .setAudioFormat(new AudioFormat.Builder() |
58 | .setEncoding(AudioFormat.ENCODING_PCM_16BIT) |
59 | .setSampleRate(16000) |
60 | .setChannelMask(AudioFormat.CHANNEL_IN_MONO) |
61 | .build()) |
62 | .setBufferSizeInBytes(bufferSize) |
63 | .build(); |
64 | print("Made recorder"); |
65 | |
66 | recorder.startRecording(); |
67 | print("Started recording"); |
68 | |
69 | byte[] buf = new[bufferSize]; |
70 | |
71 | while licensed { |
72 | //printWithTime("Read bytes: " + recorder.read(buf, 0, l(buf), AudioRecord.READ_BLOCKING); |
73 | fS line = "audio: " + base64(buf); |
74 | q.add(r { |
75 | for (StefansOS_ConnectToServer sender : senders) pcall { |
76 | sender.sendLine(line); |
77 | } |
78 | }); |
79 | } |
80 | |
81 | /*recorder.release(); |
82 | print("Released recorder");*/ |
83 | } |
84 | |
85 | svoid cleanMeUp { |
86 | if (recorder != null) recorder.release(); |
87 | dispose senders; |
88 | } |
Began life as a copy of #1027050
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: | #1027051 |
Snippet name: | Android Send Audio Raspi v2 [for Bluetooth headset, OK!] |
Eternal ID of this version: | #1027051/24 |
Text MD5: | 91c2bf21f75dab7afa9c875b14a1fcb7 |
Transpilation MD5: | d60c3ba5a864ad584067f87c75603dbf |
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-15 15:03:51 |
Source code size: | 2440 bytes / 88 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 346 / 565 |
Version history: | 23 change(s) |
Referenced in: | [show references] |