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

发布于 2024-12-12 00:17:25 字数 644 浏览 0 评论 0原文

当同时在两个 VideoView 中开始播放两个 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 时,我会得到相同的行为(在后台准备第二个 MediaPlayerrelease()首先,然后 start() 第二个)

When starting playback of two HTTP streams in two VideoViews simultaneously, it works. When I try to stop one and start the other, it throws an error. Code used:

 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();
        }
    });

Any ideas what's causing this error? I get the same behaviour when I use MediaPlayer and SurfaceView (preparing second MediaPlayer in background, release() the first, then start() the second)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2024-12-19 00:17:25

您可以使用以下代码,它对我有用,

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

you can use the following code it work for me,

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();
                    }
                });

            }
        });

    }
}

also don miss to add tow VideoView and one ProgressBar in layout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文