Android Soundpool.play() 在发布版本中不起作用
我正在尝试构建一个记录声音然后回放的应用程序。 录音部分工作完美,但当我尝试重放声音时,它什么也没做。 当我在调试器中运行它并逐步执行播放音频的步骤时,它可以工作。 当我删除所有断点并在调试中运行程序时,它不会。
该问题可能是由在后台完成的一些事情引起的,并且在我尝试运行音频之前尚未完成,但我不完全确定。
任何帮助将不胜感激。
以下是源代码的相关部分。
创建声音池
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我昨天遇到了同样的问题 - 在 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.为什么不尝试通过 MediaPlayer 播放录制的音频。
要播放录制的文件,请使用以下代码:
Why don't you try to play the recorded audio through MediaPlayer.
To play the recorded file use this code:
这可能被视为黑客攻击。但为我工作。基本上在加载和播放之间等待几毫秒
This may be considered a hack. But worked for me. Basically wait for a few milliseconds between load and play