0

I'm making an app that records audio as long as the imagebutton is touched and held. I achieved this with the following code:

public class MainActivity extends Activity {

MediaRecorder recorder;
MediaPlayer mediaPlayer;

//STORAGE PATHS
String storagePath;
String externalStoragePath;

//FULL OUTPUT PATHS
String externalOutputPath;

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

    //AUDIO RECORDER
    recorder = new MediaRecorder();
    recorder.reset();       
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
        externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        externalOutputPath = externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3";
        recorder.setOutputFile(externalOutputPath);
    }
    else
    {
        storagePath = Environment.getDataDirectory().getAbsolutePath();
        recorder.setOutputFile(storagePath + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3");
    }

    //IMAGE BUTTON ONTOUCHLISTENER
    final ImageButton recBtn = (ImageButton) findViewById(R.id.recButton);
    recBtn.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                recBtn.setImageResource(R.drawable.record_btn_pressed);
                try {
                    recorder.prepare();
                    recorder.start();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else if (event.getAction() == MotionEvent.ACTION_UP)
            {
                recBtn.setImageResource(R.drawable.record_btn);
                try {
                    recorder.prepare();
                    recorder.stop();
                    recorder.reset();
                    recorder.release();
                    recorder = null;
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Log.i(STORAGE_SERVICE, "File saved to: " + externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/test.3gp");
            }
            return true;
        }

    });
} //END OF ONCREATE

Now after I've stopped recording, I searched for the mp3 file in file manager (android app) and tried playing it music player but it doesn't work. It says that the file can't be played.

I then also tried to add a "test play" button to the app, to try and play the recorded file with MediaPlayer but it doesn't work either. As soon as I press the play button, the app crashes.

public void testPlay (View v) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(externalOutputPath);
    mediaPlayer.start();
}

Yes, I did add android:onClick="testPlay" to the xml file

Logcat file:

07-31 16:51:43.938: E/MediaPlayer(26918): Unable to to create media player
07-31 16:51:43.953: E/AndroidRuntime(26918): FATAL EXCEPTION: main
07-31 16:51:43.953: E/AndroidRuntime(26918): java.lang.IllegalStateException: Could not execute method of the activity
07-31 16:51:43.953: E/AndroidRuntime(26918):    at android.view.View$1.onClick(View.java:3633)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at android.view.View.performClick(View.java:4240)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at android.view.View$PerformClick.run(View.java:17721)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at android.os.Handler.handleCallback(Handler.java:730)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at android.os.Looper.loop(Looper.java:137)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at android.app.ActivityThread.main(ActivityThread.java:5103)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at java.lang.reflect.Method.invokeNative(Native Method)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at java.lang.reflect.Method.invoke(Method.java:525)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at dalvik.system.NativeStart.main(Native Method)
07-31 16:51:43.953: E/AndroidRuntime(26918): Caused by: java.lang.reflect.InvocationTargetException
07-31 16:51:43.953: E/AndroidRuntime(26918):    at java.lang.reflect.Method.invokeNative(Native Method)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at java.lang.reflect.Method.invoke(Method.java:525)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at android.view.View$1.onClick(View.java:3628)
07-31 16:51:43.953: E/AndroidRuntime(26918):    ... 11 more
07-31 16:51:43.953: E/AndroidRuntime(26918): Caused by: java.io.IOException: setDataSourceFD failed.: status=0x80000000
07-31 16:51:43.953: E/AndroidRuntime(26918):    at android.media.MediaPlayer.setDataSource(Native Method)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:981)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:960)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:918)
07-31 16:51:43.953: E/AndroidRuntime(26918):    at com.whizzappseasyvoicenotepad.MainActivity.testPlay(MainActivity.java:99)
07-31 16:51:43.953: E/AndroidRuntime(26918):    ... 14 more

Permissions:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Guy
  • 6,414
  • 19
  • 66
  • 136
  • 1
    I don't know exactly why your file won't play. I'd just like to point out that what you're creating is _not_ an mp3 file. You're telling the recorder to encode the audio data using the AMR narrowband codec and put it in a 3GPP container. The fact that you named the file ".mp3" makes no difference. – Michael Jul 31 '13 at 13:18
  • I removed the .mp3 and it still won't play it. Thanks for letting me know though! – Guy Jul 31 '13 at 13:22
  • 1
    I don't see any call to `mediaplayer.prepare` (or `prepareAsync`). – Michael Jul 31 '13 at 13:23
  • which error/exception do you get? – Wandang Jul 31 '13 at 13:41
  • @Wandang I updated the original post, added the logcat file – Guy Jul 31 '13 at 14:54

4 Answers4

0
java.lang.IllegalStateException

often indicates that a permission is missing in your manifest.xml

sschrass
  • 7,014
  • 6
  • 43
  • 62
  • I added permissions to the original post, please tell me if you think any permission is missing. – Guy Jul 31 '13 at 15:06
  • https://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE – sschrass Jul 31 '13 at 15:07
  • oops, this permission is actually not needed because "Any app that declares the WRITE_EXTERNAL_STORAGE permission is implicitly granted this permission." – sschrass Jul 31 '13 at 15:09
  • Yes, I was just about to say that :) I tried adding it anyway, just for the sake of it, but the app still crashes, as expected – Guy Jul 31 '13 at 15:10
  • try to change the encoding of your recorded file. here: http://stackoverflow.com/questions/9657280/mediaplayer-setdatasource-causes-ioexception-for-valid-file is a discussion about java.io.IOException: setDataSourceFD failed – sschrass Jul 31 '13 at 15:13
0

I'm using the class AudioRecord and read the stream in a seperate thread and writing the file as I go.

I'm hoping that by just providing the code that I am using that you can solve your problem. Good luck.

In my Constructor:
    bufferSize = AudioRecord.getMinBufferSize(mSampleRate,
            mNbChannels, RECORDER_AUDIO_ENCODING);

private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

public void start() 
{
    System.out.println("SoundRecorder JAVA: start");
    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            mSampleRate, mNbChannels,
            RECORDER_AUDIO_ENCODING, bufferSize);

    recorder.startRecording();

    isRecording = true;

    recordingThread = new Thread(new Runnable() 
    {

        @Override
        public void run() {
            writeAudioDataToFile();
        }
    }, "AudioRecorder Thread");

    recordingThread.start();
}

private void writeAudioDataToFile() 
{
    byte data[] = new byte[bufferSize];
    String filename = mTempPath;
    FileOutputStream os = null;
    mRecordFile = new File(filename);

    try {
        os = new FileOutputStream(filename);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    int read = 0;

    if (null != os) 
    {
        while (isRecording) 
        {
            read = recorder.read(data, 0, bufferSize);

            Log.i("SoundRecord", "read " + read + " bytes. Wrote In file: " + filename);

            if (AudioRecord.ERROR_INVALID_OPERATION != read) {
                try {
                    os.write(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

public void stop() 
{
    if (null != recorder) 
    {
        System.out.println("SoundRecorder JAVA: stop");
        isRecording = false;

        recorder.stop();
        recorder.release();

        recorder = null;
        recordingThread = null;
    }
}
Kuoni
  • 124
  • 4
0

here is a sample of code for recording AMR-NB ( the onClick method )

NOTE: bitRate, sampleRate, FilePath, fileTyp=3gp

If you prepare your player to equal what you recorded, then your IO error should go away...

        if (isRecording) return;
        tmpFilNme = "me" +Calendar.getInstance().getTime().toString().hashCode();
        view.setKeepScreenOn(true);
        File dir = Environment.getExternalStorageDirectory();          
        File appDir = new File(dir, "PictureComment");
        if (!appDir.exists()) appDir.mkdir();
        recordFilePath = new File(appDir, tmpFilNme +".3gp").getAbsolutePath();
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 

        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setAudioEncodingBitRate(12200);
        mRecorder.setAudioSamplingRate(8000); // this is the MIME rate

        mRecorder.setOutputFile(recordFilePath);
        try {
            mRecorder.prepare();
            mRecorder.start();
            isRecording = true;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43
0

I'm still not sure what was the problem, but I think there was something wrong with the way I stopped recording (the whole try, catch thing). I just rewrote the whole code and put the MediaRecorder in two different methods. startRecording() and stopRecording() method. And now it works perfectly!

startRecording()

public void startRecording (){
     recorder = new MediaRecorder();
     recorder.reset();      
     recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
     if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
     {
         externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
         externalOutputPath = externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3";
         recorder.setOutputFile(externalOutputPath);
     }
     else
     {
        storagePath = Environment.getDataDirectory().getAbsolutePath();
        recorder.setOutputFile(storagePath + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3");
     }
     recorder.setOnErrorListener(errorListener);
     recorder.setOnInfoListener(infoListener);

     try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

stopRecording()

public void stopRecording() {
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release();

        recorder = null;
    }
}
Guy
  • 6,414
  • 19
  • 66
  • 136