0

i am writing an application that send voice from one android device to another but when i click the button to send the application force to close i am using code similar to Streaming voice between Android Phones over WiFi and i will post the code below

the sender:

    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);
    }
}

the reciever:

    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);
    }
}

and here is the Logcat

12-09 09:13:37.829: E/Trace(609): error opening trace file: No such file or directory (2) 12-09 09:13:38.890: I/Choreographer(609): Skipped 46 frames! The application may be doing too much work on its main thread. 12-09 09:13:38.900: D/gralloc_goldfish(609): Emulator without GPU emulation detected. 12-09 09:13:39.070: I/Choreographer(609): Skipped 59 frames! The application may be doing too much work on its main thread. 12-09 09:13:57.070: D/VS(609): Socket Created 12-09 09:13:57.101: D/VS(609): Buffer created of size 4096 12-09 09:13:57.101: D/VS(609): Address retrieved 12-09 09:13:57.130: E/AudioRecord(609): Unsupported configuration: sampleRate 16000, format 1, channelCount 1 12-09 09:13:57.130: E/AudioRecord-JNI(609): Error creating AudioRecord instance: initialization check failed. 12-09 09:13:57.140: E/AudioRecord-Java(609): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object. 12-09 09:13:57.140: D/VS(609): Recorder initialized 12-09 09:13:57.180: W/dalvikvm(609): threadid=11: thread exiting with uncaught exception (group=0x40a13300) 12-09 09:13:57.180: E/AndroidRuntime(609): FATAL EXCEPTION: Thread-69 12-09 09:13:57.180: E/AndroidRuntime(609): java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord. 12-09 09:13:57.180: E/AndroidRuntime(609): at 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 freed 191K, 5% free 6239K/6535K, paused 15ms+4ms, total 248ms 12-09 09:13:57.590: W/IInputConnectionWrapper(609): showStatusIcon on inactive InputConnection 12-09 09:13:58.300: I/Choreographer(609): Skipped 53 frames! The application may be doing too much work on its main thread.

Community
  • 1
  • 1
lena
  • 89
  • 1
  • 1
  • 12
  • Try to add your requirement clearly. So that others can help you. – Satish Shinde Dec 09 '14 at 05:51
  • what do you mean by requirement? – lena Dec 09 '14 at 05:53
  • Can you post your logcat output? – JASON G PETERSON Dec 09 '14 at 05:53
  • sorry i mean when i click the button to send voice the application force to close – lena Dec 09 '14 at 06:01
  • where i can find my logcat output – lena Dec 09 '14 at 06:04
  • The fatal exception is with "startRecording() called on an uninitialized AudioRecord". The answer here (http://stackoverflow.com/questions/4843739/audiorecord-object-not-initializing) has some tips. Could be as simple as manifest permissions, or could have to do with your initialization parameters. – JASON G PETERSON Dec 09 '14 at 06:40
  • i think that error mean the emulator does not accept 16000 as sampling rate so i modify it to 8000 and i will try but when i install the application on my phone i get the same error does this mean that the android version play role in the defined sampling rate?? – lena Dec 09 '14 at 06:49

0 Answers0