我正在执行此活动,在YouTubePlayerView上将播放不同的视频,并分别单击相应的按钮。如何正确执行此操作?抱歉,代码杂乱无章,我是android的新手。
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
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 HazardsActivity extends YouTubeBaseActivity {
private ImageButton gs_btn;
private ImageButton gr_btn;
private ImageButton ls_btn;
private ImageButton liq_btn;
private ImageButton ts_btn;
private YouTubePlayerView YTplayer;
YouTubePlayer.OnInitializedListener mOninitilizedListner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hazards);
YTplayer = (YouTubePlayerView) findViewById(R.id.youTubePlayerView);
gs_btn = (ImageButton) findViewById(R.id.gs_button);
gr_btn = (ImageButton) findViewById(R.id.gr_button);
ls_btn = (ImageButton) findViewById(R.id.ls_button);
liq_btn = (ImageButton) findViewById(R.id.liq_button);
ts_btn = (ImageButton) findViewById(R.id.ts_button);
gs_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gs_btn.setBackgroundResource(R.mipmap.gs_active);
gr_btn.setBackgroundResource(R.mipmap.gr_inactive);
ls_btn.setBackgroundResource(R.mipmap.ls_inactive);
liq_btn.setBackgroundResource(R.mipmap.liq_inactive);
ts_btn.setBackgroundResource(R.mipmap.ts_inactive);
YTplayer.initialize(YoutubeConfig.getApiKey(), mOninitilizedListner);
}
});
gr_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gs_btn.setBackgroundResource(R.mipmap.gs_inactive);
gr_btn.setBackgroundResource(R.mipmap.gr_active);
ls_btn.setBackgroundResource(R.mipmap.ls_inactive);
liq_btn.setBackgroundResource(R.mipmap.liq_inactive);
ts_btn.setBackgroundResource(R.mipmap.ts_inactive);
}
});
ls_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gs_btn.setBackgroundResource(R.mipmap.gs_inactive);
gr_btn.setBackgroundResource(R.mipmap.gr_inactive);
ls_btn.setBackgroundResource(R.mipmap.ls_active);
liq_btn.setBackgroundResource(R.mipmap.liq_inactive);
ts_btn.setBackgroundResource(R.mipmap.ts_inactive);
}
});
liq_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gs_btn.setBackgroundResource(R.mipmap.gs_inactive);
gr_btn.setBackgroundResource(R.mipmap.gr_inactive);
ls_btn.setBackgroundResource(R.mipmap.ls_inactive);
liq_btn.setBackgroundResource(R.mipmap.liq_active);
ts_btn.setBackgroundResource(R.mipmap.ts_inactive);
}
});
ts_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gs_btn.setBackgroundResource(R.mipmap.gs_inactive);
gr_btn.setBackgroundResource(R.mipmap.gr_inactive);
ls_btn.setBackgroundResource(R.mipmap.ls_inactive);
liq_btn.setBackgroundResource(R.mipmap.liq_inactive);
ts_btn.setBackgroundResource(R.mipmap.ts_active);
}
});
mOninitilizedListner = new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo("q5nf6Wl5TVI");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
};
}
}
我的问题是我不知道如何在不同的按钮单击上加载特定视频。我只能在单个按钮上播放单个视频,而且我想加载并在开始此活动时开始播放视频。预先感谢您的回答!