我正在试用 Android 音频捕获示例 ( https://developer.android.com/guide/topics/media/audio-capture.html ),但它似乎无法在 Samsung Galaxy S5(我测试过的唯一手机)上运行。这是使用 API 级别 23。
音频文件确实是在磁盘上创建的,但它是一个 0 字节文件——这显然是不正确的。这让我相信 MediaRecorder
有问题。
另一方面,MediaRecorder 的 getMaxAmplitude
似乎在工作 - 所以它确实可以访问麦克风。
我在 SO 上发现了一堆其他问题,但没有一个有答案。最近有人遇到过这个问题吗?
最佳答案
我已经发布了所有内容的完整代码,因此您可以毫无问题地执行它。
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="68dp"
android:layout_marginTop="50dp"
android:text="Start Recording"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="64dp"
android:text="Stop Recording"
/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity implements View.OnClickListener{
private Button startButton;
private Button stopButton;
private MediaRecorder mediaRecorder;
private File audioFile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById(R.id.button1);
startButton.setOnClickListener(this);
startButton.setText("start");
stopButton = (Button) findViewById(R.id.button2);
stopButton.setOnClickListener(this);
stopButton.setEnabled(false);
stopButton.setText("stop");
audioFile = new File(Environment.getExternalStorageDirectory(),
"audio_test4.3gp");
}
private void resetRecorder() {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setAudioEncodingBitRate(16);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setOutputFile(audioFile.getAbsolutePath());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
mediaRecorder = new MediaRecorder();
resetRecorder();
mediaRecorder.start();
startButton.setEnabled(false);
stopButton.setEnabled(true);
break;
case R.id.button2:
try {
mediaRecorder.stop();
}catch (RuntimeException ex){
}
mediaRecorder.release();
mediaRecorder = null;
startButton.setEnabled(true);
stopButton.setEnabled(false);
break;
}
}
@Override
protected void onPause() {
super.onPause();
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}
}
}
list .xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.softeng.audiorecording" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Android 设备监视器的屏幕截图:
在我的 Java 代码中,看到我给它命名为 audio_test4.3gp
并且在 ScreenShot 中有同名的文件,它的大小是 3104
。
关于android - MediaRecorder 不将音频保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338606/