android - 所有设备都支持哪种 MediaRecorder 配置?
android - Which MediaRecorder configuration is supported by all device?
我正在我的应用程序中录制通信语音,我添加了存储和录音权限清单,并且还以编程方式获取。
我的代码在一台设备上运行良好(Android 6.0 Lenovo K3 Note)
但不是另一个 (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 大小)
我正在我的应用程序中录制通信语音,我添加了存储和录音权限清单,并且还以编程方式获取。
我的代码在一台设备上运行良好(Android 6.0 Lenovo K3 Note) 但不是另一个 (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 大小)