I want to play video in arbitrary time for videoview in Android
_player.setVideoURI(Uri.parse(uri));
MediaController mc = new MediaController(this);
_player.setMediaController(mc);
_player.start();
Now I want set time: 10:20 to seek file and play it (10 min and 20 sec)
see VideoView.seekTo(int msec) method
Also note one thing from the doc
The playback position can be adjusted with a call to seekTo(int).
Although the asynchronuous seekTo(int) call returns right way, the
actual seek operation may take a while to finish, especially for
audio/video being streamed. When the actual seek operation completes,
the internal player engine calls a user supplied
OnSeekComplete.onSeekComplete() if an OnSeekCompleteListener has been
registered beforehand via
setOnSeekCompleteListener(OnSeekCompleteListener).
You have to wait until the video is prepared to seek.
int seekTimeMs = (10 * 60 + 20) * 1000; // 10:20
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.seekTo(seekTimeMs);
}
});
Related
I'm trying to play an youtube video using Youtube Video Player in my android app from given start_time and end_time.
I used player.loadVideo("_wcs7ixyDbY", 12000), so that my video starts playing after 12 seconds. But I want to end my video at 20 seconds.
I used
player.loadVideo(videoID, 12000);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if(player.getCurrentTimeMillis() <= 20000) {
handler.postDelayed(this, 1000);
} else {
handler.removeCallbacks(this);
player.pause();
}
}
}, 1000);
This option pause my video after 20 seconds.
How to end my video after 20 seconds (not pause)
Thank You
The Android YouTube API provides a method to stop / release the video also:
abstract void release()
Stop any loading or video playback and release any system resources used by this YouTubePlayer.
https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayer
i have an mp4 i am trying to view on my android in a video view but for some reason when i use seekto i can only seek to every 10 seconds. if i seek to 34 seconds it seeks to 30 and if i seek to 36 it seeks to 40. and so on for every thing i try to seek to. i have heard something about seek points in an mp4 file. is this whats causing my seek to fail?
videoView = (VideoView)findViewById(R.id.watchMp4View);
videoView.setVideoURI(Uri.parse(uri));
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
videoView.seekTo(timeToSkip); //
mediaPlayer.seekTo(timeToSkip);
mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mediaPlayer) {
videoView.start();
}
});
this issue is related to the creation of the .mp4 file, your file need to be prepared to allow seeking, in the encoding process check the Keyframe property, try with a shorter keyframe.
I just want to ask how to get the time of the video while it plays. I am using videoview. Let us say for example, when you are playing a video with a length of 10 minutes and you are already in 4th minute, how can I get the value of 4 minute? Any help would be appreciated. Thank you.
Below is my code in playing video.
private void PlayVideo(){
MediaController mc = new MediaController(getActivity());
mc.setAnchorView(videoMovie);
Uri video = Uri.parse(url);
videoMovie.setMediaController(mc);
videoMovie.setVideoURI(video);
videoMovie.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
videoMovie.start();
}
});
}
Look this tutorial
and if you want to get the time of the video while it plays, this code is work well
videoView.getDuration()
I'm currently working on an app that has to play audio. I have a method that plays a series of .mp3's in order. Here is the method:
/**
* Plays a series of sounds in order without delay.
*
* #param audioResourceIds
* An in-order array of the audio asset resources to play.
* */
public void playQueuedPlaylist(int[] audioResourceIds)
{
float lastPlayedSoundDuration;
for(int i = 0; i<audioResourceIds.length; i++)
{
lastPlayedSoundDuration = playMedia(audioResourceIds[i], null);
try
{
Thread.sleep((long)lastPlayedSoundDuration);
}
catch(Exception eh)
{
Log.v("BulacsAlmighty","Exception caught at playQueuedPlaylist()");
}
}
}
/**
* Used to play voice overs
*
* #param index
* id of the sound
* #param listener
* on completion listener
*/
public int playMedia(int index, OnCompletionListener listener)
{
MediaPlayer mp = MediaPlayer.create(context, index);
mp.setVolume(streamVolume, streamVolume);
stopMedia(index); // Stops the sound if it still is playing.
mp.start();
return mp.getDuration();
}
The problem is that the sounds sometimes do not play. And when they play, the timing is correct, the order is correct, but the last audio resource played is played at a significantly lower volume than the other sounds played before it.
Tell me, where did I go wrong!?
How are you stopping the previous tracks if you are not storing and releasing the associated MediaPlayer for that track? How does stopMedia work?
setVolume will fail if the MediaPlayer is on the Error state. Please set On<Event>Listeners so that you can really find out what is happening with the previously played MediaPlayers. Particularly, you might be interested in implementing OnInfoListener and OnPreparedListener.
Also, how is streamVolume modified? You might have a race condition somewhere and that code might be setting streamVolume preemptively. Please check all code that are modifying streamVolume.
Just curious why do you call
mp.setVolume(streamVolume, streamVolume);
at all? Is it really necessary? You don't mention why the volume needs to be adjusted from code.
I have the following code to take a video as a raw resource, start the video and loop it but I need the video to loop seamlessly as of now when it comes to an end of the clip and starts the clip again the transition between causes a flicker for a split second, which I really can't have for my app.
public class Example extends Activity {
VideoView vv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vv = (VideoView)findViewById(R.id.VideoView01);
//Video Loop
vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
vv.start(); //need to make transition seamless.
}
});
Uri uri = Uri.parse("android.resource://com.example/"
+ R.raw.video);
vv.setVideoURI(uri);
vv.requestFocus();
vv.start();
}
}
The clip is only 22 seconds long but was created to be seamless so it is possible to work without the delay.
Try this it will work 100%
VideoView videoView;<---write this in outside of method or else declare it as final variable.
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
In Kotlin simply use
videoView.setOnPreparedListener { it.isLooping = true }
Not sure if this helps years later, but I used
vv.start();
vv.setOnCompletionListener ( new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
vv.start();
}
});
and it has a seamless loop
The pause is for the underlying MediaPlayer to refresh its buffers. How long that will take will depend on a number of factors, many of which are outside your control (e.g., speed of CPU, speed of on-board flash storage).
One you can control is to get your video out of the resource and into the filesystem. Resources are stored in the APK, which is a ZIP file, so extracting the video this way probably takes extra time.
You may need to switch away from VideoView and use a SurfaceView with two MediaPlayers, alternating between them -- one is playing while the next is preparing, so when the playing one ends you can switch to the new player. I have not tried this, and so I do not know what the ramifications might be. However, I know that this technique is frequently used for audio playback to transition from one clip to another.
Little late, but any reason that you can't use the following?
MediaPlayer.setLooping(true);
If you are using Kotlin
videoView.setOnPreparedListener(object : MediaPlayer.OnPreparedListener {
override fun onPrepared(mp: MediaPlayer?) {
//Start Playback
videoView.start()
//Loop Video
mp!!.isLooping = true;
Log.i(TAG, "Video Started");
}
});
Using Arrow Expression short form
videoView.setOnPreparedListener { mp ->
//Start Playback
videoView.start()
//Loop Video
mp!!.isLooping = true;
Log.i(TAG, "Video Started");
};
Answer to this is to remove the audio from the video and convert that to a .ogg file which can be looped seamlessly and then use the video without audio to loop round and this works.
Here is answer friends, you must use vv.resume in setOnCompletionListener class
[https://stackoverflow.com/a/27606389/3414469][1]