从内部存储播放soundpool

时间:2013-06-19 16:34:05

标签: android audio soundpool

我尝试了几种使用soundpool从内存加载声音的方法。首先这个剪切代码如何录制声音:

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
更改网址并按照此answer

进行操作

并且所有这些都没有结果。我试图使用此代码进行检查:

Log.d("sound", "soundpool play: " + soundPool.play(soundId, 1, 1, 0, 0, 1.0f));  

结果“soundpool play:0”

0 个答案:

没有答案