视频加载时不显示ProgressDialog

时间:2013-12-09 10:52:46

标签: android android-videoview progressdialog

我有来自uri的VideoView流媒体视频。我想在加载数据时显示ProgressDialog。但它没有显示出来。只有黑色屏幕可见,比一段时间后开始播放视频。

为什么我的对话框没有显示?

private static ProgressDialog pDialog;
VideoView vid;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    setContentView(R.layout.activity_player);

    vid = (VideoView) findViewById(R.id.videoView1);
    pDialog = ProgressDialog.show(PlayerActivity.this, "", getString(R.string.please_wait_while_loading), true);

    Intent intent = getIntent();
    String url = intent.getStringExtra(MainActivity.STREAM_URL);
    StartPlay(url);
}

private void StartPlay(String url)
{
    MediaController mc = new MediaController(this, false);
    mc.setAnchorView(vid);
    mc.setMediaPlayer(vid);
    vid.setMediaController(mc);
    Uri video = Uri.parse(url);
    vid.setVideoURI(video);     
    vid.requestFocus();
    vid.setKeepScreenOn(true);
    vid.setOnPreparedListener(new OnPreparedListener()
    {

        @Override
        public void onPrepared(MediaPlayer mp)
        {
            if (pDialog.isShowing()) 
                pDialog.dismiss();
            vid.start();    
        }
    });
    vid.setOnErrorListener(new OnErrorListener()
    {

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra)
        {
            if (pDialog.isShowing()) 
                pDialog.dismiss();

            Intent data = PlayerActivity.this.getIntent();
            data.putExtra(EXTRA_MESSAGE_TITLE, getString(R.string.title_MediaPlayerError));
            data.putExtra(EXTRA_MESSAGE, getString(R.string.message_MediaPlayerError));
            setResult(RESULT_CODE_ERROR, data);
            PlayerActivity.this.finish();

            return false;
        }
    }); 
}

现在我知道发生了错误,并且对话被驳回了。这个错误意味着什么?我能解决吗?

     12-09 15:27:18.160: E/WindowManager(2277): Activity myapp.PlayerActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4189cc40 that was originally added here
12-09 15:27:18.160: E/WindowManager(2277): android.view.WindowLeaked: Activity myapp.PlayerActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4189cc40 that was originally added here
12-09 15:27:18.160: E/WindowManager(2277):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:438)
12-09 15:27:18.160: E/WindowManager(2277):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
12-09 15:27:18.160: E/WindowManager(2277):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
12-09 15:27:18.160: E/WindowManager(2277):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
12-09 15:27:18.160: E/WindowManager(2277):  at android.view.Window$LocalWindowManager.addView(Window.java:558)
12-09 15:27:18.160: E/WindowManager(2277):  at android.app.Dialog.show(Dialog.java:316)
12-09 15:27:18.160: E/WindowManager(2277):  at android.app.ProgressDialog.show(ProgressDialog.java:130)
12-09 15:27:18.160: E/WindowManager(2277):  at android.app.ProgressDialog.show(ProgressDialog.java:113)
12-09 15:27:18.160: E/WindowManager(2277):  at myapp.PlayerActivity.onCreate(PlayerActivity.java:44)
12-09 15:27:18.160: E/WindowManager(2277):  at android.app.Activity.performCreate(Activity.java:5084)
12-09 15:27:18.160: E/WindowManager(2277):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-09 15:27:18.160: E/WindowManager(2277):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
12-09 15:27:18.160: E/WindowManager(2277):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
12-09 15:27:18.160: E/WindowManager(2277):  at android.app.ActivityThread.access$600(ActivityThread.java:136)
12-09 15:27:18.160: E/WindowManager(2277):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
12-09 15:27:18.160: E/WindowManager(2277):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 15:27:18.160: E/WindowManager(2277):  at android.os.Looper.loop(Looper.java:137)
12-09 15:27:18.160: E/WindowManager(2277):  at android.app.ActivityThread.main(ActivityThread.java:4803)
12-09 15:27:18.160: E/WindowManager(2277):  at java.lang.reflect.Method.invokeNative(Native Method)
12-09 15:27:18.160: E/WindowManager(2277):  at java.lang.reflect.Method.invoke(Method.java:511)
12-09 15:27:18.160: E/WindowManager(2277):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
12-09 15:27:18.160: E/WindowManager(2277):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
12-09 15:27:18.160: E/WindowManager(2277):  at dalvik.system.NativeStart.main(Native Method)
12-09 15:27:18.330: E/MediaPlayer(2277): Uri is  http://192.168.105.34/hls/ilook.m3u8
12-09 15:27:18.330: D/MediaPlayer(2277): Couldn't open file on client side, trying server side
12-09 15:27:18.440: D/MediaPlayer(2277): start() mUri is http://192.168.105.34/hls/ilook.m3u8
12-09 15:27:18.460: D/MediaPlayer(2277): start() mUri is http://192.168.105.34/hls/ilook.m3u8
12-09 15:27:49.230: E/MediaPlayer(2277): Uri is  http://192.168.105.34/hls/ilook.m3u8
12-09 15:27:49.230: D/MediaPlayer(2277): Couldn't open file on client side, trying server side
12-09 15:27:49.420: D/MediaPlayer(2277): start() mUri is http://192.168.105.34/hls/ilook.m3u8
12-09 15:27:49.430: D/MediaPlayer(2277): start() mUri is http://192.168.105.34/hls/ilook.m3u8
12-09 15:27:49.680: E/MediaPlayer(2277): Uri is  http://192.168.105.34/hls/ilook.m3u8
12-09 15:27:49.680: D/MediaPlayer(2277): Couldn't open file on client side, trying server side
12-09 15:27:49.780: D/MediaPlayer(2277): start() mUri is http://192.168.105.34/hls/ilook.m3u8
12-09 15:27:49.790: D/MediaPlayer(2277): start() mUri is http://192.168.105.34/hls/ilook.m3u8
12-09 15:27:52.410: D/MediaPlayer(2277): start() mUri is http://192.168.105.34/hls/ilook.m3u8
如果在对话创建时使用getApplicationContext,则出现

错误:

12-09 16:43:08.330: E/AndroidRuntime(6352): FATAL EXCEPTION: main
12-09 16:43:08.330: E/AndroidRuntime(6352): java.lang.RuntimeException: Unable to start activity ComponentInfo{myapp/myapp.PlayerActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2080)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.app.ActivityThread.access$600(ActivityThread.java:136)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.os.Looper.loop(Looper.java:137)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.app.ActivityThread.main(ActivityThread.java:4803)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at java.lang.reflect.Method.invoke(Method.java:511)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at dalvik.system.NativeStart.main(Native Method)
12-09 16:43:08.330: E/AndroidRuntime(6352): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:657)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.app.Dialog.show(Dialog.java:316)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.app.ProgressDialog.show(ProgressDialog.java:130)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.app.ProgressDialog.show(ProgressDialog.java:113)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at myapp.PlayerActivity.onCreate(PlayerActivity.java:44)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.app.Activity.performCreate(Activity.java:5084)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-09 16:43:08.330: E/AndroidRuntime(6352):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
12-09 16:43:08.330: E/AndroidRuntime(6352):     ... 11 more

1 个答案:

答案 0 :(得分:1)

试试这个:

        public static void getVideoFromServer(String urlPath) {

    mProgressDialog.show();
    // Check whether internet/wifi connection available or not
    // if false show alert dialog
    if (!ConnectionChecker.selfLearnig(mContext)) {
        mProgressDialog.dismiss();
        showNoInternetAlert();

        return;
    } else {

        try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(mContext);
            mediacontroller.setAnchorView(mVideoview);
            // Get the URL from String VideoURL
            mVideo = Uri.parse(urlPath);
            mVideoview.setMediaController(mediacontroller);
            mVideoview.setVideoURI(mVideo);

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();

        }

        mVideoview.requestFocus();
        mVideoview.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                mProgressDialog.dismiss();
                mVideoview.start();

            }
        });

        mVideoview.setOnCompletionListener(new OnCompletionListener() {
            // Called when video is completed
            public void onCompletion(MediaPlayer mp) {

            }
        });
    }
}

希望这有帮助。