我正在我的应用程序中录制通信语音,我添加了存储和录音权限清单,并以编程方式获取。
我的代码在一台设备上工作正常(Android 6.0联想K3注)但不在另一台设备上(Android 8.1 ONEPLUS A5010)
在第二个设备输出中保存为3.15KB的空白文件
我正在添加我正在使用的代码,请告诉我我做错了什么。
MediaRecorder mRecorder;
String mFileName;
OnCreate中的代码
File file = new File(getFilesDir(), "engwingoLastCall.3gp");
mFileName = file.getAbsolutePath();
try {
if(mRecorder == null) {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}
} catch (Exception e) {
Log.d(TAG,"Recorder Error:"+e.getMessage());
}
方法
public void startRecording() {
try {
if(mRecorder != null) {
mRecorder.prepare();
mRecorder.start();
}
} catch (Exception e) {
Log.d("Recorder", "prepare() failed");
}
}
public void stopRecording() {
if(mRecorder != null) {
try {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
} catch (IllegalStateException e) {
e.printStackTrace();
}catch (Exception e){
Log.d(TAG,e.getMessage());
}
}
}
由于您没有使用setProfile()
方法设置配置文件,因此您可能还需要为音频设置音频通道,比特率和采样率。这是一个例子:
mRecorder.setAudioChannels(1);
// you would not want to record stereo, it is not logical
mRecorder.setAudioEncodingBitRate(128000);
// you can set it to 64000 or 96000 to lower quality, therefore decreasing the size of the audio
mRecorder.setAudioSamplingRate(44100);
// AFAIK, default value.
希望这可以帮助。
我的代码没问题,但是我的录像机出现这种行为的原因是,其他一些服务也使用了我的录像机,这就是为什么文件保存为空(3.15KB大小)