There is a BackgroundMusic class inherited from AsyncTask , which plays music in the background:

 public class BackgroundMusic extends AsyncTask<Void,Void,Void>{ private MediaPlayer mediaPlayer; @Override protected void onPreExecute() { //инициализация mediaPlayer } @Override protected Void doInBackground(Void... voids) { mediaPlayer.start(); return null; } public void setVolume(int volume){ mediaPlayer.setVolume(volume, volume); } 

In a single activity, an instance of BackgroundMusic is created, in it an additional stream, from where the setVolume method is setVolume for a static instance of BackgroundMusic activity.

 public class MyActivity extends Activity { public static BackgroundMusic backgroundMusic; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); backgroundMusic=new BackgroundMusic(); gameThread=new GameThread(context); setContentView(new GameView(gameThread)); } 

But an additional stream is created in which the music starts to play and the volume of the mediaPlayer successfully adjusted, parallel to the already running instance of BackgroundMusic (the volume in it remains unchanged). The debugger displays 3 AsyncTask , all in the WAIT state (although throughout the code, 2 AsyncTask , one of which is BackgroundMusic). Suspending all AsyncTask does not lead to anything. Most likely, problems with synchronization, but I can not properly organize it.

    1 answer 1

    Create a service that will play music and control MediaPlayer .

    In onStartCommand() write the control logic of the MediaPlayer , well, do not forget the return START_STICKY .

    Transfer to the Intent service with the necessary parameters, which you will parse in onStartCommand() and depending on the transferred control to MediaPlayer .

    PS And in your case AsyncTask launched MediaPlayer and finished its work.