I am using an Youtube Player api for playing youtube videos in my application. Video start playing and pausing after 1-2 seconds
I created Video Fragment and ViewGroup. Subsequently I create some youtobe videoview.
VideoFragment
public static final class VideoFragment extends YouTubePlayerSupportFragment implements
OnInitializedListener
{
private YouTubePlayer player;
private String videoId;
public static VideoFragment newInstance()
{
return new VideoFragment();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
initialize(Constants.DEVELOPER_KEY, this);
}
@Override
public void onDestroy()
{
if (player != null)
{
player.release();
}
super.onDestroy();
}
public void setVideoId(String videoId)
{
if (videoId != null && !videoId.equals(this.videoId))
{
this.videoId = videoId;
if (player != null)
{
player.cueVideo(videoId);
}
}
}
public void pause()
{
if (player != null)
{
player.pause();
}
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean restored)
{
this.player = player;
if (!restored && videoId != null)
{
player.cueVideo(videoId);
}
}
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult result)
{
this.player = null;
}
}
Function for creating Youtobe videoview
private ViewGroup createYouTubePlayer(final VideoData data, final FrameLayout youTubePlayer)
{
youTubePlayer.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
FragmentManager fm = mActivity.getSupportFragmentManager();
if (v.getId() == mCurrentYouTubePlayer)
{
return;
}
VideoFragment fragment = (VideoFragment) fm.findFragmentById(mCurrentYouTubePlayer);
if (fragment == null)
{
fragment = VideoFragment.newInstance();
fragment.setVideoId(data.srcPath);
fm.beginTransaction().add(youTubePlayer.getId(), fragment).commit();
mCurrentYouTubePlayer = v.getId();
}
else
{
fm.beginTransaction().remove(fragment).commit();
fragment = VideoFragment.newInstance();
fragment.setVideoId(data.srcPath);
fm.beginTransaction().add(youTubePlayer.getId(), fragment).commit();
mCurrentYouTubePlayer = v.getId();
}
}
});
return youTubePlayer;
}
The principal reason your YouTube video may pause is because the Auto-Pause feature is set by default. This feature is designed to pause the videos if you've been inactive on the device for a while and prevent you from missing important details in the content you were watching.
Most often than not, the main reason behind the “YouTube keeps pausing or stopping” issue is buffering. Buffering occurs when the video isn't fully loaded. An unloaded video will keep stopping at certain points until it fully loads.
Try restarting the phone, so that all unneeded apps aren't running. Then, try playing a video. Start with ones that you have on the device first, ones that don't require internet access. They should work fine unless there is still something running in the background that is using up your phone's resources.
It is not possible to add buttons as overlays above the player as specified by Google, otherwise the player will stop:
https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayerView
Note that while videos are playing, this View has a minimum size of 200x110 dp. If you make the view any smaller, videos will automatically stop playing. Also, it is not permitted to overlay the view with other views while a video is playing.
This view does not support padding. To achieve the same effect, wrap the view in another ViewGroup or give it margins.
Padding is also not supported on YouTubePlayer.
To overlay your view on video, I will recommend you to use ExoPlayer, It is not part of the android sdk, but it's recommended by google and included in android developer documentation :
http://google.github.io/ExoPlayer/
Exoplayer allow you to stream any kind of video, not only Youtubes videos.
It's also good to mention that Exoplayer is used in youtube application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With