我尝试了几种使用声音池从内部存储器加载声音的方法。首先这段代码是我如何录制声音的:
url = Environment.getExternalStorageDirectory().getAbsolutePath();
fileName = "/record.3gp";
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(url+fileName));
try {
recorder.prepare();
recorder.start();
}
catch (IOException e) {
Log.e("sound", "prepare() failed: " + e.getMessage());
}
那么这就是我使用媒体播放器播放声音的方式:
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(url+fileName);
mPlayer.prepare();
mPlayer.start();
mPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopSound();
}
});
}
catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我的录音播放了,没有错。但在某些情况下,我需要加载 soundpool。这些我试图与 soundpool 一起玩的代码:
// method 1
soundMap.put(1, soundPool.load(url+fileName, 1));
soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1.0f);
// method 2
soundId = soundPool.load(soundPool.load(url+fileName, 1), 1);
soundPool.play(soundId, 1, 1, 0, 0, 1.0f)
// 方法 3
更改 url 并按照这个答案
所有这些都没有结果。我尝试使用此代码进行检查:
Log.d("sound", "soundpool play: " + soundPool.play(soundId, 1, 1, 0, 0, 1.0f));
结果“soundpool play: 0”