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

发布于 2024-11-18 21:50:24 字数 815 浏览 2 评论 0原文

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

该问题可能是由在后台完成的一些事情引起的,并且在我尝试运行音频之前尚未完成,但我不完全确定。

任何帮助将不胜感激。

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

创建声音池

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

从声音池播放

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱的那么颓废 2024-11-25 21:50:24

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

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

在 DEBUG 模式下,应用程序有很多时间执行 SoundPool.load(...) 函数,因此它可以正常工作。

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.

┊风居住的梦幻卍 2024-11-25 21:50:24

为什么不尝试通过 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();

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();
苹果你个爱泡泡 2024-11-25 21:50:24

这可能被视为黑客攻击。但为我工作。基本上在加载和播放之间等待几毫秒

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

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();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文