Android MediaPlayer background playback
Created by:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(FILENAME);
mp.prepare();
Launched:
mp.start
Plays even when Activity onDestroy()
In the newly opened Activity, an mp is created and music plays on top of the already running mp
Question: how do I resume control of an already running mp in a newly opened Activity (after onDestroy ())?
2 answers
Either make the launch MediaPlayer
in Service
, or destroy MediaPlayer
in onDestroy()
.
It will play until you do mp. stop (), or mp. reset (), or mp. release (), or the system itself will not kill it.
In general, it is desirable to enter in the Activity onDestroy (): mp. release() Then the player will stop and release the occupied resources.
And in general, if there are problems with the player, look at the diagram and everything will be clear at once: http://developer.android.com/reference/android/media/MediaPlayer.html
In order to manage the player from multiple activiti, make a service and put the player in there. Here's what you need to know: http://startandroid.ru/ru/uroki/vse-uroki-spiskom/162-urok-97-service-binding-serviceconnection.html http://startandroid.ru/ru/uroki/vse-uroki-spiskom/163-urok-98-service-lokalnyj-binding.html