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.