音频未在 MacBook Pro 的 Android 模拟器中播放

Audio is not playing in Android Emulator in MacBook Pro

This is an issue in MacBook Pro. I figured out the problem after few hours of debugging

我正在使用以下代码创建媒体播放器并播放 mp3 文件。我在 Android Studio 中使用内置模拟器。有什么调试建议吗?

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    MediaPlayer mediaPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mediaPlayer = MediaPlayer.create(this,R.raw.music);
        mediaPlayer.start();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ablejohnson.audiodemo.MainActivity">
</android.support.constraint.ConstraintLayout>

编辑

为了进一步调试,我为 mediaPlayer

添加了一个 onErrorListener
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
                Log.e("error","what:"+what+",extra"+extra);
                return false;
            }
        });

日志中打印了以下行。

D/Atlas: Validating map...
E/MediaPlayer: Should have subtitle controller already set
E/MediaPlayer: Error (1,-19)
E/error: what:1,extra-19

我引用文档,"A "raw"资源是系统不会尝试以任何特定方式解析的文件。但是,该资源的内容不应该是原始音频。它应该是以一种受支持的格式正确编码和格式化的媒体文件。"

检查您的 music 文件格式是否受支持。

查看文档了解更多详细信息。 https://developer.android.com/guide/topics/media/mediaplayer.html

支持格式: https://developer.android.com/guide/topics/media/media-formats.html

这是 MacBook Pro 模拟器 运行ning 中的一个特定问题。我尝试 运行 实际设备上的相同应用程序和 windows+ 模拟器这两种情况都工作正常。

这里的解决方案很老套,但对我有用

turn off Bluetooth and relaunch the emulated device