I am developing in Android and have been trying to create an Async task that plays audio files in order depending on a set of boolean values (if i1 is true, then it plays i1, and then when i1 is done, if i2 is true it plays that and so on). My first attempt is not crashing the app, but is also not playing the audio file.
class SessionMusicTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
try {
while(playing == true){
if(i1 == true && done == true){
done = false;
player.setDataSource(i1Loc);
player.prepare();
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
done = true;
}
});
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
Is this the best approach to the problem? Any idea why the file isn’t playing?
Thanks for any help in advance.
Update:
I just put a finish() in the onPostExecute() and the activity ends almost immediately, leading me to believe the task is not even starting to play the audio file?
Make sure you are releasing the MediaPlayer class when a song is done playing otherwise you could see some strange behavior: