I am working on a MediaPlayer
to play an audio file
Currently works on the main thread
public boolean playFile(String path) {
MediaPlayer mp = new MediaPlayer();
mp.setAudioAttributes(AudioFocusManager.getPlaybackAttributes());
try {
mp.setDataSource(path);
mp.prepare();
mp.start();
mp.setOnErrorListener((mp1, what, extra) -> {
return true;
});
mp.setOnCompletionListener( mp1 -> {
});
return true;
} catch (Exception e) {
return false;
}
}
but I am trying to move it to an executor
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
});
Once I move it to the executor, then the second and third returns triggers an error
Unexpected return value
And playFile
triggers the following
Missing return statement
I have tried using AtomicReference
AtomicReference<Boolean> correctlyPlayed = new AtomicReference<>(false);
//set up MediaPlayer
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
MediaPlayer mp = new MediaPlayer();
mp.setAudioAttributes(AudioFocusManager.getPlaybackAttributes());
try {
mp.setDataSource(path);
mp.prepare();
mp.start();
mp.setOnErrorListener((mp1, what, extra) -> {
return true;
});
mp.setOnCompletionListener( mp1 -> {
});
correctlyPlayed.set(true);
} catch (Exception e) {
AudioFocusManager.releaseAudioFocus();
correctlyPlayed.set(false);
}
});
return correctlyPlayed.get();
but it triggers always false, meaning that it does not wait for the thread to update it
How could I do it?
As the comment said, use executor#submit
instead of executor#execute
, so you can get a Future
, and you can call Future#get
to get the result:
Future<Boolean> future = executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
// ...
return true;
}
});
return future.get(); // block until get result