MediaRecorder不将音频保存到文件

时间:2016-05-20 05:33:21

标签: android audio mediarecorder android-mediarecorder

我正在试用Android音频捕捉示例(https://developer.android.com/guide/topics/media/audio-capture.html),但它似乎不适用于三星Galaxy S5(我测试过的唯一手机)。这是使用API​​级别23。

音频文件确实在磁盘上创建,但它是一个0字节的文件 - 这显然是不正确的。这使我相信MediaRecorder的某些内容不正确。

另一方面,MediaRecorder的getMaxAmplitude似乎正在运作 - 因此它可以访问麦克风。

我在SO上发现了一堆其他问题,但没有一个人有答案。有人最近遇到过这种情况吗?

3 个答案:

答案 0 :(得分:1)

我发布了所有内容的完整代码,所以你没有问题。

<强> 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;
        }
    }


}

<强>的manifest.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>

ScreenShot表单Android设备监控:

enter image description here

在我的java代码中看到我给它命名为audio_test4.3gp,而在ScreenShot中有一个同名文件,大小为3104

答案 1 :(得分:1)

我会假设您使用手机而不是模拟器。

如果您尝试使用计算机上的文件浏览器在录制后立即找到您的文件,您将无法在某些手机上看到它或使用零字节看到它。

使用手机上的stock文件浏览器找到该文件,我确定你会找到它(除非你有一些例外)。

祝你好运。

答案 2 :(得分:0)

这里我有MediaRecorder的完整工作代码,试试这个

    //For Creating new File everytime
    private static int audioIndex = 0;

 public void startRecording()
 {
    ++audioIndex;
    Log.d("Index", audioIndex + "");
    Ask.on(RecordingActivity.this)
            .forPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO)
            .withRationales("Give Permissions")
            .when(new Ask.Permission() {
                @Override
                public void granted(List<String> permissions) {


                    PackageManager pmanager = RecordingActivity.this.getPackageManager();
                    if (pmanager.hasSystemFeature(PackageManager.FEATURE_MICROPHONE)) {


                        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
                        mFileName += "/eatthatfrog" + audioIndex + ".3gp";
                        Log.d("File", mFileName);
                        mediaRecorder = new MediaRecorder();
                        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        mediaRecorder.setOutputFile(mFileName);
                        try {
                            mediaRecorder.prepare();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        mediaRecorder.start();


                        Log.d("Recording ", "True");

                    } else {
                        Toast.makeText(RecordingActivity.this, "This device doesn't have a mic!", Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void denied(List<String> permissions) {
                    Log.d("Permission Denied", permissions.toString());
                }
            }).go();
}


@Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.stop:
            mediaRecorder.stop();
            mediaRecorder.reset();
            mediaRecorder.release();
            myChronometer.stop();
    }

compile 'com.vistrav:ask:1.2'用于运行时权限

相关问题