Set to particular time for youtube video using YouTubePlayerSupportFragment - android

I'm aware of using
"&t=0m25s"
for setting the youtube video to 25 seconds from the beginning. But, can someone please help me to set the same feature using YouTubePlayerSupportFragment
Here is my code
YouTubePlayerSupportFragment playerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fl_video, playerFragment).commit();
playerFragment.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
player.play();
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult error) {
// YouTube error
String errorMessage = error.toString();
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
Log.d("errorMessage:", errorMessage);
}
});

I found a way to solve this problem. There's something known as
cueVideo()
function in YouTubePlayerSupportFragment.
player.cueVideo("QYw77aNGXnE", 92000);
NOTE : 92000 corresponds to 92 seconds (1 minute 32 seconds) as the value needs to be given in milliseconds.

Related

YouTubeAndroidPlayerApi video not playing when id passed from recycleview onClick listener

When I pass youtube ID from recycleview onClick then it shows "This video is unavailable". But same video id works fine when I hard code it. I tried every possible way but could not find any solution. Please, help if anyone can.
This is the code for sending video key,
intent.putExtra("vUrl", adListModel.getUrl());
Getting the video key,
vUrl = getIntent().getStringExtra("vUrl");
Here, I am trying to load video from the video key,
youTubePlayerView.initialize(getResources().getString(R.string.google_api_key), new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
String videoId = vUrl;
Log.e("YOUTUBE_KEYS", videoId);
youTubePlayer.cueVideo(videoId);
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
youTubePlayer.play();
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});

Embed config is not supported in RemoteEmbeddedPlayer?

Whenever I'm trying to open Youtube player in my app, onInitializationSuccess() function is always called. But it shows a blank screen.
First this error appears:
W/YouTubeAndroidPlayerAPI: Forcefully created overlay:affq#82a17f5 helper:Lazy#9b7098a view:null status: ....... {...}
and then:
E/YouTubeAndroidPlayerAPI: Embed config is not supported in RemoteEmbeddedPlayer.
Here is my code:
FragmentManager fragmentManager = ((AppCompatActivity)this.context).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment youtubeFragment = new YouTubePlayerSupportFragment();
fragmentManager.beginTransaction().add(R.id.GlideLayout ,youtubeFragment).commit();
utubevw = (YouTubePlayerSupportFragment) youtubeFragment;
utubecontainer = imageLayout.findViewById(R.id.youtubecontainer);
utubecontainer.setVisibility(View.VISIBLE);
mOnInitializeListener = new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo("_8kExcHqFi4");
Log.i("youtube", "Successful");
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.i("youtube:Fail", youTubeInitializationResult.toString());
}
};
utubevw.initialize(String.valueOf(R.string.googleapikey), mOnInitializeListener);
Searched everywhere, even posted in Github, but couldn't find a solution.
FYI - I don't want to use Webview to play Youtube.
I had the same problem just now and i was able to solve it by removing all possible overlaying views.
I had a FAB just closely overlaying the youtube fragment. After moving the FAB away the error was fixed.
Try using youTubePlayer.loadVideo(" ") instead of youTubePlayer.cueVideo(" ") to auto play the video when the fragment is started.
According to the docs, if you use the cueVideo() function, you need to call play() or it does not download any of the video stream. Whereas the loadVideo() auto plays the video.
I tried the answer of #PeterOla, it worked for sometime but after sometime the error came back, and after hours of struggle, i found that i had to parse just the key url (without the youtube/) in the youTubePlayer.cueVideo(""), and it worked well for me.

Embed YouTube live stream in android app

I'm trying to embed a live stream to android app. This code works perfect for other videos but when I put the URL code of live stream channel it is unable to load. It just keeps on loading. Suggest me something or is there any other API which I need to use for embedding live stream.
onInitializedListener = new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.("5kbU-C0FxqA");
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
};
First video id out-of-date so you need to replace another one and check agein
Also i think your code should be like below
onCreate
YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player);
youTubePlayerView.initialize(getString(R.string.google_developer_api_key), this);
OnInitializedListener
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo(""); // your video id
youTubePlayer.play();
}
If you can't make it work with the YouTubePlayer API, you may consider using this alternative player: android-youtube-player.
This is all you need to do to play a live video:
Add the player to your XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.pierfrancescosoffritti.androidyoutubeplayer.player.YouTubePlayerView
android:id="#+id/youtube_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Initialize the player and play a video:
YouTubePlayerView youtubePlayerView = findViewById(R.id.youtube_player_view);
getLifecycle().addObserver(youtubePlayerView);
youtubePlayerView.getPlayerUIController().enableLiveVideoUI(true);
youtubePlayerView.initialize(new YouTubePlayerInitListener() {
#Override
public void onInitSuccess(#NonNull final YouTubePlayer initializedYouTubePlayer) {
initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
#Override
public void onReady() {
String videoId = "2ccaHpy5Ewo";
initializedYouTubePlayer.loadVideo(videoId, 0);
}
});
}
}, true);
You can see a working example in the sample app.

Youtube YouTubePlayerSupportFragment not work correctly (loading always?)

I have a problem with YouTube (always loading).
Apparently works correctly "but only first time", I want say, the first instance (first run) works correctly and load correctly horitzontal and vertically, but when change fragment - > fragmentManager.beginTransaction() in my MainActivity (I have function on all fragments calls this method to beginTransaction) , and after I go to - > beginTransaction YouTube - > the video always load:
Example:
Step 1
First time - > MainActivity - > beginTransaction YouTube - > click - -> Load
correctly
Step 2
Second time - > MainActivity - > beginTransaction YouTube - > click - -> Load always
Close App (Menu) and repeat Step 1 and 2 .
This is my code :
public void youtube(){
youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
youTubePlayerFragment.initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
if (!wasRestored) {
youTubePlayer.cueVideo(VIDEO_ID);
youTubePlayers=youTubePlayer;
}
// Toast.makeText(getActivity(), "1", Toast.LENGTH_SHORT).show();
youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
#Override
public void onFullscreen(boolean b) {
if(b){
//Toast.makeText(getActivity(), "1", Toast.LENGTH_SHORT).show();
MainActivity.isYoutubeReadyHoritzontal=true;
knowIfPortraitAndBackManyally=0;
}else {
//Toast.makeText(getActivity(), "2", Toast.LENGTH_SHORT).show();
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
knowIfPortraitAndBackManyally=1;
MainActivity.countIFexit=0;
//MainActivity.isYoutubeReadyHoritzontal=false;
}
}
});
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format("YouTube Error (%1$s)",
errorReason.toString());
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
}
}
});
android.support.v4.app.FragmentManager fragmentManager = getChildFragmentManager();
fragmentManager.beginTransaction().replace(R.id.youtube_fragment,youTubePlayerFragment).commit();
MainActivity.isYoutubeReadyHoritzontal=true;
}
XML
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/youtube_fragment" >
I had the same problem and it was caused by a failure to properly dispose of a previous player instance. Make sure to call youTubePlayer.release() when it's no longer needed.

video playback displays blank screen with only audio?

I have to develop an one android application.
here i have to play the youtube video on full screen mode and normal mode.
i have using youtubeplayerfragment.
Here the video is playing well on fullscreen mode.
The Same video is didnot play on normal mode.
My Exact problem is,
When I try to play the video, all I get is blank video but I get all the audio and subtitles.
If I hit the back button, I see the last video frame before the video player closes.
Why am facing these issue ???
please give me any solution ??
am using following xml code:
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="#+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_below="#+id/title"
android:layout_height="wrap_content"/>
This is activity code:
_Video = articlevideo.substring(1);
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
YouTubePlayerSupportFragment fragment = new YouTubePlayerSupportFragment();
fragmentTransaction.add(R.id.youtube_fragment, fragment);
fragmentTransaction.commit();
fragment.initialize(DeveloperKey.DEVELOPER_KEY, new OnInitializedListener() {
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(_Video);
}
}
private static final int RECOVERY_DIALOG_REQUEST = 1;
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(getActivity(), RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
}
}
});
Whats wrong in my code .please give me any suggestions .
You can check YouTube Direct Lite App as a reference.
While switching to full screen or orintation change, activity is killed and created again in Android. So you may need something like
android:configChanges="orientation|screenSize" >
Check the manifest for it.

Categories

Resources