2

这是我的 videoview 代码

try {

            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    SingleItemView.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(Uri.parse(video));
            videoview.requestFocus();
            videoview.setKeepScreenOn(true);

            videoview
                    .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        // Close the progress bar and play the video
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mProgressDialog.dismiss();
                            videoview.start();
                        }
                    });
        } catch (Exception e) {
            mProgressDialog.dismiss();
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

但如果视频流不工作,对话框仍然存在。

如果视频不工作,我想把对话放回主要活动..

我怎样才能做到这一点?

我尝试放置对话框但仍然无法正常工作我不知道为什么..

提前致谢 :)

编辑:我也尝试这样但仍然无法正常工作

try {
                    // Start the MediaController
                    MediaController mediacontroller = new MediaController(
                            SingleTrackActivity.this);
                    mediacontroller.setAnchorView(videoview);
                    // Get the URL from String VideoURL
                    videoview.setMediaController(mediacontroller);
                    videoview.setVideoURI(Uri.parse(duration));
                    videoview.requestFocus();
                    videoview.setKeepScreenOn(true);

                    videoview
                            .setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                // Close the progress bar and play the video
                                public void onPrepared(
                                        MediaPlayer mediaPlayer) {
                                    pDialog.dismiss();
                                    videoview.start();
                                }
                            });
                } catch (Exception e) {
                    AlertDialog alertDialog = new AlertDialog.Builder(
                            SingleTrackActivity.this).create();

                    // Setting Dialog Title
                    alertDialog.setTitle("Alert Dialog");

                    // Setting Dialog Message
                    alertDialog.setMessage("Video Play Error :"
                            + e.toString());

                    pDialog.dismiss();

                    System.out.println("Video Play Error :" + e.toString());
                    finish();
                }
4

1 回答 1

2

媒体播放器的工作与您的代码异步完成。如果媒体设置失败,则在代码完成您的方法后会发生失败。同时,onPreparedListener由于媒体没有进入PREPARED状态,所以没有调用。所以尝试添加一个OnErrorListener

videoview.setOnErrorListener(new MediaPlayer.OnErrorListener(){
    public boolean onError(MediaPlayer mp, int what, int extra) {
        // inform the user something went wrong
        mProgressDialog.dismiss();
        return true;
    }
});

如果这不起作用,请检查您为媒体播放器/视频视图获得的回调。

于 2013-10-02T07:22:30.587 回答