使用媒体播放器播放视频时 SurfaceView 为空白

发布于 2024-11-19 04:47:40 字数 468 浏览 2 评论 0原文

这是我的代码,但是当我启动媒体播放器时,它只发出声音,而表面没有显示任何内容。为什么?

我对此不知道。您有一些代码可以帮助我学习这个吗?

    videoV = (SurfaceView) findViewById(R.id.SurfaceView1);
    sh = videoV.getHolder();

    File path = Environment.getExternalStorageDirectory();
    File file = new File(path, "sample2.mp4");

    sh.addCallback(this);     
    mp = new MediaPlayer();
    mp.setDataSource(file.getAbsolutePath());
    mp.setDisplay(sh);
    mp.prepare();
    mp.start();

This is My code but when I start the mediaplayer it have only the sound come out and the surface have nothing shown . Why?

I have no idea on this. Do you have some code help me to learn with this.

    videoV = (SurfaceView) findViewById(R.id.SurfaceView1);
    sh = videoV.getHolder();

    File path = Environment.getExternalStorageDirectory();
    File file = new File(path, "sample2.mp4");

    sh.addCallback(this);     
    mp = new MediaPlayer();
    mp.setDataSource(file.getAbsolutePath());
    mp.setDisplay(sh);
    mp.prepare();
    mp.start();

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

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

发布评论

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

评论(4

汹涌人海 2024-11-26 04:47:41

尝试在

sh.addCallback(this);

sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

之后添加就我而言这很有帮助。

Try to add after

sh.addCallback(this);

this

sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

In my case it was helpful.

烟若柳尘 2024-11-26 04:47:41

您添加了准备好的监听器吗?我在该方法中实现了开始,因为它是指示视频何时准备好渲染的触发器。

sh.addCallback(this);     
mp = new MediaPlayer();
mp.setDataSource(file.getAbsolutePath());
mp.setDisplay(sh);
mp.setOnPreparedListener(this);
mp.prepare();

public void onPrepared(MediaPlayer arg0) {
    mp.start();
}

Have you added the on prepared listener? I implemented the start in that method since it is the trigger that indicates when the video is ready to be rendered.

sh.addCallback(this);     
mp = new MediaPlayer();
mp.setDataSource(file.getAbsolutePath());
mp.setDisplay(sh);
mp.setOnPreparedListener(this);
mp.prepare();

public void onPrepared(MediaPlayer arg0) {
    mp.start();
}
苦笑流年记忆 2024-11-26 04:47:41

试试这个代码。

resource 是您要播放的文件名,one.two 是包名称,您的路径可能类似于 android.resource://package_name/raw/file_name< /code>

VideoView video=(VideoView) findViewById(R.id.videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
//Uri uri = Uri.parse("android.resource://play.vedio/"+R.raw.dobeernotdrugs);
video.setKeepScreenOn(true);
video.setVideoPath("android.resource://one.two/raw/"+resource);
video.start();
video.requestFocus();

另请查看此教程

try this code.

resource is file name which you want to play and one.two is package name your path may as like android.resource://package_name/raw/file_name

VideoView video=(VideoView) findViewById(R.id.videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
//Uri uri = Uri.parse("android.resource://play.vedio/"+R.raw.dobeernotdrugs);
video.setKeepScreenOn(true);
video.setVideoPath("android.resource://one.two/raw/"+resource);
video.start();
video.requestFocus();

Also take a look at this tutorial

心头的小情儿 2024-11-26 04:47:41

这对于 Android 开发新手或任何会看到此内容的人来说可能会有所帮助。

就我而言,在 OnCreate 方法中使用此代码片段帮助我找出哪些设备可以使用 SurfaceView

    if (
            GLES20.glGetString(GLES20.GL_RENDERER) == null ||
                    GLES20.glGetString(GLES20.GL_VENDOR) == null ||
                    GLES20.glGetString(GLES20.GL_VERSION) == null ||
                    GLES20.glGetString(GLES20.GL_EXTENSIONS) == null ||
                    GLES10.glGetString(GLES10.GL_RENDERER) == null ||
                    GLES10.glGetString(GLES10.GL_VENDOR) == null ||
                    GLES10.glGetString(GLES10.GL_VERSION) == null ||
                    GLES10.glGetString(GLES10.GL_EXTENSIONS) == null) {
        // try to use SurfaceView
    } else {
        // try to use TextureView
    }

找出 SurfaceView之间的差异>TextureView 请参阅此链接

This could be beneficial for novice android developers or anyone who will see this.

In my case, using this snippet in OnCreate method helped me to find out which device can use SurfaceView

    if (
            GLES20.glGetString(GLES20.GL_RENDERER) == null ||
                    GLES20.glGetString(GLES20.GL_VENDOR) == null ||
                    GLES20.glGetString(GLES20.GL_VERSION) == null ||
                    GLES20.glGetString(GLES20.GL_EXTENSIONS) == null ||
                    GLES10.glGetString(GLES10.GL_RENDERER) == null ||
                    GLES10.glGetString(GLES10.GL_VENDOR) == null ||
                    GLES10.glGetString(GLES10.GL_VERSION) == null ||
                    GLES10.glGetString(GLES10.GL_EXTENSIONS) == null) {
        // try to use SurfaceView
    } else {
        // try to use TextureView
    }

To find out differences between SurfaceView and TextureView see this link.

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