如何使用YoyTubePlayerView Android在全屏横向模式下播放YouTube视频

时间:2022-01-07 18:57:54

I am trying to play video only in full screen mode using Youtube Intents

我正在尝试使用Youtube Intents在全屏模式下播放视频

What i am trying to do is i have a TubePlayerView in Xml As the code below

我想要做的是我在Xml中有一个TubePlayerView如下面的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/videoView1"

    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_marginTop="2dp"
    android:paddingTop="5dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />
</LinearLayout>

The youtubeplayer view with id videoView ,when i click on it i want to play video on new intent Using youtube intent as code follows.

具有id videoView的youtubeplayer视图,当我点击它时我想播放关于新意图的视频使用youtube意图作为代码如下。

But the problem is event after implementing onClick on it the Intent is not called

但问题是在实现onClick之后的事件没有调用Intent

public class CallingIntent extends YouTubeBaseActivity implements     YouTubePlayer.OnInitializedListener  {

public static final String develop_you_key = " i have my key";
YouTubePlayerView youTubeView;
TextView ID,Video_id,Video_name,duration,countView,published;
String ID1,Video_id1,Video_name1,duration1,countView1,published1;
private static final int RECOVERY_DIALOG_REQUEST = 1;
YouTubePlayer youTubePlayer;

MediaPlayer mPlayer;
Intent receInt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vid_play);

    Intent in = getIntent();
//  String st= in.getStringExtra("image");
    ID1=in.getStringExtra("ID");
    Video_id1=in.getStringExtra("video_id");
    Video_name1=in.getStringExtra("video_name");




    ID =(TextView) findViewById(R.id.textView1);
    Video_id =(TextView) findViewById(R.id.textView2);
    Video_name=(TextView)findViewById(R.id.textView3);

    ID.setText(ID1);
    Video_id.setText(Video_id1);
    Video_name.setText(Video_name1);


     youTubeView= (YouTubePlayerView) findViewById(R.id.videoView1);
    youTubeView.initialize(develop_you_key,(YouTubePlayer.OnInitializedListener)this);

    findViewById(R.id.videoView1).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = YouTubeIntents.createPlayVideoIntentWithOptions(getApplicationContext(), Video_id1, true, false);

            startActivity(intent);
            finish();
        }
    });;

    mPlayer = new MediaPlayer();


}



@Override
public void onInitializationFailure(YouTubePlayer.Provider player,
        YouTubeInitializationResult errorReason) {
    // TODO Auto-generated method stub
    String errorMessage = errorReason.toString();
    Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();



}


@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
        boolean wasRestored) {
    // TODO Auto-generated method stub


    if(!wasRestored)player.cueVideo(Video_id1);


    }
}

please observe the onclickListener and tell me on Intialization success how can i play video directly Using Youtube intent "YouTubeIntents.createPlayVideoIntentWithOptions"

请观察onclickListener并告诉我有关初始化的成功如何直接播放视频使用Youtube意图“YouTubeIntents.createPlayVideoIntentWithOptions”

The thing is that i want to load video don't want to have thumbnail image instead of VideoView .

问题是,我想加载视频不希望有缩略图而不是VideoView。

1 个解决方案

#1


After lot of modifications i changed in my xml file from com.google.android.youtube.player.YouTubePlayerView to com.google.android.youtube.player.YouTubeThumbnailView

经过大量修改后,我在xml文件中将com.google.android.youtube.player.YouTubePlayerView更改为com.google.android.youtube.player.YouTubeThumbnailView

and loading image `

和加载图像`

<com.google.android.youtube.player.YouTubeThumbnailView
    android:id="@+id/videoView1"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_marginTop="2dp"
    android:paddingTop="5dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>`

To load image from youtube and to play video in full screen mode the code modified to

要从youtube加载图像并以全屏模式播放视频,请将代码修改为

public class VideoDetailView extends YouTubeBaseActivity implements YouTubeThumbnailView.OnInitializedListener{

public static final String develop_you_key = "i have my key";
YouTubePlayerView youTubeView;
TextView ID,Video_id,Video_name,duration,countView,published;
String ID1,Video_id1,Video_name1,duration1,countView1,published1;
//YouTubePlayerView youtubeplayer =(YouTubePlayerView) findViewById(R.id.videoView1);
YouTubeThumbnailView YoutubeThumb ;
YouTubeThumbnailLoader youTubeThumbnailLoader;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.video_detail);

    Intent in = getIntent();
//  String st= in.getStringExtra("image");
    ID1=in.getStringExtra("ID");
    Video_id1=in.getStringExtra("video_id");
    Video_name1=in.getStringExtra("video_name");
    duration1=in.getStringExtra("duration");
    countView1=in.getStringExtra("viewcount");
    published1=in.getStringExtra("published");


//  VideoView vv =(VideoView) findViewById(R.id.videoView1);
    ID =(TextView) findViewById(R.id.textView1);
    Video_id =(TextView) findViewById(R.id.textView2);
    Video_name=(TextView)findViewById(R.id.textView3);
    duration= (TextView)findViewById(R.id.textView4);
    countView=(TextView)findViewById(R.id.textView5);
    published=(TextView)findViewById(R.id.textView6);
    ID.setText(ID1);
    Video_id.setText(Video_id1);
    Video_name.setText(Video_name1);
    duration.setText(duration1);
    countView.setText(countView1);
    published.setText(published1);

    YoutubeThumb= (YouTubeThumbnailView) findViewById(R.id.videoView1);
    YoutubeThumb.initialize(develop_you_key,(YouTubeThumbnailView.OnInitializedListener)this);
    YoutubeThumb.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = YouTubeIntents.createPlayVideoIntentWithOptions(getApplicationContext(), Video_id1, true, false);
            startActivity(intent);
        }
    });



}




@Override
public void onInitializationFailure(YouTubeThumbnailView thumbnail,
        YouTubeInitializationResult arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnail,
        YouTubeThumbnailLoader imageLoader) {
    // TODO Auto-generated method stub

    youTubeThumbnailLoader  = imageLoader;
    imageLoader.setOnThumbnailLoadedListener(new OnThumbnailLoadedListener() {

        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {
            // TODO Auto-generated method stub

        }
    });
    youTubeThumbnailLoader.setVideo(Video_id1);
}


}

#1


After lot of modifications i changed in my xml file from com.google.android.youtube.player.YouTubePlayerView to com.google.android.youtube.player.YouTubeThumbnailView

经过大量修改后,我在xml文件中将com.google.android.youtube.player.YouTubePlayerView更改为com.google.android.youtube.player.YouTubeThumbnailView

and loading image `

和加载图像`

<com.google.android.youtube.player.YouTubeThumbnailView
    android:id="@+id/videoView1"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_marginTop="2dp"
    android:paddingTop="5dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>`

To load image from youtube and to play video in full screen mode the code modified to

要从youtube加载图像并以全屏模式播放视频,请将代码修改为

public class VideoDetailView extends YouTubeBaseActivity implements YouTubeThumbnailView.OnInitializedListener{

public static final String develop_you_key = "i have my key";
YouTubePlayerView youTubeView;
TextView ID,Video_id,Video_name,duration,countView,published;
String ID1,Video_id1,Video_name1,duration1,countView1,published1;
//YouTubePlayerView youtubeplayer =(YouTubePlayerView) findViewById(R.id.videoView1);
YouTubeThumbnailView YoutubeThumb ;
YouTubeThumbnailLoader youTubeThumbnailLoader;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.video_detail);

    Intent in = getIntent();
//  String st= in.getStringExtra("image");
    ID1=in.getStringExtra("ID");
    Video_id1=in.getStringExtra("video_id");
    Video_name1=in.getStringExtra("video_name");
    duration1=in.getStringExtra("duration");
    countView1=in.getStringExtra("viewcount");
    published1=in.getStringExtra("published");


//  VideoView vv =(VideoView) findViewById(R.id.videoView1);
    ID =(TextView) findViewById(R.id.textView1);
    Video_id =(TextView) findViewById(R.id.textView2);
    Video_name=(TextView)findViewById(R.id.textView3);
    duration= (TextView)findViewById(R.id.textView4);
    countView=(TextView)findViewById(R.id.textView5);
    published=(TextView)findViewById(R.id.textView6);
    ID.setText(ID1);
    Video_id.setText(Video_id1);
    Video_name.setText(Video_name1);
    duration.setText(duration1);
    countView.setText(countView1);
    published.setText(published1);

    YoutubeThumb= (YouTubeThumbnailView) findViewById(R.id.videoView1);
    YoutubeThumb.initialize(develop_you_key,(YouTubeThumbnailView.OnInitializedListener)this);
    YoutubeThumb.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = YouTubeIntents.createPlayVideoIntentWithOptions(getApplicationContext(), Video_id1, true, false);
            startActivity(intent);
        }
    });



}




@Override
public void onInitializationFailure(YouTubeThumbnailView thumbnail,
        YouTubeInitializationResult arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnail,
        YouTubeThumbnailLoader imageLoader) {
    // TODO Auto-generated method stub

    youTubeThumbnailLoader  = imageLoader;
    imageLoader.setOnThumbnailLoadedListener(new OnThumbnailLoadedListener() {

        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {
            // TODO Auto-generated method stub

        }
    });
    youTubeThumbnailLoader.setVideo(Video_id1);
}


}