[英]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.