Android Media Player returns wrong duration in API level 19 (Kitkat)

  • Thread starter Wrichik Basu
  • Start date
  • Tags
    Android
In summary: InterruptedException e) { //Log.e(this.getClass().toString(), "Millis: interrupted."); } handler.sendMessage(null); }
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,116
2,691
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:
Java:
public static long getDurationLeft() {
    //Log.e(this.getClass().toString(), "Millis: " + (mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition()));
    return (mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition());
}
And this is how the activity shows the time left:
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();

}
Here, 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)?
 
Technology news on Phys.org
  • #2
Rather than pursue this error in the OS for 19 perhaps you can find stats on the number of people using that version and then decide if it’s worth pursuing.

Is there any way you can use other v19 api to emulate what you really want it to do and skip calling the problem api?
 
  • #3
jedishrfu said:
Rather than pursue this error in the OS for 19 perhaps you can find stats on the number of people using that version and then decide if it’s worth pursuing.
When creating a new project in Android Studio, it gives a cumulative distribution chart for the version distribution:

1586935057841.png


From the above, about 4% of the Android phones are Kitkat. If this problem continues, I will have to ditch support for Kitkat and go straight up to Lollipop 5.1. I cannot publish an app to Play Store with a known bug.
jedishrfu said:
Is there any way you can use other v19 api to emulate what you really want it to do and skip calling the problem api?
For my laptop, I can run only x86 and x86-64 system images; other architectures like armeabi-v7a are very slow and not worth spending time on. With this limitation, I tried two out of the two system images available, and both showed the same error. My app should be independent of system architecture, by the way.
 
  • #4
So you have a problem with a 7-year old version of the operating system emulator. Seems that you have only a small number of choices:
  1. Buy or borrow a real phone and test it
  2. Accept the advice of the internet that this is an emulator-only problem
  3. Declare the 7-year oid OS version unsupported
  4. Require a more recent OS version
 
  • #5
For the while, I have changed the minSDK to 22 (Nougat). If the current version is successful on PlayStore, then I might roll out a beta version with minSDK 19 and check whether the app actually works. In the meantime, if anyone here has a phone running Kitkat and wants to help me by testing my app, send me a private message.
 

1. Why is my Android media player returning the wrong duration in API level 19 (Kitkat)?

This is a known issue in the Kitkat API level 19 for Android media players. It is caused by a bug in the media player's time calculation algorithm.

2. How can I fix this issue in my Android media player?

To fix this issue, you can use a workaround by manually calculating the duration of the media file using the media player's playback position and the total length of the media file. This workaround can be implemented by using the getDuration() and getCurrentPosition() methods.

3. Is there a permanent solution for this issue in API level 19?

Unfortunately, there is currently no permanent solution for this issue in API level 19. The bug has been reported to Google, but as of now, there is no official fix for it. It is recommended to use the workaround mentioned above until a permanent solution is released.

4. Does this issue affect all media players in API level 19?

Yes, this issue affects all media players in API level 19, regardless of the device or app. It is a bug within the Android system itself and not specific to any particular media player.

5. Are there any other issues related to media playback in API level 19?

Apart from the wrong duration issue, there are also other known issues related to media playback in API level 19, such as audio/video synchronization problems and audio playback glitches. These issues have also been reported to Google and are being worked on for future updates.

Similar threads

  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
6
Views
5K
Back
Top