Reputation: 571
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
Upvotes: 0
Views: 1959
Reputation: 571
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
Upvotes: 10
Reputation: 612
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
Upvotes: 0