Android Soundpool.play()在发布版本中不工作



我试图建立一个应用程序,记录声音,然后播放他们回来。录音部分工作完美,但当我试图重放声音它什么也不做。当我在调试器中运行它并逐步播放音频时,它可以工作。当我删除所有断点并在调试中运行程序时,它不会。

这个问题可能是由一些在后台完成的事情引起的,在我尝试运行音频之前没有完成,但我不完全确定。

任何帮助都将是非常感激的。

以下是源代码的相关部分。

创建Soundpool

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

从soundpool播放

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

Audiorecorder.java输出文件为。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;
}

我昨天也遇到了同样的问题-在RELEASE模式下不能正常工作的原因是您必须等待SoundPool.load(...)函数执行。

在执行活动/片段之前,我在菜单活动中加载声音解决了这个问题,其中我将使用SoundPool.play(...)功能播放一些音乐。

在调试模式下,应用程序有很多时间来执行SoundPool.load(...)功能,所以它工作。

你为什么不试试用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;
}

播放录制的文件使用以下代码:

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

这可能被认为是一种攻击。但对我有用。基本上在加载和播放之间等待几毫秒

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

最新更新