简体   繁体   English

MacBook Pro的Android仿真器中未播放音频

[英]Audio is not playing in Android Emulator in MacBook Pro

This is an issue in MacBook Pro. 这是MacBook Pro中的问题。 I figured out the problem after few hours of debugging 经过几个小时的调试,我发现了问题

I am using the following code to create a media player and play an mp3 file. 我正在使用以下代码创建媒体播放器并播放mp3文件。 I am using the built-in emulator in Android Studio. 我正在使用Android Studio中的内置模拟器。 Any suggestions for debugging? 有调试建议吗?

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 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>

EDIT 编辑

For further debugging I added an onErrorListener for mediaPlayer 为了进一步调试,我为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;
            }
        });

The following line is getting printed in logs. 以下行正在日志中打印。

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

This is a specific issue in Emulator running on MacBook Pro. 这是在MacBook Pro上运行的模拟器中的一个特定问题。 I tried to run the same app on actual devices and windows+ emulator both cases it was working fine. 我试图在实际设备和Windows +模拟器上运行相同的应用程序,两种情况都可以正常运行。

The solution here is hacky but it worked for me 这里的解决方案很烂,但对我有用

turn off Bluetooth and relaunch the emulated device 关闭蓝牙并重新启动仿真设备

I quote the documentation, "A "raw" resource is a file that the system does not try to parse in any particular way. However, the content of this resource should not be raw audio. It should be a properly encoded and formatted media file in one of the supported formats." 我引用了文档,““原始”资源是系统不会尝试以任何特定方式解析的文件。但是,该资源的内容不应是原始音频,而应是经过正确编码和格式化的媒体文件以一种受支持的格式。”

Check if your music file is in a supported format. 检查您的music文件是否为受支持的格式。

Check out the doc for further details. 查看文档以获取更多详细信息。 https://developer.android.com/guide/topics/media/mediaplayer.html https://developer.android.com/guide/topics/media/mediaplayer.html

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MacBook Pro崩溃 - 来自Android模拟器? - MacBook Pro crashing - from Android Emulator? 无法在 MacBook M1 Pro 上通过 Android Studio 安装 Android 模拟器 - Cannot install Android Emulator via Android Studio on a MacBook M1 Pro 模拟器在 Macbook pro 上的 Android Studio 中启动时显示快速保存对话框 - Emulator shows quick save dialog when launched in Android Studio on Macbook pro Android模拟器无法在Macbook Pro上完成启动(连接到套接字&#39;127.0.0.1:1970&#39;时出错) - Android emulator does not complete startup on Macbook Pro (Error while connecting to socket '127.0.0.1:1970') 在带有 Apple M1 芯片的 Apple MacBook Pro 中运行“react-native run-android”命令时无法检测到模拟器 - Unable to detect emulator when `react-native run-android` command is ran in Apple MacBook Pro with Apple M1 Chip Android Studio:“AVD Pixel_2_API_30 的模拟器进程已终止。” 在 MacBook Pro M1 (Apple Silicon) 上 - Android Studio: "The emulator process for AVD Pixel_2_API_30 has terminated." on MacBook Pro M1 (Apple Silicon) AVD 的模拟器进程在 16 英寸 MacBook Pro 中被杀死 - The emulator process for AVD was killed in MacBook pro 16 inch 如何在Macbook Pro上显示/查看android屏幕? - How to present/view an android screen on a macbook pro? 无法将Android设备连接到MacBook Pro - Can't connect Android devices to MacBook Pro Android Studio 在 MacBook Pro 2021 中安装失败 - Android studio install fail in MacBook Pro 2021
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM