gpt4 book ai didi

android - youtubeplayer在启动新视频时立即停止

转载 作者:行者123 更新时间:2023-12-03 05:54:59 24 4
gpt4 key购买 nike

我能够初始化youtube视频,但是每当我尝试加载另一个播放现有视频的视频时,我都会创建一个方法,如何停止一个youtube视频,而后者通常用来停止和加载另一个视频。

        YouTubePlayer youTubePlayerMain;

private void init(final String VIdeoId) {
rvMainl.setVisibility(View.GONE);
youTubeView.setVisibility(View.VISIBLE);
if (youTubePlayerMain != null)
{
try{
youTubePlayerMain.pause();
youTubePlayerMain.cueVideo(VIdeoId);
}
catch(Exception e){
e.printStackTrace();
}
}
else
{

youTubeView.initialize(Config.YOUTUBE_API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
youTubePlayerMain = youTubePlayer;
if (!b) {
youTubePlayer.cueVideo(VIdeoId); // Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo

}

}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
if (youTubeInitializationResult.isUserRecoverableError()) {
youTubeInitializationResult.getErrorDialog(Youtube.this, RECOVERY_REQUEST).show();
} else {

Toast.makeText(Youtube.this, youTubeInitializationResult.toString(), Toast.LENGTH_LONG).show();
}
}
});
}

}

我发现问题,全局YouTubePlayer并第二次加载视频。

最佳答案

这是为我工作。

Youtube.java( Activity )

public class Youtube extends YouTubeBaseActivity implements View.OnClickListener {
public static final String API_KEY = Config.YOUTUBE_API_KEY;
boolean doubleBackToExitPressedOnce = false;
//http://youtu.be/<VIDEO_ID>
public static final String VIDEO_ID = "ZWub9VTECxQ";
LinearLayout llVideo1, llvideo2, llVideo3,llVideo4;
ImageView ivBack;

private static final int RECOVERY_REQUEST = 1;
private YouTubePlayerView youTubeView;
RelativeLayout rvMainl;
YouTubePlayer youTubePlayerMain;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_youtube_player);
rvMainl = (RelativeLayout) findViewById(R.id.rvMain);
ivBack = (ImageView) findViewById(R.id.youtube_iv_back);

youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.setVisibility(View.GONE);
llVideo1 = (LinearLayout) findViewById(R.id.frag_youtube_btn_one);
llvideo2 = (LinearLayout) findViewById(R.id.frag_youtube_btn_two);
llVideo3 = (LinearLayout) findViewById(R.id.frag_youtube_btn_three);
llVideo4 = (LinearLayout)findViewById(R.id.frag_youtube_btn_four);
llVideo4.setOnClickListener(this);
llVideo1.setOnClickListener(this);
llvideo2.setOnClickListener(this);
llVideo3.setOnClickListener(this);
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
backButton();
}
});
}

@Override
protected void onResume() {
super.onResume();



}

@Override
public void onBackPressed() {

if (youTubeView.getVisibility() == View.VISIBLE)
{
rvMainl.setVisibility(View.VISIBLE);

youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.setVisibility(View.GONE);
}
else
{
backButton();
}


}


private void backButton() {
Intent i = new Intent(Youtube.this, MainActivity.class);
startActivity(i);
finish();
overridePendingTransition(R.anim.left_to_right_back, R.anim.right_to_left_back);
}


@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.frag_youtube_btn_one:
youTubeView.destroyDrawingCache();
youTubeView.destroyDrawingCache();

init("DHFfXNES8gw");
break;
case R.id.frag_youtube_btn_two:
init("RLcsVqx4IsU");
break;
case R.id.frag_youtube_btn_three:
init("8Fx97V_Z3YY");
break;
case R.id.frag_youtube_btn_four:
init("940qp3BdR6o");
break;

}
}

private void init(final String VIdeoId) {
rvMainl.setVisibility(View.GONE);
youTubeView.setVisibility(View.VISIBLE);



if (youTubePlayerMain != null)
{
try{
youTubePlayerMain.pause();
youTubePlayerMain.cueVideo(VIdeoId);
}
catch(Exception e){
e.printStackTrace();
}


}else {

youTubeView.initialize(Config.YOUTUBE_API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
youTubePlayerMain = youTubePlayer;
if (!b) {
try
{ youTubePlayer.cueVideo(VIdeoId); // Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo

}catch (Exception e)
{
Toast.makeText(getApplicationContext(),"Somrthing Went Wrong with this video ",Toast.LENGTH_SHORT).show();

}

}

}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
if (youTubeInitializationResult.isUserRecoverableError()) {
youTubeInitializationResult.getErrorDialog(Youtube.this, RECOVERY_REQUEST).show();
} else {

Toast.makeText(Youtube.this, youTubeInitializationResult.toString(), Toast.LENGTH_LONG).show();
}
}
});
}

}
}

fragment_youtube_player.xml

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/youtube_iv_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_back" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:text="Everest Videos"
android:textColor="@android:color/white"
android:textSize="20dp" />

</android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">


<RelativeLayout
android:id="@+id/rvMain"
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">


<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/border"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout
android:padding="10dp"
android:id="@+id/frag_youtube_btn_one"

android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_youtube" />


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:gravity="center_horizontal"
android:text="@string/str_video_one" />
</LinearLayout>



</android.support.v7.widget.CardView>


<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="10dp"
android:layout_weight="1"

android:background="@drawable/border"
android:orientation="vertical"
android:padding="10dp">


<LinearLayout
android:id="@+id/frag_youtube_btn_two"

android:padding="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_youtube" />


<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="@string/str_video_two" />

</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">


<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/border"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout
android:id="@+id/frag_youtube_btn_three"

android:padding="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_youtube" />


<TextView
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="@string/str_video_three" />
</LinearLayout>




</android.support.v7.widget.CardView>


<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/border"
android:orientation="vertical"
android:padding="10dp">


<LinearLayout
android:id="@+id/frag_youtube_btn_four"
android:padding="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="120dp">

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_youtube" />


<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="EKOMILK SCAN." />
</LinearLayout>




</android.support.v7.widget.CardView>
</LinearLayout>

</LinearLayout>
</RelativeLayout>

<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>

关于android - youtubeplayer在启动新视频时立即停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45999406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com