Android 使用Youtube播放器api在后台播放Youtube视频
我已经使用Android 使用Youtube播放器api在后台播放Youtube视频,android,background,youtube-api,Android,Background,Youtube Api,我已经使用youtube播放器api成功播放了youtube视频。但我需要在后台运行它的后退按钮按下。我已经谷歌了很多,但没有找到什么请帮助我实现这一点。提前感谢 这是我的密码- public class FullscreenDemoActivity extends YouTubeFailureRecoveryActivity implements View.OnClickListener, YouTubePlayer.OnFullscreenListener
youtube播放器api
成功播放了youtube视频。但我需要在后台运行它的后退按钮按下。我已经谷歌了很多,但没有找到什么请帮助我实现这一点。提前感谢
这是我的密码-
public class FullscreenDemoActivity extends YouTubeFailureRecoveryActivity
implements View.OnClickListener,
YouTubePlayer.OnFullscreenListener {
// private MyPlaybackEventListener myPlaybackEventListener;
private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9 ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
: ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
private LinearLayout baseLayout;
private YouTubePlayerView playerView;
public YouTubePlayer player;
private Button fullscreenButton;
private CompoundButton checkbox;
private View otherViews;
private boolean fullscreen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen_demo);
baseLayout = (LinearLayout) findViewById(R.id.layout);
playerView = (YouTubePlayerView) findViewById(R.id.player);
fullscreenButton = (Button) findViewById(R.id.fullscreen_button);
checkbox = (CompoundButton) findViewById(R.id.landscape_fullscreen_checkbox);
otherViews = findViewById(R.id.other_views);
playerView.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
this.player = player;
setControlsEnabled();
// Specify that we want to handle fullscreen behavior ourselves.
player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
player.setOnFullscreenListener(this);
if (!wasRestored) {
player.cueVideo("avP5d16wEp0");
}
int controlFlags = player.getFullscreenControlFlags();
setRequestedOrientation(PORTRAIT_ORIENTATION);
controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
player.setFullscreenControlFlags(controlFlags);
player.play();
}
@Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return playerView;
}
@Override
public void onClick(View v) {
player.setFullscreen(!fullscreen);
}
private void doLayout() {
LinearLayout.LayoutParams playerParams = (LinearLayout.LayoutParams) playerView
.getLayoutParams();
if (fullscreen) {
// When in fullscreen, the visibility of all other views than the
// player should be set to
// GONE and the player should be laid out across the whole screen.
playerParams.width = LayoutParams.MATCH_PARENT;
playerParams.height = LayoutParams.MATCH_PARENT;
otherViews.setVisibility(View.GONE);
} else {
otherViews.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams otherViewsParams = otherViews
.getLayoutParams();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
playerParams.width = otherViewsParams.width = 0;
playerParams.height = WRAP_CONTENT;
otherViewsParams.height = MATCH_PARENT;
playerParams.weight = 1;
baseLayout.setOrientation(LinearLayout.HORIZONTAL);
} else {
playerParams.width = otherViewsParams.width = MATCH_PARENT;
playerParams.height = WRAP_CONTENT;
playerParams.weight = 0;
otherViewsParams.height = 0;
baseLayout.setOrientation(LinearLayout.VERTICAL);
}
setControlsEnabled();
}
}
private void setControlsEnabled() {
checkbox.setEnabled(player != null
&& getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
fullscreenButton.setEnabled(player != null);
}
@Override
public void onFullscreen(boolean isFullscreen) {
fullscreen = isFullscreen;
doLayout();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// System.out.println(getScreenOrientation());
doLayout();
}
private final class MyPlaybackEventListener implements PlaybackEventListener {
private void updateLog(String prompt){
System.out.println(prompt);
};
@Override
public void onBuffering(boolean arg0) {
updateLog("onBuffering(): " + String.valueOf(arg0));
}
@Override
public void onPaused() {
updateLog("onPaused()");
}
@Override
public void onPlaying() {
updateLog("onPlaying()");
}
@Override
public void onSeekTo(int arg0) {
updateLog("onSeekTo(): " + String.valueOf(arg0));
}
@Override
public void onStopped() {
player.loadVideo("avP5d16wEp0");
player.cueVideo("avP5d16wEp0");
player.play();
updateLog("onStopped()");
}
}
}
公共类FullscreenDemoActivity扩展了YouTubeFailureRecoveryActivity
实现View.OnClickListener,
youtubeplyer.OnFullscreenListener{
//私有MyPlaybackEventListener MyPlaybackEventListener;
私有静态最终整数纵向\u方向=Build.VERSION.SDK\u int<9?ActivityInfo.SCREEN\u方向\u纵向
:ActivityInfo.SCREEN\u方向\u传感器\u纵向;
私人线路布局基地布局;
私人YouTubePlayerView playerView;
公共青年篮球运动员;
私人按钮全屏按钮;
私有复合按钮复选框;
私人观点或其他观点;
私有布尔全屏;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.全屏演示);
baseLayout=(LinearLayout)findViewById(R.id.layout);
playerView=(YouTubePlayerView)findViewById(R.id.player);
全屏按钮=(按钮)findViewById(R.id.fullscreen_按钮);
复选框=(复合按钮)findViewById(R.id.scape\u全屏\u复选框);
其他视图=findViewById(R.id.其他视图);
初始化(DeveloperKey.DEVELOPER\u键,this);
}
@凌驾
public void onInitializationSuccess(YouTubePlayer.Provider,
YouTubeplyer播放器,布尔值已恢复){
this.player=player;
setControlsEnabled();
//指定我们希望自己处理全屏行为。
player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN\u FLAG\u自定义\u布局);
player.setOnFullscreenListener(这个);
如果(!已还原){
player.cueVideo(“avP5d16wEp0”);
}
int controlFlags=player.getFullscreenControlFlags();
设置请求的方向(纵向);
controlFlags |=youtubeplyer.FULLSCREEN_FLAG_始终_FULLSCREEN_在_景观中;
player.setFullscreenControlFlags(controlFlags);
player.play();
}
@凌驾
受保护的YouTubePlayer.Provider getYouTubePlayerProvider(){
返回playerView;
}
@凌驾
公共void onClick(视图v){
player.setFullscreen(!fullscreen);
}
私人空房{
LinearLayout.LayoutParams播放参数=(LinearLayout.LayoutParams)播放视图
.getLayoutParams();
如果(全屏){
//在全屏显示时,除
//玩家应该设置为
//玩家应该在整个屏幕上进行布局。
playerParams.width=LayoutParams.MATCH_父项;
playerParams.height=布局参数.MATCH_父项;
otherview.setVisibility(View.GONE);
}否则{
otherview.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams otherViewsParams=其他视图
.getLayoutParams();
if(getResources().getConfiguration().orientation==Configuration.orientation\u){
playerParams.width=otherViewsParams.width=0;
playerParams.height=包裹内容;
otherViewsParams.height=匹配父项;
playerParams.weight=1;
基本布局。设置方向(线性布局。水平);
}否则{
playerParams.width=其他视图sparams.width=匹配父项;
playerParams.height=包裹内容;
playerParams.weight=0;
其他视图sparams.height=0;
基本布局。设置方向(线性布局。垂直);
}
setControlsEnabled();
}
}
私有void setControlsEnabled(){
checkbox.setEnabled(player!=null
&&getResources().getConfiguration().orientation==Configuration.orientation\u纵向);
fullscreenButton.setEnabled(播放器!=null);
}
@凌驾
全屏上的公共无效(布尔值为全屏){
全屏=全屏;
doLayout();
}
@凌驾
公共无效OnConfiguration已更改(配置newConfig){
super.onConfigurationChanged(newConfig);
//System.out.println(getScreenOrientation());
doLayout();
}
私有最终类MyPlaybackEventListener实现PlaybackEventListener{
私有void updateLog(字符串提示){
System.out.println(提示);
};
@凌驾
公共void onBuffering(布尔arg0){
updateLog(“onBuffering():”+String.valueOf(arg0));
}
@凌驾
已暂停的公共void(){
updateLog(“onPaused()”);
}
@凌驾
公众娱乐{
updateLog(“onPlaying()”);
}
@凌驾
seekto上的公共无效(int arg0){
updateLog(“onSeekTo():”+String.valueOf(arg0));
}
@凌驾
公共空间{
loadVideo(“avP5d16wEp0”);
player.cueVideo(“avP5d16wEp0”);
player.play();
updateLog(“onStopped()”);
}
}
}
我搜索了这个主题,发现没有官方的方式,也不会根据谷歌代码
他们建议你创建一个玩家
隐藏玩家
使播放器静音
使用videoId和可选偏移量调用seekTo()
等待状态更改为“播放”
按暂停
调用seekTo(原始偏移,false)
将播放器取消静音
向玩家展示。(或其他)
它可能不会做你想做的事