In Youtube Playlist, I have many Videos, now I would like to share Videos with others (using Video URL - which is playing right now)
I am using below code to play YouTube Playlist in an Android application:
public static final String PlayList_ID = "PLP7qPet500dfglA7FFTxBmB_snxCaMHDJ";
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
youTubePlayer = player;
Toast.makeText(getApplicationContext(),
"YouTubePlayer.onInitializationSuccess()",
Toast.LENGTH_LONG).show();
youTubePlayer.setPlayerStateChangeListener(myPlayerStateChangeListener);
youTubePlayer.setPlaybackEventListener(myPlaybackEventListener);
if (!wasRestored) {
player.cuePlaylist(PlayList_ID);
}
}
Inside onLoaded() you can get id from that id you can create url.
You can you this way.
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
player.setPlayerStateChangeListener(new PlayerStateChangeListener() {
#Override
public void onVideoStarted() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onLoading() {
}
#Override
public void onLoaded(String mVideoId) {
String url = "http://www.youtube.com/watch?v=" + mVideoId
}
#Override
public void onError(ErrorReason reason) {
}
#Override
public void onAdStarted() {
}
});
}
Related
I'm trying to create an Android application that, once searched for a video on youtube, plays it simultaneously in two YoutubePlayerViews, that is the code of the PlayerActivity
public abstract class PlayerActivity extends YouTubeBaseActivity implements OnInitializedListener {
private YouTubePlayerView playerView, playerView1;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_player);
playerView = (YouTubePlayerView)findViewById(R.id.player_view);
playerView1 = (YouTubePlayerView)findViewById(R.id.player_view1);
playerView.initialize(YoutubeConnector.KEY, new YouTubePlayer.OnInitializedListener(){
#Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(getApplicationContext(), getString(R.string.failed), Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean restored) {
if (!restored) {
player.cueVideo(getIntent().getStringExtra("VIDEO_ID"));
}
}
});
playerView1.initialize(YoutubeConnector.KEY, new OnInitializedListener() {
#Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(getApplicationContext(), getString(R.string.failed), Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean restored) {
if (!restored) {
player.cueVideo(getIntent().getStringExtra("VIDEO_ID"));
}
}
});
TextView video_title = (TextView)findViewById(R.id.player_title);
TextView video_desc = (TextView)findViewById(R.id.player_description);
TextView video_id = (TextView)findViewById(R.id.player_id);
video_title.setText(getIntent().getStringExtra("VIDEO_TITLE"));
video_id.setText("Video ID : "+(getIntent().getStringExtra("VIDEO_ID")));
video_desc.setText(getIntent().getStringExtra("VIDEO_DESC"));
}
}
the problem is that one video loads while the other remains on a black screen, this API allows simultaneously riproduction?
I want to control play video by user not system , it plays automatically now. I try to use youTuBePlayer.pause(); but it's not working.
How do i let the video be loaded , it's status is on pause ?
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_layout, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(getResources().getString(R.string.youTuBeApiKey), new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
if (!wasRestored) {
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
youTubePlayer.loadVideo(commonEducationObject.getHD_YouTubeID());
//youTubePlayer.play();
youTubePlayer.pause();//it's not working
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult error) {
String errorMessage = error.toString();
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
Log.d("errorMessage:", errorMessage);
}
});
Have tried?
youTubePlayerFragment.initialize(Constants.API_KEY, new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
yPlayer = player;
yPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
yPlayer.cueVideo(VIDEO_ID);
}
}
#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 the solution !
youTubePlayer.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onLoading() {
}
#Override
public void onLoaded(String s) {
youTubePlayer.pause();
}
#Override
public void onAdStarted() {
}
#Override
public void onVideoStarted() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onError(YouTubePlayer.ErrorReason errorReason) {
}
});
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();
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() {
}
};
I am using the YouTube API to play a video in my app.
I want to play the video right when the user enters the fragment so I used the command player.play(); in order to play automatically. However this command doesn't seem to work...
Here is my code:
public class YouTubeVideoFragment extends YouTubePlayerFragment{
YouTubePlayer player;
public YouTubeVideoFragment()
{
}
#Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
if(player!= null)
{
player.release();
}
}
#Override
public void onStop()
{
// TODO Auto-generated method stub
super.onStop();
if(player!= null)
{
player.release();
}
}
public static YouTubeVideoFragment newInstance(String url)
{
YouTubeVideoFragment f = new YouTubeVideoFragment();
Bundle b = new Bundle();
b.putString("url", url);
f.setArguments(b);
f.init();
return f;
}
private void init()
{
initialize("api_key", new OnInitializedListener() {
#Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1)
{
Log.e("YouTubeFragment", "Error");
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored)
{
if (!wasRestored)
{
player.cueVideo(getArguments().getString("url"));
player.play();//THIS DOESNT WORK!!!
}
}
});
}
}
If you need to load and play a video right after initialization of a fragment, you should simply use player.loadVideo(videoId) instead of player.cueVideo(videoId) and override onLoaded() hook.
Have a look at the docs:
loadVideo(String videoId)
Loads and plays the specified video.
cueVideo(String videoId)
Loads the specified video's thumbnail and prepares the player to play the video, but does not download any of the video stream until play() is called.
You have to call player.play() in onLoaded() of PlayerStateChangeListener().
So change onInitializationSuccess to this:
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored)
{
if (!wasRestored)
{
player.setPlayerStateChangeListener(new PlayerStateChangeListener() {
#Override
public void onVideoStarted() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onLoading() {
}
#Override
public void onLoaded(String videoId) {
player.play();
}
#Override
public void onError(ErrorReason reason) {
}
#Override
public void onAdStarted() {
}
});
}