- #1
- 2,173
- 2,711
I have almost completed my first Android app. It let's the user set a date and time, after which the user picks a media file. The app plays the media when the time comes. The app comes with an inbuilt media player using the Android MediaPlayer API. The countdown and the playback are done in a single foreground service that can run without user interaction once started.
Once the playback starts, the user can tap on the notification to launch an activity that shows the time left for the media to end. The media player can be paused or started from the notification (without requiring to launch the activity).
The service provides a static method for the activity to know the time left:
And this is how the activity shows the time left:
Here,
The app runs perfectly on API 22+. I tested it on an API 29 emulator and API 22 and 23 real phones. There is no problem anywhere. This video shows how the app should work normally (API 23).
On testing on an API 19 emulator, however, I find that the media player starts returning wrong duration once it is paused. See this video demonstrating the error. Notice that once the player is started after a pause, the time remains constant. If I pause it once again and start it, the time spring backs to the total media length.
I searched online if anyone else has faced such a problem, and the answer is yes, though no one has specifically mentioned API 19. StackOverflow has some questions mentioning this issue, but none of the answers worked for me. This answer says that the problem is solved on a real device and happens only on an emulator. But I do not have a real device running API 19, nor do I know anyone who has one.
Has anyone here faced this problem? Any known solution (other than changing minSDK to 22)?
Once the playback starts, the user can tap on the notification to launch an activity that shows the time left for the media to end. The media player can be paused or started from the notification (without requiring to launch the activity).
The service provides a static method for the activity to know the time left:
Java:
public static long getDurationLeft() {
//Log.e(this.getClass().toString(), "Millis: " + (mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition()));
return (mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition());
}
Java:
private void setTimeLeft() {
handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
Bundle b = msg.getData();
timeLeftView.setText(b.getString("TextView"));
}
};
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
Log.e(this.getClass().toString(), "Inside thread: " + Thread.currentThread().getName());
try {
while (true) {
Message message = Message.obtain();
Bundle bundle = new Bundle();
if (MahalayaService.mediaPlayer.isPlaying()) {
long millis = MahalayaService.getDurationLeft();
if (millis > 0) {
bundle.putString("TextView", DurationFinder.getDuration(millis, 1, getApplicationContext()));
} else {
break;
}
message.setData(bundle);
handler.sendMessage(message);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Log.e(this.getClass().toString(), e.getMessage());
}
} else {
//Log.e(this.getClass().toString(), "Millis: paused.");
}
}
} catch (IllegalStateException ignored) {
} finally {
Message message = Message.obtain();
Bundle bundle = new Bundle();
bundle.putString("TextView", "Playback complete");
message.setData(bundle);
handler.sendMessage(message);
}
}
});
thread.start();
}
MahalayaService
is the name of the service class, and DurationFinder
is a class that I have defined.The app runs perfectly on API 22+. I tested it on an API 29 emulator and API 22 and 23 real phones. There is no problem anywhere. This video shows how the app should work normally (API 23).
On testing on an API 19 emulator, however, I find that the media player starts returning wrong duration once it is paused. See this video demonstrating the error. Notice that once the player is started after a pause, the time remains constant. If I pause it once again and start it, the time spring backs to the total media length.
I searched online if anyone else has faced such a problem, and the answer is yes, though no one has specifically mentioned API 19. StackOverflow has some questions mentioning this issue, but none of the answers worked for me. This answer says that the problem is solved on a real device and happens only on an emulator. But I do not have a real device running API 19, nor do I know anyone who has one.
Has anyone here faced this problem? Any known solution (other than changing minSDK to 22)?