MediaRecord无法启动Android 7.1.1模拟器

abs*_*tx1 5 java debugging android android-emulator

简而言之,我无法让MediaRecorder#record在Android 7.1.1模拟器上运行,并且不明白为什么.以下是进一步的细节:

环境:操作系统:Mac OS X El Capitan版本10.11.6

IDE:AndroidStudio 2.2.3版

问题:我编写了一个示例应用程序来演示此问题.这个应用程序包含一个带有OnClickListener的按钮,它将初始化和配置MediaRecorder实例,准备并开始录制.在设置中启用RECORD_AUDIO权限后,以下代码在AVD管理器下载和使用的Android 7.0 API 24仿真器(x86_64图形:硬件)上正确运行,但在Android 7.1.1 API 25上产生以下错误(使用Google API) )仿真器(x86_64图形:硬件):

仅在Android 7.1.1 API上出错25 java.lang.RuntimeException:启动失败.在android.media.MediaRecorder.start(Native方法)

问题 有谁知道问题是什么?我的设置是否有问题,不适合Android 7.1.1?到目前为止,这已在2台Mac上复制.

码:

MainActivity.java:

package com.abstractx1.testrecord;

import android.media.MediaRecorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button recordButton = (Button) findViewById(R.id.recordButton);

        recordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String path = getFilesDir().getAbsolutePath() + "/" + "AudioRecording.3gp";

                MediaRecorder mediaRecorder = new MediaRecorder();
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
                mediaRecorder.setOutputFile(path);

                try {
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                } catch (Exception e) {
                    Log.e("DEBUG-MainActivity", "Error", e);
                    Log.e("DEBUG-MainActivity",     Log.getStackTraceString(e));
                }

                Toast.makeText(MainActivity.this, "Recording started", Toast.LENGTH_LONG).show();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.abstractx1.testrecord.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:text="Record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/recordButton" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

AndroidMainfest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abstractx1.testrecord">

    <uses-feature
        android:name="android.hardware.microphone"
        android:required="false" />

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

谢谢,如果其他地方已经回答,我会提前道歉.我搜索了几天没有成功.此外,如果存在模糊/缺乏信息,那么我将扩展并回答问题.

这是一个基本的东西,我认为它一定是模拟器的一个问题.

有人可以帮我这个吗?

小智 5

由于不使用环境,我遇到了类似的问题,因此媒体播放器无法找到用于存储音频的路径,因此它适用于某些设备,但不适用于其他设备:https ://github.com/alkathirikhalid/android-uni-foundation

我还审查了您的代码我发现了可能的错误或至少是建议

  • 文件位置/路径,您应该使用Environment.getExternalStorageDirectory().getAbsolutePath()...让操作系统为您提供位置,因为不同的设备有不同的命名约定
  • 使用所需的功能为 false,那么您应该在调用媒体播放器之前检查设备是否有麦克风
  • 使用权限写入外部存储您打算存储录制的音频以存储并可能稍后使用/播放
  • 最后,如 Android 文档“注意:目前,Media Recorder 无法在模拟器上运行”中所述,不支持模拟器。 https://developer.android.com/reference/android/media/MediaRecorder.html

强烈建议在处理多媒体、位置和其他设备外围设备(例如传感器)时使用实际设备。

干杯。