使用两个VideoView时出现播放错误

时间:2011-10-24 22:06:19

标签: android surfaceview android-videoview android-mediaplayer

同时开始在两个VideoView s中播放两个HTTP流时,它可以正常工作。当我试图阻止一个并启动另一个时,它会抛出一个错误。使用的代码:

 mVideoView1.setVideoPath(videoPath);
 mVideoView2.setVideoPath(videoPath);
 mVideoView1.start();       
 Button btn = (Button) findViewById(R.id.button);
     btn.setOnClickListener(new OnClickListener() {
        @Override
               public void onClick(View v) {
        mVideoView1.stopPlayback();
        mVideoView2.start();
        }
    });

任何想法导致此错误的原因是什么?当我使用MediaPlayerSurfaceView(在后台准备第二个MediaPlayer,第一个release(),第二个start()时,我会得到相同的行为

1 个答案:

答案 0 :(得分:0)

您可以使用以下适合我的代码,

package com.materialexample;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.VideoView;

import in.co.bhadreshtech.materialexample.R;

public class VideoViews extends AppCompatActivity {
    ProgressBar progressBar = null;
    VideoView videoView1 = null;
    VideoView video_views2 = null;
    String videoUrl = "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";

    Context context = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_views);
        context = null;
        videoView1 = (VideoView) findViewById(R.id.video_view1);
        video_views2 = (VideoView) findViewById(R.id.video_views2);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        Uri videoUri = Uri.parse(videoUrl);
        videoView1.setVideoURI(videoUri);
        video_views2.setVideoURI(videoUri);
        videoView1.start();
        video_views2.start();
        progressBar.setVisibility(View.VISIBLE);
        videoView1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.start();
                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int arg1,
                                                   int arg2) {
                        // TODO Auto-generated method stub
                        progressBar.setVisibility(View.GONE);
                        mp.start();
                    }
                });

            }
        });
        video_views2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.start();
                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int arg1,
                                                   int arg2) {
                        // TODO Auto-generated method stub
                        progressBar.setVisibility(View.GONE);
                        mp.start();
                    }
                });

            }
        });

    }
}

也不要错过在布局中添加拖动VideoView和一个ProgressBar