我有一个播放音频的应用程序,我想在播放时录制至少10秒的音频。我已经查看了谷歌和微软的MediaRecorder类的官方文档,当你想使用该类录制音频时,我已经遵循了配方的每一步。我也声明了所有需要的权限,但我在设置MediaRecorder对象的输出文件时遇到了问题。文档说我需要在方法中使用FileDescriptor对象
mediaRecorder.SetOutputFile(FileDescriptor dp)
但该类的构造函数不接受文件名的字符串,我本能地知道,如果MediaRecorder对象将音频写入文件,那么该文件名需要一个名称,这样我以后可以搜索它并测试音频是如何录制的。
像这样实例化FileDescriptor会编译,但文件名呢?
MediaRecorder recorder = new MediaRecorder(this);
//set the source of the audio
recorder.SetAudioSource(AudioSource.Mic);
//set the media encoding of the output
recorder.SetOutputFormat(Android.Media.OutputFormat.Mpeg2Ts);
//specify a file descriptor where to save the recording
//what about the file name?, It surely needs a string for the file name
FileDescriptor destination = new FileDescriptor();
recorder.SetOutputFile(destination);
//prepare the recorder
recorder.Prepare();
//start recording for 10 seconds
recorder.SetMaxDuration(10000);
//start the recording session
recorder.Start();
通过检查FileDescriptor在Java中的使用方式,我了解到由于继承等原因,它可以像下面这样工作。
FileDescriptor destination = new FileOutputStream("myrecording");
//the line above does not compile for Xamarin Android
如何创建FileDescriptor实例,同时向其传递可用于搜索录制音频的文件名?