Android:MediaPlayer 无缝或无缝视频播放 [英] Android: MediaPlayer gapless or seamless Video Playing

查看:49
本文介绍了Android:MediaPlayer 无缝或无缝视频播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过实现 OnCompletionListener 将数据源设置为不同的文件,我可以连续播放视频.没有问题.我调用 reset()prepare() 就好了.

I can play the videos fine back to back by implementing the OnCompletionListener to set the data source to a different file. No problems there. I call reset() and prepare() just fine.

我一直无法弄清楚的是,如何摆脱数据源更改和新视频启动之间的 1-2 秒间隙屏幕闪烁.差距显示黑屏,我还没有找到任何绕过它的方法.

What I haven't been able to figure out, is how to get rid of the 1-2 second gap screen flicker between the data source change and the new video starting. The gap shows a black screen, and I haven't found any way to get around it.

我尝试将父视图的背景设置为图像,但它设法绕过了它.即使 SurfaceView 是透明的(默认情况下是这样),我也尝试同时播放多个视频文件,并在一个结束时切换媒体播放器的显示,另一个应该是开始.

I've tried setting the background of the parent view to an image, but it manages to bypass that. Even if the SurfaceView is transparent (which it is by default.) I've also tried to have the multiple video files played at the same time, and switching mediaplayer's display when one ends and the other is supposed to start.

我尝试的最后一件事是在视频准备"时临时显示的背景中的第二个视图,并在视频准备好开始时将其删除.这也不是很无缝.

The last thing I tried, was to have a second view in the background that I show temporarily while the video is "preparing" and removing it when the video is ready to start. That also wasn't very seamless.

有什么办法可以消除这个差距.循环播放视频效果非常好,并且完全符合我的要求,不同之处在于它浏览的是同一个视频,而不是播放我选择的不同视频.

Is there any way to get rid of that gap. Running a video in a loop works wonderfully and does exactly what I want with the exception that it's looking through the same video instead of playing a different one that I pick.

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="@drawable/background"
    android:layout_height="fill_parent">
    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center">
    </SurfaceView>
</FrameLayout>

Player.java

public class Player extends Activity implements
        OnCompletionListener, MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
        private MediaPlayer player;
    private SurfaceView surface;
    private SurfaceHolder holder;

    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.main);
        surface = (SurfaceView)findViewById(R.id.surface);
        holder = surface.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);    
    }

    public void onCompletion(MediaPlayer arg0) {
        File clip = new File(Environment.getExternalStorageDirectory(),"file2.mp4");
        playVideo(clip.getAbsolutePath());
    }
    public void onPrepared(MediaPlayer mediaplayer) {
        holder.setFixedSize(player.getVideoWidth(), player.getVideoHeight());
        player.start();
    }

    private void playVideo(String url) {
        try {
            File clip = new File(Environment.getExternalStorageDirectory(),"file1.mp4");

            if (player == null) {
                player = new MediaPlayer();
                player.setScreenOnWhilePlaying(true);
            }
            else {
                player.stop();
                player.reset();
            }
            player.setDataSource(url);
            player.setDisplay(holder);
            player.setOnPreparedListener(this);
            player.prepare();
            player.setOnCompletionListener(this);
        }
        catch (Throwable t) {
            Log.e("ERROR", "Exception Error", t);
        }
    }

推荐答案

我也有同样的问题,如以下链接所述

I too have the same problem as outlined by below link

VideoView 在更改视频时消失一秒钟

但如果您尝试使用 Android 4.0+ (ICS),则不会出现此问题.我开始将 VideoView.java 和 MediaPlayer.java 从 4.0 移植到我的应用程序,但这似乎很复杂,直到现在还没有运气.基本上它似乎是旧版本的本机代码中的一个错误.

But this issue wont occur if you try using Android 4.0+ (ICS). I started to port VideoView.java and MediaPlayer.java from 4.0 to my app , but thats seems complex and no luck till now. Basically it seems a bug in the native code of the older versions.

这篇关于Android:MediaPlayer 无缝或无缝视频播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆