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() {
}
};
Related
I am trying to play youtube video, In Picture in Picture mode.
For this I am using youtube player api, when I press back button youtube player activity comes in picture in picture mode but video is stops playing, when i press play button then it again comes in full screen and start playing from start.
In full screen after rotating screen video is playing normally and start playing video where it before rotating.
I am not getting continues playing video in PIP.
Here is my code
public class YoutubePIPActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener
{
private static final String API_KEY = AppConstant.YUTUBE_PLAYER_API_KEY;
private static String mVIDEO_ID = "o7VVHhK9zf0";
private boolean isPIPModeEnabled = true;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube_pip);
YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);
youTubePlayerView.initialize(API_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored)
{
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(mVIDEO_ID);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult)
{
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
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() {
}
#Override
public void onVideoStarted() {
}
};
#Override
public void onBackPressed()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
&& getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)
&& isPIPModeEnabled)
{
enterPIPMode();
}
else
{
super.onBackPressed();
}
}
private void enterPIPMode()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
&& getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
Rational rational = new Rational(250, 150);
PictureInPictureParams.Builder params = new PictureInPictureParams.Builder();
params.setAspectRatio(rational);
this.enterPictureInPictureMode(params.build());
} else
{
this.enterPictureInPictureMode();
}
new Handler().postDelayed(() -> checkPIPPermission(), 30);
}
}
#RequiresApi(Build.VERSION_CODES.N)
void checkPIPPermission()
{
isPIPModeEnabled = isInPictureInPictureMode();
if(!isInPictureInPictureMode())
{
onBackPressed();
}
}
#Override
protected void onUserLeaveHint()
{
super.onUserLeaveHint();
enterPIPMode();
}
#Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig)
{
if(newConfig !=null){
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
}
}
}
Here is activity defined in manifest
<activity android:name=".ui.activity.youtube.YoutubePIPActivity"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
android:exported="false"
android:resizeableActivity="true"
android:supportsPictureInPicture="true"
android:theme="#style/Theme.AppCompat.FullScreen"
tools:targetApi="n"/>
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]
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() {
}
};
}
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();
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() {
}
};