37

I am having an odd issue where my audio file sometimes plays and sometimes does not play. The catch is that when it decides to not play, the DDMS gives me an:

E/MediaPlayer﹕ Should have subtitle controller already set

Because this is one-to-one with the music not playing, I have determined that this is probably the issue...

If the music is not playing and I hit the volume button it begins to play. If I wait about 30 seconds of no-play, it begins to start again (not looping).

Whats going on here? I am on KitKat using

        player = new MediaPlayer();
        AssetFileDescriptor afd = null;
        try {
            afd = getAssets().openFd("Theme.mp3");
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            player.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        player.setLooping(true); //restart playback end reached
        //player.setVolume(1, 1); //Set left and right volumes. Range is from 0.0 to 1.0
        player.start(); //start play back
bneigher
  • 818
  • 4
  • 13
  • 24
  • Have you found a solution to your problem? – Ephraim Mar 11 '14 at 06:42
  • 4
    possible duplicate of [Should have subtitle controller already set Mediaplayer error Android](http://stackoverflow.com/questions/20087804/should-have-subtitle-controller-already-set-mediaplayer-error-android) – rds Aug 20 '14 at 11:53
  • Sorry to be the useless "me too" but, did you manage to solve it? I am facing the same problem here *only* with 5.0 Lollypop. The same code have never failed before, and doesn't fail in an emulator with 4.4.4. – Fran Marzoa Nov 22 '14 at 21:37
  • 2
    Every answer in this question does not work. Don't waste your time. You want this: http://stackoverflow.com/a/20149754/1048340 – Jared Rummler Apr 11 '15 at 14:56

5 Answers5

10

Looking at a previous discussion on StackOverflow, and the referenced Android commit where this was introduced, the code above might not completely initialize the MediaPlayer object.

The KitKat example code for media playback suggests that you should call:

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

immediately after you construct the MediaPlayer, and before you call its setDataSource method.

Community
  • 1
  • 1
Will Angley
  • 1,392
  • 7
  • 11
  • 29
    This didn't change anything. – bneigher Feb 14 '14 at 23:12
  • 1
    I'm not entirely sure what's happening then. The log statement should *not* be enough to halt the program on its own; the only Android log that will do this is `Log.wtf`. Perhaps the issue is related to how the code posted above is called? – Will Angley Feb 14 '14 at 23:29
  • 1
    Could be. I am running Processing (extends pApplet) and calling the player logic on overritten onStart on destroy onPause ect... – bneigher Feb 14 '14 at 23:36
  • 1
    Although this is more speculative - I've never used Processing - you might look into using the MediaPlayer [asynchronously](http://developer.android.com/guide/topics/media/mediaplayer.html#preparingasync). There may be a conflict between the way Processing is trying to handle your app's threads at runtime, and the way the synchronous MediaPlayer calls are expecting them to be handled. – Will Angley Feb 14 '14 at 23:39
  • im using the code that google recommends, including setAudioStreamType() and i still get this error. tried it in the main thread and async. same error. –  Aug 20 '14 at 12:22
1

I had the same issue and I fixed it by adding the following right after instantiating MediaPlayer.

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        if (mp == mediaPlayer) {
                            mediaPlayer.start();
                        }
                    }
                });

Previously I was implementing MediaPlayer.OnPreparedListener and overriding onPrepared() but it didn't work.

I hope this helps!

-1

This should fix your problem (did for me): Replace the line that says "player.start()" following the rest of your code with an async callback like so:

player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mediaPlayer.start();
    }
});

This error is just a Log.e, not a real error. It shouldn't cause your player to not play, I'm guessing it's just because the player hadn't finished preparing when you try to call start().

E/MediaPlayer﹕ Should have subtitle controller already set
wblaschko
  • 3,252
  • 1
  • 18
  • 24
  • 2
    No, this has no impact. I am having the same issue here, and I've code that used to run just fine. Now I get the same halt (of audio, not the app -- app is fine, i can start another audio track for example) right after seeing that error log. – Ephraim Mar 11 '14 at 06:41
-1

Its been a long time since I was working on this app. Here is what I ended up doing to get this to work. (Tested on KitKat and Lollipop). I think switching from MediaPlayer to APMediaPlayer was part of the trick.

@Override
public void onDestroy() {
    if(player != null) {
        player.release();
        player = null;
    }
    super.onDestroy();
}


@Override
public void onStart() {
    super.onStart();
    if(player != null) {
        player.start();
    }
    else {
        player = new APMediaPlayer(this); //create new APMediaPlayer
        player.setMediaFile("Theme.mp3"); //set the file (files are in data folder)
        player.start(); //start play back
        player.setLooping(true); //restart playback end reached
        player.setVolume(1, 1); //Set left and right volumes. Range is from 0.0 to 1.0
    }

}

@Override
public void onResume() {
    super.onResume();
    if(player != null) {
        player.start();
    }

}
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
bneigher
  • 818
  • 4
  • 13
  • 24
-12

set in manifest file may help you

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Lokesh G
  • 831
  • 11
  • 19