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

88
LINES

< > BotCompany Repo | #1027051 // Android Send Audio Raspi v2 [for Bluetooth headset, OK!]

JavaX source code (Android) [tags: use-pretranspiled] - run with: the app

Libraryless. Click here for Pure Java version (5515L/37K/127K).

!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 q;

p {
  for (S ip : ips) {
    print("Connecting to " + ip);
    new StefansOS_ConnectToServer sender;
    sender.server = ip;
    sender.start();
    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);
    q.add(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 #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: 347 / 566
Version history: 23 change(s)
Referenced in: #1027054 - Android Send Audio To Raspi v1 [works, but doesn't seem to use Bluetooth headset]
#1027055 - Android Raspi Bridge v3 [for Bluetooth headset, with voice output, basically works]