Android - Youtube Player Stop an video at specified time - android

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

Related

Control the playback time of a video

I'm playing a video in ExoPlayer. Now I want to distribute a specific time for that video to the user so that the user chooses.
For example, I only want to display 30 seconds to 50 seconds.
In AndroidStudio and JAVA language.
Thanks for helping me
It's not related to ExoPlayer! You can handle it on your own.
So use this snippet to solve this problem:
At the same time with starting video:
videoview.seekTo(SPECIFIED_START_TIME);
Then run this handler:
handler = new Handler();
runnable = new Runnable() {
#Override
public void run() {
if(videoview.getCurrentPosition() >= DESTINATION_TIME){ // After reaching destination time
videoview.seekTo(SPECIFIED_START_TIME);
videoview.pause(); // or stop();
}
}
};
handler.postDelayed(runnable, 0);

Controlling video playback speed during online video streaming in android

I am to play video in android from Vimeo doing online streaming. For this, Right now, I am using VideoView. Now, I wanna control the video playback speed control feature like: 0.5x,1x,1,5x,2x And video cache support.
Are both of these possible with Android VideoView or is there any third player that I can use?
Help!
Hi for the best control i advice you with vlc lib or pvPlayer. or you can use this simple code but it dosen't work well.
public void slowMotion(){
final Handler handler = new Handler();
Runnable r = new Runnable() {
#Override
public void run() {
if (videoview != null) {
if (videoview.isPlaying()){
videoview.pause();
}
else{
videoview.start();
}
}
handler.postDelayed(this, 50);
}
};
handler.postDelayed(r, 800);
}
Good luck :D

Android Playing audio file (.wav)

Need to play some .wav file but only some part of it (from start).
For example I have test.wav file and it is 10 seconds I want to play only 0-5 seconds.
I try to use seekTo method but it doesn't help my app was crashed.
AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.test);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_SYSTEM);
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mMediaPlayer.setOnPreparedListener(mOnPreparedListener);
mMediaPlayer.setOnCompletionListener(mMediaPlayerCompletionListener);
mMediaPlayer.seekTo(5000);
mMediaPlayer.prepare();
You could just stop playing after set amount of time (5 sec in your case). There are many ways to do that, one could be:
final Handler handler = new Handler();
//create a runnable that will be called to stop playback
final Runnable runnable = new Runnable() {
#Override
public void run() {
//stop playback, make sure mMediaPlayer is declared as a field
//in the class where it's used
mMediaPlayer.stop();
}
};
//post a job for handler do be done in 5 seconds
handler.postDelayed(runnable, 5 * 1000);
you can't seek before you call prepare, i am not sure whether you can do that before actually calling MediaPlayer.play() to be honest you need to check that up but for sure prepare goes first.

How to Play video from an Arbitrary Time in Android?

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);
}
});

How to make video ads disappear after xx seconds in Android?

I am making a video ads, now i can play video from internet, redirect to new url when user tap to the video.
But i dont know how to make Skip button, and stop , disappear my video ads after some seconds
I search google for a long time, but i cant get the solution!
can you give me some advice please?
I found the solution in this way:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if(mVideoViewer.isPlaying()){
mVideoViewer.stopPlayback();
}
// Do something
Log.i(getCallingPackage(), "====== Auto stop ===========");
}
}, 15000);
Stop play video after 15 seconds
Try this to stop playing video.
Timer timer=new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
if (videoView.isPlaying())
{
videoView.stopPlayback();
}
}
},10000);

Categories

Resources