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!!!
}
}
});
}
}