how to use YouTubeBaseActivity in Fragment ,android - android

I useing the tabhost, I hope to use YouTubeBaseActivity in fragment
what can I do?
public class yutubemain extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
private YouTubePlayerView ytpv;
private YouTubePlayer ytp;
final String serverKey="AIzaSyBrNphq0HsQvQ5Rx4-Kenff-FhumNVMbnY";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ytpv = (YouTubePlayerView) findViewById(R.id.youtubeplayer);
ytpv.initialize(serverKey, this);
}
#Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
Toast.makeText(this, "Initialization Fail", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasrestored) {
ytp = player;
Intent gt =getIntent();
ytp.loadVideo(gt.getStringExtra("id"));
}
}
this code change in fragment...

Use YouTubePlayerSupportFragment if you are only playing a YouTube video in a single support.v4.app.Fragment rather than YouTubeBaseActivity.

Related

Youtube Player video not loading when returning to Fragment

I have set up a YouTubePlayerView which works perfectly but I face a weird problem. When I go to another Fragment and then go back, YouTubePlayerView can't load the video, even if the exact code is executed.
private String currentVideoID "p450mjB3mxc";
private YouTubePlayerView youtubeView;
private void init(View view)
{
youtubeView = view.findViewById(R.id.youtubeView);
new YoutubeHelper(youtubeView,currentVideoID,this);
}
public void setYoutubePlayer(YouTubePlayer youtubePlayerPublic)
{
this.youtubePlayer = youtubePlayerPublic;
youtubePlayer.loadVideo(currentVideoID);
}
YoutubeHelper.class:
public class YoutubeHelper {
public final static String YoutubeAPIKey="MY_API_KEY";
public YouTubePlayer youtubePlayerPublic;
private YouTubePlayerView youTubeView;
public YoutubeHelper(YouTubePlayerView youTubeView, String initialVideo, MainFragment fragment)
{
this.youTubeView = youTubeView;
init(initialVideo,fragment);
}
private void init(String initialVideo,final MainFragment fragment)
{
youTubeView.initialize(YoutubeAPIKey,
new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
final YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo(initialVideo);
youtubePlayerPublic = youTubePlayer;
fragment.setYoutubePlayer(youTubePlayer);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
Log.d("Youtube Error:", youTubeInitializationResult.toString());
}
});
}
}
I have tried this answer with no success at all.
Any ideas?
I found the solution if anyone facing the same problem. Before leaving the fragment do:
youtubePlayer.pause();
youtubePlayer.release();

Trying to add a youtube video to a ListItem

I am creating a listview in Android. When I click on the item in the listview, it goes to second activity with the Youtubeplayer. But instead of playing the video, a message appears inside the Youtube player saying
An error occurred while initializing the youtube player
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final String[] sublectures = {"Part 1", "Part 2"};
ListAdapter appadapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sublectures);
ListView list2 = findViewById(R.id.list2);
list2.setAdapter(appadapter1);
list2.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String lecture = String.valueOf(parent.getItemAtPosition(position));
Intent startintent = new Intent(getApplicationContext(),Main3Activity.class);
startActivity(startintent);
}
});
}}
This is Main3Activity
public class Main3Activity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public static final String API_KEY = "AIzaSyBdVOqvu3C-MTnmO0iNOgeKBCSmb9ULrXc";
public static final String VIDEO_ID = "W2TYS_Jvzjc";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
YouTubePlayerView youTubePlayerView = findViewById(R.id.youtubePlayerView);
youTubePlayerView.initialize(API_KEY, this);
}
#Override
public void onInitializationFailure (YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult){
Toast.makeText(this, "Fail to Load", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
player.cueVideo(VIDEO_ID);
}
}
}
Try out this code works for me
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
public class VideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
YouTubePlayerView youTubePlayerView;
String API_KEY="AIzaSyB2TtG6UjEtDWfzxHvQCf2DJxkUPfJI1Lc";
private static final int RECOVERY_REQUEST = 1;
String TAG="VideoActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
youTubePlayerView=(YouTubePlayerView)findViewById(R.id.youtube);
youTubePlayerView.initialize(API_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
Bundle bundle = getIntent().getExtras();
String showVideo = bundle.getString("videoId");
youTubePlayer.cueVideo(showVideo);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
if (youTubeInitializationResult.isUserRecoverableError()) {
youTubeInitializationResult.getErrorDialog(this, RECOVERY_REQUEST).show();
} else {
Toast.makeText(this, "Error Intializing Youtube Player", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_REQUEST) {
getYouTubePlayerProvider().initialize(API_KEY, this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return youTubePlayerView;
}
}

Android YouTubePlayerApi: YouTubePlayer keeps on loading

I'm using YouTubePlayerAPI and YouTubePlayerSupportFragment in my app. Recently, I found that YouTubePlayer keeps on loading in first page when backed from secondary page. But I couldn't find out what is causing it. I've been looking for information but I haven't found anything useful.
My testing methodology:
Click the TextView to start the secondary page. Go back, the question has appeared.
Here is my codeļ¼š
public class FragmentDemoActivity extends Activity implements YouTubePlayer.OnInitializedListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragments_demo);
TextView tvDes = (TextView) findViewById(R.id.tv_start_another);
tvDes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(), FragmentDemoActivity.class));
}
});
YouTubePlayerFragmentWrap youTubePlayerFragment =
(YouTubePlayerFragmentWrap) getFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.cueVideo("nCgQDjiotG0");
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
}

Video is not playing in YoutubePlayer Fragment

I am making an app which uses Youtube player fragment. It is loaded but video is not getting played. Here is my code:
Youtube.java (Fragment)
public class Youtube extends YouTubePlayerFragment {
public Youtube() {
}
public static Youtube newInstance(String url) {
Youtube frag = new Youtube();
Bundle b = new Bundle();
b.putString("url", url);
frag.setArguments(b);
frag.init();
return frag;
}
private void init() {
initialize(API_KEY, new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(getArguments().getString("url"));
// player.play();
}
}
});
}
}
Activity:
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
Youtube f = Youtube.newInstance("https://www.youtube.com/watch?v=o49aHgzTOGw");
FragmentManager fragmentManager=getFragmentManager();
fragmentManager.beginTransaction().add(f,"Fragment").commit();
}
Layout:
<FrameLayout>
<fragment class="com.google.android.youtube.player.YouTubePlayerFragment"
android:id="#+id/Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Where I am going wrong. I am not able to figure it out. Do I need to extend YoutubeBaseActivity? How to achieve this?
I think you should write the code in official way. Take look at here, and download the zip file(sample applications included in the YouTubeAndroidAPIDemo package) here to get the sample. And take look at FragmentDemoActivity.
Basically, you need to do the Activity which include YoutubePlayerFragment as follows:
public class FragmentDemoActivity extends YouTubeFailureRecoveryActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragments_demo);
YouTubePlayerFragment youTubePlayerFragment =
(YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.cueVideo("nCgQDjiotG0");
}
}
#Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
}
}
YouTubeFailureRecoveryActivity :
public abstract class YouTubeFailureRecoveryActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_DIALOG_REQUEST = 1;
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(DeveloperKey.DEVELOPER_KEY, this);
}
}
protected abstract YouTubePlayer.Provider getYouTubePlayerProvider();
}
And the fragments_demo.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:id="#+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

youtube live video is not responding in android

This is my code so far:
public class MainActivity extends ActionBarActivity implements
YouTubePlayer.OnInitializedListener {
static private final String DEVELOPER_KEY = "USER API KEY";
static private String VIDEO = "LIuk1qu0RlU";
private YouTubePlayerFragment youTubePlayerFragment;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
youTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager()
.findFragmentById(R.id.youtubeplayerfragment);
youTubePlayerFragment.initialize(DEVELOPER_KEY, this);
}
#Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
if (result.isUserRecoverableError()) {
result.getErrorDialog(this, 0).show();
} else {
Toast.makeText(
this,
"YouTubePlayer.onInitializationFailure(): "
+ result.toString(), Toast.LENGTH_LONG).show();
}
}
#Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
Toast.makeText(getApplicationContext(),
"YouTubePlayer.onInitializationSuccess()", Toast.LENGTH_LONG)
.show();
player.cueVideo(VIDEO);
}
}
(recorded video is playing but live video is stopping my app)
I have tried video id = all recorded videos its working fine ..
when i tried videoid =LIuk1qu0RlU
it's not working, I have also tried other live video id

Categories

Resources