我应该如何传递setSourceFile才能正常工作。
基本上我想同时录制和播放音频。
这是我的代码:
public class MainActivity extends ActionBarActivity {
String outputfile;
MediaRecorder recorder;
MediaPlayer player;
ParcelFileDescriptor[] pfd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recorder = new MediaRecorder();
pfd = new ParcelFileDescriptor[0];
try {
pfd=ParcelFileDescriptor.createPipe();
} catch (IOException e) {
e.printStackTrace();
}
}
public void start(View v) throws IOException {
recorder.setOutputFile(pfd[1].getFileDescriptor());//THIS IS WORKING
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
MediaPlayer player = new MediaPlayer();
try {
player.setDataSource(pfd[0].getFileDescriptor());// NOT WORKING
}catch (IOException e) {
e.printStackTrace();
}
//THIS IS THE EXCEPTION:
// java.io.IOException: setDataSourceFD failed.: status=0x80000000
plater.prepare();
player.start();
}
还有其他方法可以将音频从录音机传输到扬声器吗?