在无法正常工作的两个Android设备之间通过wifi发送语音消息

时间:2015-04-06 12:05:14

标签: java android

我正在尝试通过wi-fi在android中发送语音消息。每个日志工作正常显示数据包正在从一个设备发送,并且正在另一台设备上接收..但是,当我正在将收到的缓冲区写入扬声器时,它不播放任何声音。

以下是发件人的代码:

public class SendMainActivity extends Activity {

    private EditText target;
    private TextView streamingLabel;
    private Button startButton, stopButton;

    public byte[] buffer;
    public static DatagramSocket socket;
    private int port = 50005; // which port??
    AudioRecord recorder;

    // Audio Configuration.
    private int sampleRate = 16000; // How much will be ideal?
    private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

    private boolean status = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.v("VS", "Inside  Oncreate");
        target = (EditText) findViewById(R.id.target_IP);
        streamingLabel = (TextView) findViewById(R.id.streaming_label);
        startButton = (Button) findViewById(R.id.start_button);
        stopButton = (Button) findViewById(R.id.stop_button);

        streamingLabel.setText("Press Start! to begin");

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

    private final OnClickListener stopListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            status = false;
            recorder.release();
            Log.v("VS", "Recorder released");
        }

    };

    private final OnClickListener startListener = new OnClickListener() {

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

    };

    public void startStreaming() {

        Thread streamThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    int minBufSize = AudioRecord.getMinBufferSize(sampleRate,
                            channelConfig, audioFormat);
                    DatagramSocket socket = new DatagramSocket();
                    Log.v("VS", "Socket Created");

                    byte[] buffer = new byte[minBufSize];

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

                    final InetAddress destination = InetAddress
                            .getByName(target.getText().toString());
                    Log.v("VS", "Address retrieved:" + destination);

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

                    recorder.startRecording();
                    Log.v("VS", "Recording Started");
                    //Toast.makeText(MainActivity.this, "Speak Up",
                        //  Toast.LENGTH_LONG).show();
                    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);

                    }

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

            }

        });
        streamThread.start();
    }

}  

以下是Receiver的代码:

public class MainActivity extends Activity {

    private Button receiveButton, stopButton;

    public static DatagramSocket socket;
    private AudioTrack speaker;

    // Audio Configuration.
    private int sampleRate = 16000; // How much will be ideal?
    private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

    private boolean status = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.v("VR", "Inside Oncreate");
        receiveButton = (Button) findViewById(R.id.receive_button);
        stopButton = (Button) findViewById(R.id.stop_button);
        findViewById(R.id.receive_label);

        receiveButton.setOnClickListener(receiveListener);
        stopButton.setOnClickListener(stopListener);

    }

    private final OnClickListener stopListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            status = false;
            speaker.release();
            Log.v("VR", "Speaker released");

        }

    };

    private final OnClickListener receiveListener = new OnClickListener() {

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

        }

    };

    public void startReceiving() {
        Log.v("VR", "Inside Recieve button");
        Thread receiveThread = new Thread(new Runnable() {

            @Override
            public void run() {

                try {
                    Log.v("VR", "Inside Recieve run");
                    DatagramSocket socket = new DatagramSocket(50005);
                    Log.v("VR", "Socket Created");

                    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);
                    Log.v("VR", "minBufSize:"+minBufSize);
                    speaker = new AudioTrack(AudioManager.STREAM_MUSIC,
                            sampleRate, channelConfig, audioFormat, minBufSize,
                            AudioTrack.MODE_STREAM);

                    speaker.play();
                    Log.v("VR", "speaker.play()");
                    while (status == true) {
                        try {
                            Log.v("VR", "Inside While");
                            DatagramPacket packet = new DatagramPacket(buffer,
                                    buffer.length);
                            Log.v("VR", "packet created");
                            socket.receive(packet);
                            Log.v("VR", "Packet Received");

                            // reading content from packet
                            buffer = packet.getData();
                            Log.v("VR", "Packet data read into buffer"+buffer.toString());

                            // sending data to the Audiotrack obj i.e. speaker
                            speaker.write(buffer, 0, minBufSize);
                            Log.v("VR", "Writing buffer content to speaker");

                        } catch (IOException e) {
                            Log.e("VR", "IOException");
                        }
                    }

                } catch (SocketException e) {
                    Log.e("VR", "SocketException");
                }

            }

        });
        receiveThread.start();
    }

}

一切似乎都很好。我可以看到logcat中的所有日志,我甚至尝试将扬声器音量增加到最大水平,我一直大声说话,麦克风仍然没有收到另一台设备上的语音信息。我在做什么错了吗?任何有关工作的想法/建议都将非常感激。

0 个答案:

没有答案