Search code examples
androidandroid-intentmedia-player

MediaPlayer doesn't play in background when started by an intent


In my app I download an mp3 podcast and then play it with MediaPlayer using an intent:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW);

Uri data = Uri.parse("file://" + filepath);
intent.setDataAndType(data, "audio/*");

try {
        startActivity(intent);
} catch (ActivityNotFoundException e) {
        e.printStackTrace();

}

This works, but unfortunately the player pauses as soon as I leave it, ie. when it goes to the background. When opened manually the player happily plays in the background.

What do I need to make it play in background when starting it with an intent?

Just to clarify, I don't want to start the player in the background, I just wonder why it stops when it looses focus.


Solution

  • What do I need to make it play in background when starting it with an intent?

    You don't do it that way. You use the MediaPlayer class and operate it from within a service.