其他分享
首页 > 其他分享> > 如何在播放另一个VideoView B时预播视频视频 – Android

如何在播放另一个VideoView B时预播视频视频 – Android

作者:互联网

我有兴趣在VideoView B播放时能够“预缓冲”VideoView A.这可能吗?

>我有两个VideoView实例.
>在给定时间只会显示一个VideoView,占据手机的整个屏幕.
>目的是在从VideoView A和VideoView B切换时减少“黑屏”.
>该应用程序一个接一个地播放视频列表.

在2015年1月19日添加了以下代码

下面的代码抓取每个玩家的MediaPlayer的引用.当MediaPlayer A开始渲染时,MediaPlayer B将调用stop(),release(),setDataSource(url),prepareAsync().当MediaPlayer A完成时,Media Player B将调用start().

public class PrebufferingActivity extends Activity {
    private VideoView player1;
    private VideoView player2;
    private MediaPlayer mediaPlayer1;
    private MediaPlayer mediaPlayer2;
    public static final String URL_1 = "sample1.mp4";
    public static final String URL_2 = "sample2.mp4";

    public boolean FIRST_TIME = true;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prebuffering);

        player1 = (VideoView) findViewById(R.id.videoPlayer1);
        player2 = (VideoView) findViewById(R.id.videoPlayer2);

        player1.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return false;
            }

        });

        player2.setOnTouchListener(new OnTouchListener(){

             @Override
             public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return false;
             }
        });
        player1.setOnCompletionListener(new OnCompletionListener(){
            @Override
            public void onCompletion(MediaPlayer mp)            {
                mediaPlayer2.start();

                // Toggle visibility. Player1 is completed. Hide Player1. Show Player2.
                player2.setVisibility(View.VISIBLE);
                player1.setVisibility(View.INVISIBLE);
            }
        });

        player1.setOnInfoListener(new OnInfoListener(){
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) 
            {
                if(what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
                {
                    mediaPlayer2.stop();
                    mediaPlayer2.release();

                    try {
                        mediaPlayer2.setDataSource(URL_2);
                        mediaPlayer2.prepareAsync();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } 
                }
                return false;
            }
        });

        player1.setOnPreparedListener(new OnPreparedListener(){
            @Override
            public void onPrepared(MediaPlayer mp)          {
                mediaPlayer1 = mp;

                if(FIRST_TIME == true) {
                    mediaPlayer1.start();
                    player1.requestFocus();
                    FIRST_TIME = false;
                }
            }
        });

        player2.setOnCompletionListener(new OnCompletionListener(){

            @Override
            public void onCompletion(MediaPlayer mp)            {
                mediaPlayer1.start();
                player1.requestFocus();

                // Toggle visibility. Player2 is completed. Hide Player2. Show Player1.
                player1.setVisibility(View.VISIBLE);
                player2.setVisibility(View.INVISIBLE);
            }
        });

        player2.setOnInfoListener(new OnInfoListener(){
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) 
            {
                if(what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
                {
                    // Prepare Player 2
                    mediaPlayer1.stop();
                    mediaPlayer1.release();
                    try {
                        mediaPlayer1.setDataSource(PrebufferingActivity.URL_1);
                        mediaPlayer1.prepareAsync();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } 
                }

                return false;
            }
        });

        player2.setOnPreparedListener(new OnPreparedListener(){
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer2 = mp;
            }
        });

        // Player 1
        player1.setMediaController(new MediaController(this));
        player1.setVideoURI(Uri.parse(URL_1));

        // Player 2
        player2.setMediaController(new MediaController(this));
        player2.setVideoURI(Uri.parse(URL_2));
    }

解决方法:

假设您使用MediaPlayer在VideoView中处理视频的播放,您可以使用prepareAsync()方法来实现这一点. prepareAsync()方法基本上执行与prepare()相同的功能,该功能用于缓冲和准备视频,但它可以在后台异步完成.

编辑

我做了类似的事情,但实际上我使用的是SurfaceView.要处理另一个流,只需创建另一个MediaPlayer实例.以下示例将创建两个MediaPlayer实例并缓冲第一个视频.在准备好第一个视频后,第二个视频将被异步缓冲.然后将第一个MediaPlayer绑定到SurfaceView,并启动第一个视频.当第一个视频播放完毕后,将释放相应的MediaPlayer实例,附加辅助实例,并自动启动第二个视频.以下示例代码已经过测试并且工作正常:

public class StreamingActivity extends Activity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener
{

    MediaPlayer     mediaPlayerPrimary;
    MediaPlayer     mediaPlayerSecondary;
    SurfaceHolder   surfaceHolder;
    SurfaceView     surfaceView;
    String          videoSourcePrimary = "videoSourcePrimaryURL";
    String          videoSourceSecondary = "videoSourceSecondaryURL";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.streaming_activity_layout);
        /** Create SurfaceView */
        surfaceView = (SurfaceView) findViewById(R.id.surface_view);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0)
    {
        try
        {
            /** Create MediaPlayer(s) */
            mediaPlayerPrimary = new MediaPlayer();
            mediaPlayerSecondary = new MediaPlayer();
            /** Attach primary MediaPlayer to SurfaceView */
            mediaPlayerPrimary.setDisplay(surfaceHolder);
            /** Set sources */
            mediaPlayerPrimary.setDataSource(videoSourcePrimary);
            mediaPlayerSecondary.setDataSource(videoSourceSecondary);
            /** Prepare MediaPlayer(s) */
            mediaPlayerPrimary.prepare();
            mediaPlayerSecondary.prepareAsync();
            /** Set listeners */
            mediaPlayerPrimary.setOnPreparedListener(this);
            mediaPlayerPrimary.setOnCompletionListener(this);
            mediaPlayerSecondary.setOnPreparedListener(this);
            mediaPlayerSecondary.setOnCompletionListener(this);
            /** Set audio stream type */
            mediaPlayerPrimary.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayerSecondary.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
    {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0)
    {
    }

    @Override
    public void onPrepared(MediaPlayer mp)
    {
        if (mp == mediaPlayerPrimary)
        {
            /** First video ready, start playback */
            mediaPlayerPrimary.start();
        }
        else if (mp == mediaPlayerSecondary)
        {
            /** Second video is ready */
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp)
    {
        /** First video is completed, start second video */
        mediaPlayerPrimary.release();
        mediaPlayerSecondary.setDisplay(surfaceHolder);
        mediaPlayerSecondary.start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.streaming_activity_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

标签:android,videoview,buffering,android-videoview
来源: https://codeday.me/bug/20190628/1318860.html