4

I'm trying to build an app that records sounds and then plays them back. The recording part works perfectly but when I try to replay the sound it does nothing. When I run it in the debugger and step trough the steps to play the audio, it works. When I remove all breakpoints and run the program in debug, it does not.

The problem is probably caused by some things that are done in the background and are not completed before I try to run the audio, but I'm not entirely sure.

Any help would be greatly appreciated.

Here are the relevant parts of the source code.

Creating the Soundpool

mSoundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0);

Playing from the soundpool

int soundId = mSoundPool.load(mAudioRecorder.stop(), 0);
if(soundId > 0){
    mSoundPool.play(soundId, 0.99f, 0.99f, 0, -1, 1.0f);
}

Audiorecorder.java the output file is .mp4

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);


public String stop() throws IOException {
    mRecorder.stop();
    return mPath;
}
Minion91
  • 1,911
  • 12
  • 19
  • can you please help me here, Thanks... [android soundpool error][1] [1]: http://stackoverflow.com/questions/29199164/android-soundpool-error – 5h177y Mar 23 '15 at 18:58

3 Answers3

5

I had the same problem yesterday - the reason of not proper working in RELEASE mode, is the fact that you have to wait for SoundPool.load(...) function to perform.

I solved it with loading sounds in menu activity before executing activities/fragments in which I am going to play some music with SoundPool.play(...) function.

In DEBUG mode app have a lot of time to perform SoundPool.load(...) function, so it works then.

PrzemekTom
  • 1,328
  • 1
  • 13
  • 34
2

Why don't you try to play the recorded audio through MediaPlayer.

mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "audiorecordtest.MPEG_4";
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);


public String stop() throws IOException {
    mRecorder.stop();
    return mPath;
}

To play the recorded file use this code:

mPlayer = new MediaPlayer();
             try {
                mPlayer.setDataSource(mFileName);
            } catch (IllegalArgumentException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IllegalStateException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
              try {
                mPlayer.prepare();
            } catch (IllegalStateException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            mPlayer.start();
Manikandan
  • 1,479
  • 6
  • 48
  • 89
  • Because I want to be able to play multiple samples, and loop some of them. The Mediaplayer works perfectly for playing it once though. – Minion91 Jul 06 '11 at 23:46
  • I have the same problem. Have you find any reason for this behavior? – Simon Nov 05 '12 at 21:50
0

This may be considered a hack. But worked for me. Basically wait for a few milliseconds between load and play

    try {
            soundPlayer_m.load(clipToPlay);
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    soundPlayer_m.play(clipToPlay);
                }
            }, 300);

            setInitialVolume();
        } catch (Exception e) {
            Log.d("Exception", "E: " + e.toString());
            Toast.makeText(this, R.string.no_file, Toast.LENGTH_SHORT).show();
        }