如何在android的弹出窗口中播放视频文件?

时间:2014-11-11 09:58:09

标签: android popup media-player android-videoview

我想在Android设备的弹出窗口中播放任何视频文件。此外,我必须显示片段中的视频,而不是来自活动。

使用mVideoDialog = new Dialog(this);对我不起作用。它抛出了这个错误:

  

android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null无效;你的活动在运行吗?

我尝试通过将getActivity()传递给Dialog来修复它,但应用程序崩溃时出现了同样的错误。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您必须转到自定义对话框,添加此代码 -

    Dialog mVideoDialog ;
VideoView mVideoFullScreen;
 MediaController controller;

创建方法 -

mVideoDialog = new Dialog(this);
    mVideoDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    mVideoDialog.setContentView(R.layout.dialog);
    mVideoDialog.setOnKeyListener(this);
    mVideoFullScreen = (VideoView) mVideoDialog.findViewById(R.id.videoview1);
    controller = new MediaController(this);
    HelloWebViewClient webViewClient = new HelloWebViewClient();
    webView.setWebViewClient(webViewClient);
     }

 private class HelloWebViewClient extends WebViewClient
{ 
 @Override public boolean shouldOverrideUrlLoading(WebView view, String url)
 {

     if (url.contentEquals("file:///android_asset/01")) {
         showVideo(01); //check user click url from webview and pass int
     }
     else if (url.contentEquals("file:///android_asset/02"))
     {
        showVideo(02);//pass int to determine which video to play
     }
     else
     {
     System.out.println("DDD URL: "+url.toString()); view.loadUrl(url);  
     }
     return true;       
     }

显示播放视频文件的视频方法 -

public void showVideo(int i) {
    // TODO Auto-generated method stub
    if(i==01)
    {
     mVideoDialog.show();
     mVideoFullScreen.setVideoPath("file:///sdcard/video file name.m4v");
     controller.setMediaPlayer(mVideoFullScreen);
     mVideoFullScreen.setMediaController(controller);
     mVideoFullScreen.requestFocus();
     mVideoFullScreen.start();
    }else
    {  } }

干杯:)