Android - YouTubePlayer Play Live - android

I am using YouTube Android Player API to do a project about youtube live.
public class YoutubeLiveActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
private Context mContext = this;
YouTubePlayerSupportFragment youTubePlayerFragment;
private YouTubePlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube_live);
youTubePlayerFragment = (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize(API_KEY, this);
}
private void playVideoAtSelection() {
if (!(player == null)) {
player.loadVideo(LIVE_ID);
}
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider arg0, YouTubePlayer arg1, boolean arg2) {
this.player = arg1;
playVideoAtSelection();
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
}
}
YouTubePlayer cannot play the live and display "An error occurred Tap to retry". However, it can play a normal video. Please help!!
********** Updated **********
I found the problem about cannot play live video on YouTubePlayer. It is because the youtube version of mobile is not the latest version. Once updated the youtube on play store, the live video can play normally.

// Use the Youtube player.
public class YouTubePlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public String videoId = "";
private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
finish();
}
#Override
public void onVideoStarted() {
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_you_tube_player);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
videoId = bundle.getString(CommonUtils.INTENT_YOUTUBE_VIDEO_ID);
}
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(CommonUtils.You_Tube_Api_Key, this);
}
#Override
protected void onResume() {
super.onResume();
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "Error: Fail to Initialize!", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
if (!wasRestored) {
player.cueVideo(videoId);
}
}
}
activity_you_tube_player.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:padding="0dp" />
</LinearLayout>
Download Youtube jar File From this link, and Copy it into libs folder
[https://developers.google.com/youtube/android/player/downloads/][1]

Related

Changing the video upon the changing of the link in YouTube player

I am pasting a link to video in edittext. I want the video to reload when the link is changed and button is clicked after it. The edit text take the http address, splits it accordingly to get the exact link for YouTube player. Each time the address is changed , the address string that is being passed to YouTube player is changed too.
I logged it and it's working fine. But the problem is that when I paste the URL for the first time, it works fine and loads the video, but once the video is loaded and i change the URL and press the button to play video, nothing happens. I guess the playerview is initialized and i cant reinitialize it? Is there a way to reinitialize it?
Here is my code of XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/search"
android:hint="Paste the link here"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Video"
android:layout_centerHorizontal="true"
android:layout_below="#+id/search"
android:id="#+id/play"/>
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube"
android:layout_below="#+id/play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"/>
</RelativeLayout>
And the code to java file is :
public class YoutubeMain extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public static final String API_KEY = "api key";
public String VIDEO_ID;
Button playvideo;
EditText videoid;
public Boolean clicked;
YouTubePlayer myoutubeplayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.youtube);
videoid=(EditText)findViewById(R.id.search);
playvideo=(Button)findViewById(R.id.play);
playvideo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
clicked=true;
if (videoid.getText().toString().contains("="))
{
Log.d("Youtube","the id is:"+videoid.getText().toString());
int start = videoid.getText().toString().indexOf("=");
String suffix = videoid.getText().toString().substring(start + 1);
Log.d("Youtube","the id after cropping is: "+suffix);
VIDEO_ID=suffix;
}
else
{
Log.d("Youtube","the id is:"+videoid.getText().toString());
int start = videoid.getText().toString().indexOf(".");
String suffix = videoid.getText().toString().substring(start + 4);
Log.d("Youtube","the id after cropping is: "+suffix);
VIDEO_ID=suffix;
}
Log.d("Youtube","Suffix:" +VIDEO_ID);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube);
youTubePlayerView.initialize(API_KEY, YoutubeMain.this);
}
});
}
#Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
Log.d("Youtube","The value of clicked is : "+clicked);
}
private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onVideoStarted() {
}
};
}
Try this!!!
private static final int RECOVERY_REQUEST = 1;
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if(!b){
startHttp(strUrl, youTubePlayer);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_REQUEST).show();
} else {
String error = String.format(getString(R.string.player_error), errorReason.toString());
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
}
}
private void startHttp(final String strUrl, final YouTubePlayer youTubePlayer) {
youTubePlayer.cueVideo(strUrl);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(Config.YOUTUBE_API_KEY, this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return youTubePlayerView;
}

Error in initializing the YouTube player embedded into app

I'm trying to build android app which requires to open YouTube video. I'm using embedded YouTube player to play YouTube video in my application.
But I'm get the following error:
An error Occurred While Initializing the YouTube Player
What does this error means?
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
public static final String API_KEY = "AIzaSyBx5WtljJM0xq4TRqEsSh-V0ltd0elfOSk";
public static final String VIDEO_ID = "B-3e_Ynhwj4";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubePlayerView.initialize(API_KEY, this);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onVideoStarted() {
}
};
}

Youtube player getting paused in every second which is inside View Pager

I have implemented a Youtube player using YouTubePlayerSupportFragment inside a ViewPager which I have did successfully. The problem which I am getting is YoutubePlayer getting paused in every second whenever I tried to play it. But if I play it in full screen then its playing perfectly but having issue playing inside View Pager. The code for the YouTubePlayerSupportFragment is:
private void initYoutubeFragment() {
final YouTubePlayerSupportFragment youTubePlayerFragment =
YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.fl_coach_profile_youtube, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(getString(R.string.youtube_player_api_key)
, new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
mbIsVideoRestored = wasRestored;
if (!wasRestored) {
mYoutubePlayer = player;
mYoutubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
mYoutubePlayer.setPlaybackEventListener(mPlaybackEventListener);
mYoutubePlayer.setPlayerStateChangeListener(mPlayerStateChangeListener);
} else {
player.play();
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult error) {
}
});
}
Listeners for the player
// Youtube player event listener
private YouTubePlayer.PlaybackEventListener mPlaybackEventListener = new YouTubePlayer.PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
// Youtube player state change listener
private YouTubePlayer.PlayerStateChangeListener mPlayerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onVideoStarted() {
}
};
Use release() method before moving to another Fragment:
youTubePlayer.release();

Optimum way to implement youtube player into android application

I'm working on a android application and i get mp4 file urls from a json file. Now i want to play these files inside my app using a youtube player. After some research i managed to play my videos within my app. But when i go back from playing videos to other parts of my app,the app seems to be slowed down. I need to know if i'm doing this correctly.
This is my code used to play the video.
if (YouTubeApiServiceUtil.isYouTubeApiServiceAvailable(activity).equals(
YouTubeInitializationResult.SUCCESS)
&& android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Intent intent = YouTubeStandalonePlayer
.createVideoIntent(activity, API_KEY,
video.getFile());
startActivity(intent);
}
from video.getFile(); i'm getting my video url.
i have used YouTubeAndroidPlayerApi.jar as the library.
1.Download YouTubePlyaer API https://developers.google.com/youtube/android/player/downloads/
Register your app on google developer console https://console.developers.google.com
Take an unique API Key and use that in your App.
use below code
public class AboutUs extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_about_us);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(Constants.YOUTUBE_API_KEY, this);
initViews();
}
private void initViews() {
Button btnVisitMega = (Button) findViewById(R.id.btn_visit_megaforties);
Button btnVisitSecurity = (Button) findViewById(R.id.btn_visit_security_seals);
btnVisitMega.setOnClickListener(this);
btnVisitSecurity.setOnClickListener(this);
}
#Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(Constants.YOUTUBE_VIDEO_ID);
}
}
private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onVideoStarted() {
}
};

YouTube Gdata APIU is no longer available, any alternative for this?

Gdata API for YouTube feed is not working, from last some days.. so any alternative for to get feed from YouTube API..??
http://gdata.youtube.com/feeds/api/playlists/PL_yIBWagYVjyyqx_qPkbat5zufWZOyZEZ
Download YouTubePlyaer API https://developers.google.com/youtube/android/player/downloads/
Register your app on google developer console https://console.developers.google.com
Take an unique API Key and use that in your App.
use below code
public class AboutUs extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_about_us);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(Constants.YOUTUBE_API_KEY, this);
initViews();
}
private void initViews() {
Button btnVisitMega = (Button) findViewById(R.id.btn_visit_megaforties);
Button btnVisitSecurity = (Button) findViewById(R.id.btn_visit_security_seals);
btnVisitMega.setOnClickListener(this);
btnVisitSecurity.setOnClickListener(this);
}
#Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(Constants.YOUTUBE_VIDEO_ID);
}
}
private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onVideoStarted() {
}
};

Categories

Resources