Android: YouTube Video in an AlertDialog



Android编程新手!我想知道是否有可能创建一种弹出视图(类似于警报对话框为例),在那里你可以播放YouTube视频!我试过了,但是失败了!

你能帮我吗?我知道如何在不同的活动上播放YouTube视频,但我不知道如何做同样的事情,但在一种警告对话框!

谢谢大家!

这是我的代码:

b1.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    //Intent i = new Intent(PaginaRisultati.this, YTActivity.class);
                    final Dialog dialog = new Dialog(PaginaRisultati.this);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.playerview_demo);
                    YouTubePlayerView youTubeView = (YouTubePlayerView) dialog.findViewById(R.id.youtube_view);
                    OnInitializedListener oil = new YouTubePlayer.OnInitializedListener() {
                        @Override
                        public void onInitializationSuccess(Provider provider, YouTubePlayer player,
                                boolean wasRestored) {
                             if (!wasRestored) {
                                  player.cueVideo("lHtJPFLgizw");
                                }
                        }
                        @Override
                        public void onInitializationFailure(Provider provider,
                                YouTubeInitializationResult errorReason) {
                            if (errorReason.isUserRecoverableError()) {
                                  errorReason.getErrorDialog(PaginaRisultati.this, RECOVERY_DIALOG_REQUEST).show();
                                } else {
                                  String errorMessage = String.format("ERROR!!", errorReason.toString());
                                  Toast.makeText(PaginaRisultati.this, errorMessage, Toast.LENGTH_LONG).show();
                                }
                        }
                    };
                    youTubeView.initialize("MY_APY_KEY", oil);
                    dialog.show();
                }
            });

XML对话框文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
  <com.google.android.youtube.player.YouTubePlayerView
      android:id="@+id/youtube_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
</LinearLayout>

这是LogCat中的错误:

06-11 16:57:10.225: E/AndroidRuntime(1872): FATAL EXCEPTION: main
06-11 16:57:10.225: E/AndroidRuntime(1872): Process: it.sii.android.jobaroundu, PID: 1872
06-11 16:57:10.225: E/AndroidRuntime(1872): android.view.InflateException: Binary XML file line #28: Error inflating class com.google.android.youtube.player.YouTubePlayerView
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.app.Dialog.setContentView(Dialog.java:477)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at it.sii.android.jobaroundu.PaginaRisultati$MyExpandableListAdapter$3.onClick(PaginaRisultati.java:287)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.view.View.performClick(View.java:4438)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.view.View$PerformClick.run(View.java:18422)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.os.Handler.handleCallback(Handler.java:733)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.os.Looper.loop(Looper.java:136)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at java.lang.reflect.Method.invoke(Method.java:515)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at dalvik.system.NativeStart.main(Native Method)
06-11 16:57:10.225: E/AndroidRuntime(1872): Caused by: java.lang.reflect.InvocationTargetException
06-11 16:57:10.225: E/AndroidRuntime(1872):     at java.lang.reflect.Constructor.constructNative(Native Method)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
06-11 16:57:10.225: E/AndroidRuntime(1872):     ... 19 more
06-11 16:57:10.225: E/AndroidRuntime(1872): Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity  which extends YouTubeBaseActivity as its context.
06-11 16:57:10.225: E/AndroidRuntime(1872):     at com.google.android.youtube.player.YouTubePlayerView.<init>(Unknown Source)
06-11 16:57:10.225: E/AndroidRuntime(1872):     at com.google.android.youtube.player.YouTubePlayerView.<init>(Unknown Source)
06-11 16:57:10.225: E/AndroidRuntime(1872):     ... 22 more

我让我的活动看起来像一个对话框。我不得不诉诸这种解决方案,因为我必须在片段中显示youtubeplayer,并且不允许在类扩展片段中嵌套标记。下面是代码:

public class YoutubeDialogActivity extends YouTubeBaseActivity implements
    YouTubePlayer.OnInitializedListener {
public static final String API_KEY = "your api kery from google";
public static  String VIDEO_ID ;
private YouTubePlayer youTubePlayer;
private YouTubePlayerFragment youTubePlayerFragment;
private static final int RQS_ErrorDialog = 1;
private MyPlayerStateChangeListener myPlayerStateChangeListener;
private MyPlaybackEventListener myPlaybackEventListener;
String log = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.youtube_player_dialog_activity);
    Bundle bundle = getIntent().getExtras();
    VIDEO_ID = bundle.getString("video_id");
    youTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager()
            .findFragmentById(R.id.youtubeplayerfragment);
    youTubePlayerFragment.initialize(API_KEY, this);
    myPlayerStateChangeListener = new MyPlayerStateChangeListener();
    myPlaybackEventListener = new MyPlaybackEventListener();
}
@Override
public void onInitializationFailure(Provider provider,
        YouTubeInitializationResult result) {
    if (result.isUserRecoverableError()) {
        result.getErrorDialog(this, RQS_ErrorDialog).show();
    }
}
@Override
public void onInitializationSuccess(Provider provider,
        YouTubePlayer player, boolean wasRestored) {
    PlayerStyle style = PlayerStyle.MINIMAL;
    player.setPlayerStyle(style);
    youTubePlayer = player;
    youTubePlayer.setPlayerStateChangeListener(myPlayerStateChangeListener);
    youTubePlayer.setPlaybackEventListener(myPlaybackEventListener);
    if (!wasRestored) {
        player.cueVideo(VIDEO_ID);
    }
}
private final class MyPlayerStateChangeListener implements
        PlayerStateChangeListener {
    @Override
    public void onAdStarted() {
    }
    @Override
    public void onError(
            com.google.android.youtube.player.YouTubePlayer.ErrorReason arg0) {
    }
    @Override
    public void onLoaded(String arg0) {
    }
    @Override
    public void onLoading() {
    }
    @Override
    public void onVideoEnded() {
        finish();
    }
    @Override
    public void onVideoStarted() {
    }
}
}

这是manifest文件中的主题:

<activity
        android:name="com.proshore.rwd.activity.YoutubeDialogActivity"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Dialog" >
    </activity>

根据崩溃的异常是不言自明的:

一个YouTubePlayerView只能通过一个扩展了YouTubeBaseActivity作为其上下文的Activity来创建。

这也是我选择让我的活动成为一个对话框的原因之一

将您的活动主题更改为theme。

例子
<activity
        android:name=".Activities.YoutubeVideoActivity"
        android:theme="@android:style/Theme.Dialog"/>

注意:你的活动应该扩展YouTubeBaseActivity

相关内容

  • 没有找到相关文章

最新更新