传输语音应用程序力量关闭

时间:2014-12-09 05:47:57

标签: java android udp communication voice

我正在编写一个应用程序,将语音从一个Android设备发送到另一个,但当我单击按钮发送应用程序强制关闭我使用的代码类似于 Streaming voice between Android Phones over WiFi 我将发布以下代码

发件人:

    public class MainActivity extends ActionBarActivity {
    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;  
    int minBufsize=4096;
    private boolean status = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        target = (EditText) findViewById (R.id.target_IP);
        streamingLabel = (TextView) findViewById(R.id.streaming_label);
        startButton = (Button) findViewById (R.id.start);
        stopButton = (Button) findViewById (R.id.stop);
        stopButton.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View v) {
                status = false;
                recorder.release();
                Log.d("VS","Recorder released");

            }
        });
        startButton.setOnClickListener(new View.OnClickListener() {

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

        }
    });

    }
    public void startStreaming() {


        Thread streamThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {


                    int minBufSize = 4096;//AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
                    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(target.getText().toString());
                    Log.d("VS", "Address retrieved");


                    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
                    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);


                    }



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


            }

        });
        streamThread.start();
     }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

收件人:

    public class MainActivity extends ActionBarActivity {
    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
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        receiveButton = (Button) findViewById (R.id.recieve);
        stopButton = (Button) findViewById (R.id.stop);
        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
                public void onClick(View v) {
                status = false;
                speaker.release();
                Log.d("VR","Speaker released");

                }
            });
        receiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
                public void onClick(View v) {
                 status = true;
                    startReceiving();

                }
            });
    }
    public void startReceiving() {

        Thread receiveThread = new Thread (new Runnable() {

            @Override
            public void run() {

                try {

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


                    byte[] buffer = new byte[256];


                    //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) {
                        try {


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

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

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

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


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


            }

        });
        receiveThread.start();
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这里是Logcat

  

12-09 09:13:37.829:E / Trace(609):错误打开跟踪文件:没有这样的文件或目录(2)   12-09 09:13:38.890:I / Choreographer(609):跳过46帧!应用程序可能在其主线程上做了太多工作。   12-09 09:13:38.900:D / gralloc_goldfish(609):未检测到GPU仿真的仿真器。   12-09 09:13:39.070:I / Choreographer(609):跳过59帧!应用程序可能在其主线程上做了太多工作。   12-09 09:13:57.070:D / VS(609):Socket Created   12-09 09:13:57.101:D / VS(609):创建大小为4096的缓冲区   12-09 09:13:57.101:D / VS(609):检索到的地址   12-09 09:13:57.130:E / AudioRecord(609):不支持的配置:sampleRate 16000,格式1,channelCount 1   12-09 09:13:57.130:E / AudioRecord-JNI(609):创建AudioRecord实例时出错:初始化检查失败。   12-09 09:13:57.140:E / AudioRecord-Java(609):[android.media.AudioRecord]初始化本机AudioRecord对象时的错误代码-20。   12-09 09:13:57.140:D / VS(609):记录器初始化   12-09 09:13:57.180:W / dalvikvm(609):threadid = 11:线程退出未捕获异常(组= 0x40a13300)   12-09 09:13:57.180:E / AndroidRuntime(609):致命异常:Thread-69   12-09 09:13:57.180:E / AndroidRuntime(609):java.lang.IllegalStateException:在未初始化的AudioRecord上调用startRecording()。   12-09 09:13:57.180:E / AndroidRuntime(609):在android.media.AudioRecord.startRecording(AudioRecord.java:515)   12-09 09:13:57.180:E / AndroidRuntime(609):at com.example.voicesenderactivity.MainActivity $ 3.run(MainActivity.java:91)   12-09 09:13:57.180:E / AndroidRuntime(609):at java.lang.Thread.run(Thread.java:856)   12-09 09:13:57.400:D / dalvikvm(609):GC_CONCURRENT释放191K,5%免费6239K / 6535K,暂停15ms + 4ms,总计248ms   12-09 09:13:57.590:W / IInputConnectionWrapper(609):showStatusIcon在非活动的InputConnection上   12-09 09:13:58.300:I / Choreographer(609):跳过53帧!应用程序可能在其主线程上做了太多工作。

0 个答案:

没有答案