将音频记录从Android设备流式传输到另一个

时间:2014-07-17 17:08:13

标签: android audio stream android-audiorecord

我想让Android设备的麦克风之间的流声音到另一个的扬声器,所以我使用了Audiorecord !!

发送语音的活动是:

public class MainActivity extends Activity {

AudioRecord recorder;

private int sampleRate = 44100;
int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
private boolean status = true;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    startButton.setOnClickListener (startListener);
    stopButton.setOnClickListener (stopListener);

    minBufSize += 4096;
    System.out.println("minBufSize: " + minBufSize);
}

private final OnClickListener stopListener = new OnClickListener() {

    @Override
    public void onClick(View arg0) {
                status = false;
                recorder.release();
    }

};

private final OnClickListener startListener = new OnClickListener() {

    @Override
    public void onClick(View arg0) {
                status = true;
               try {

                DatagramSocket socket = new DatagramSocket();
                Log.d("VS", "Socket Created");

                byte[] buffer = new byte[minBufSize];

                Log.d("VS","Buffer created of size " + minBufSize);
                DatagramPacket packet;

                final InetAddress destination = InetAddress.getByName("addresse");
                Log.d("VS", "Address retrieved");


                recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize*10);
                Log.d("VS", "Recorder initialized");

                recorder.startRecording();


                while(status == true) {


                    //reading data from MIC into buffer
                    minBufSize = recorder.read(buffer, 0, buffer.length);

                    //putting buffer in the packet
                    packet = new DatagramPacket (buffer,buffer.length,destination,port);

                    socket.send(packet);
                    System.out.println("MinBufferSize: " +minBufSize);
                   }



            } catch(UnknownHostException e) {
                Log.e("VS", "UnknownHostException");
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("VS", "IOException");
            }            
    }

};


 }

并且android安装中的活动是:

public class MainActivity extends Activity {

public static DatagramSocket socket;
private AudioTrack speaker;



private boolean status = true;


@SuppressLint("CutPasteId")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button1 = (Button) findViewById(R.id.receive_button);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            status = true;
            try {

                DatagramSocket socket = new DatagramSocket(port);


                byte[] buffer = new byte[4096];


                //minimum buffer size. need to be careful. might cause problems. try setting manually if any problems faced
                int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);

                speaker = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,channelConfig,audioFormat,minBufSize,AudioTrack.MODE_STREAM);

                speaker.play();

                while(status == true) {

                        DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
                        socket.receive(packet);
                        Log.d("VR", "Packet Received");

                        buffer=packet.getData();
                        Log.d("VR", "Packet data read into buffer");

                        speaker.write(buffer, 0, minBufSize);
                        Log.d("VR", "Writing buffer content to speaker");


                }



            } catch(UnknownHostException e) {
                Log.e("VS", "UnknownHostException");
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("VS", "IOException");
            }
        }
    });

    Button button2 = (Button) findViewById(R.id.receive_button);
    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            status = false;
            speaker.release();

        }
    });
}

}

发件人工作但是当我点击接收器应用程序中的按钮时,会出错:NULL POINTER EXCEPTION !! 我该如何解决这个问题?

谢谢!

0 个答案:

没有答案