我正在尝试使用 Visualizer 在 MediaPlayer 中捕获播放的文件并使用 AudioTrack 播放它。但捕获的数据只是噪声。
这是我的代码。
mPlayer = MediaPlayer.create(this, R.raw.call_ringtone);
mPlayer.setLooping(false);
mPlayer.start();
// mPlayer.setVolume(0, 0);
int minSize =AudioTrack.getMinBufferSize( 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT );
mySong = new AudioTrack( AudioManager.STREAM_MUSIC, 44100,AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT, minSize, AudioTrack.MODE_STREAM);
mVisualizer = new Visualizer(mPlayer.getAudioSessionId());
mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
Visualizer.OnDataCaptureListener captureListener = new Visualizer.OnDataCaptureListener()
{
Boolean toPlay = false;
@Override
public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
int samplingRate)
{
myl.add(bytes);
}
@Override
public void onFftDataCapture(Visualizer visualizer, byte[] bytes,
int samplingRate)
{
System.out.println("onFftDataCapture");
}
};
mVisualizer.setDataCaptureListener(captureListener,
Visualizer.getMaxCaptureRate() / 2, true, false);
mVisualizer.setEnabled(true);
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mediaPlayer)
{
mVisualizer.setEnabled(false);
stopIt = true;
}
});
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mySong.play();
int j=0;
while(j<myl.size())
{
mySong.write(myl.get(j), 0, myl.get(j).length);
Log.d("writing", Integer.toString(j)+" : "+Integer.toString(temp.length));
j++;
}
}
}).start();
字节只是产生原始白噪声。OnWaveForm 方法中的数据如何具有 PCM 格式?我怎么能从中得到?