Reputation: 573
So i am trying to get a mediaplayer to work, but on first install the music doesnt start playing (i think it's because of permissions needed for the visualizer). But everytime afterwards it plays just fine.
code:
mPlayer = MediaPlayer.create(this, R.raw.bik);
mPlayer.setLooping(true);
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
Log cat:
11-23 17:18:29.979 28329-28329/com.waro.blockevader E/MediaPlayer: Error (-38,0)
11-23 17:18:29.979 28329-28329/com.waro.blockevader E/MediaPlayer: Error (-38,0)
11-23 17:18:29.979 28329-28329/com.waro.blockevader E/MediaPlayer: Error (1,-1010)
11-23 17:18:29.979 28329-28329/com.waro.blockevader E/MediaPlayer: Error (-38,0)
11-23 17:18:29.979 28329-28329/com.waro.blockevader V/MediaPlayer[Native]: isPlaying: 0
11-23 17:18:29.979 28329-28329/com.waro.blockevader E/MediaPlayer: Error (-38,0)
The file format is .mp4
Thanks for the help.
Edit:
What i meant by everytime after;
Everytime after i gave permissions and RESTARTED the app, the music starts playing just fine and no errors are given.
Permissions:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
Upvotes: 4
Views: 7358
Reputation: 573
I fixed it by changing a part in the onResumse(),
As expected it was indeed the permission causing havoc, i had:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
if (!mPlayer.isPlaying() && !mpCreated) {
initTunnelPlayerWorkaround();
init();
} else {
mPlayer.start();
mVisualizerView.link(mPlayer);
}
} else {
cleanUp();
mPlayer.start();
Log.i("boop","biep");
}
And finally discovered by that log it was going into the else, meaning it was calling the cleanUp(); method
And in the cleanUp() method i had:
private void cleanUp() {
if (mPlayer != null) {
mVisualizerView.release();
if(!mPlayer.isPlaying()) {
mPlayer.pause();
}
}
}
The 2nd if was causing havoc (duh) and i fixed it by changing it to:
private void cleanUp() {
if (mPlayer != null) {
mVisualizerView.release();
if(mPlayer.isPlaying()) {
mPlayer.pause();
}
}
I know this is a weird and specific answer, but maybe someone else who had the same struggle can find out why he or she was having problems.
Have a great day all and thanks for trying to help
~Waro (dh19, couldnt use waro sadly)
Upvotes: 1
Reputation: 37584
You are getting two different errors. The first one
E/MediaPlayer: Error (-38,0)
means that you try to call the MediaPlayer
without preparing it first.
The second error
E/MediaPlayer: Error (1,-1010)
means that the media is unsupported. You can see the error codes here and here
Upvotes: 5