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() {
}
};
}
答案 0 :(得分:0)
试试这个!!!
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;
}