Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the seekto () method of mediaplayer in Android

Shulou Source: shulou.com Published: 2022-06-03 11:02:21 11月15日 Update

This article mainly shows you "how to use the seekto () method of mediaplayer in Android". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn "how to use the seekto () method of mediaplayer in Android".

When android uses mediaPlayer to play video videos, when a user exits the current playback and resumes playback from the background, he / she needs to jump to the previous exit point to continue playback. The methods used are basically the point in time before SeekTo, but often encounter the problem of incorrect position when resuming playback, and even start playing all over again. This is because the SeekTo is caused by going back to the Keyframe near the previous point in time.

Seekto () method usage of mediaplayer in Android

1. The parameter of the seekto method is millisecond, not seconds, so you should pay attention to the parameter passing.

2. The seekto method is asynchronous, so if you want to write:

MediaPlayer.seekTo (10000)

MediaPlayer.start ()

It has no effect.

3. My personal test seekto has to be written after prepare to be effective, but I'm not sure I have to do it.

The correct method of seekto ()

If you want the video to start playing in xxxx milliseconds, the correct thing to do is:

First configure the properties for mediaPlayer

MediaPlayer.setDisplay (videoSurface.getHolder ())

MediaPlayer.setAudioStreamType (AudioManager.STREAM_MUSIC)

Try {

MediaPlayer.setDataSource (filePath)

MediaPlayer.prepare ()

MediaPlayer.seekTo (millis)

} catch (IOExceptione) {

}

Then add a listening OnSeekCompleteListener () for mediaPlayer

MediaPlayer.setOnSeekCompleteListener (newOnSeekCompleteListener () {

PublicvoidonSeekComplete (MediaPlayerm) {

M.start ()

IsPlaying=true

}

})

The above is all the content of the article "how to use the seekto () method of mediaplayer in Android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Method content time article practice parameter video learning help effective personal location key background attribute effect easy to understand more organized phenomenon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn MariaDB NVidia Apple