简体   繁体   中英

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

I am using the following code to create a media player and play an mp3 file. I am using the built-in emulator in 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

<?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.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. I tried to run the same app on actual devices and windows+ emulator both cases it was working fine.

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.

Check out the doc for further details. https://developer.android.com/guide/topics/media/mediaplayer.html

Support format: https://developer.android.com/guide/topics/media/media-formats.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM