在 Android 上播放 mp4 文件时不显示视频

发布于 2024-11-19 06:36:38 字数 1621 浏览 3 评论 0原文

我尝试渲染添加到 android 资源中 res/raw 的 mp4 文件,如下所示:

public class Main extends RoboActivity
{
    @InjectView(R.id.introVideo)
    private VideoView introVideo;

    private MediaPlayer player;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);

        player = MediaPlayer.create(this, R.raw.intro_video2);
        SurfaceHolder holder = introVideo.getHolder();
        player.setDisplay(holder);
        player.start();

        player.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mp)
            {
                startActivity(new Intent(Main.this, Story.class));
                releasePlayer();
            }
        });
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        releasePlayer();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        releasePlayer();
    }

    private void releasePlayer()
    {
        if (player != null)
        {
            player.release();
        }
    }
}

但我体验到的只是视频的声音,屏幕在我的 Samsung GalaxyTab 上保持空白。源文件是 mp4 文件(H.264 AVC、960x640、30fps),可以使用 Quicktime 和 VLC 完美播放。

我尝试使用 Handbrake 缩小原始视频并调整其大小,降至 480x320 和 25fps,我尝试了 Handbrake 的多种设置,但一切都没有成功。

我的代码是否有任何明显的错误,或者是视频格式还是其他什么问题 - 我做错了什么?

提前致谢, 托马斯.

I try to render an mp4 file I added to my android ressources in res/raw like so:

public class Main extends RoboActivity
{
    @InjectView(R.id.introVideo)
    private VideoView introVideo;

    private MediaPlayer player;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);

        player = MediaPlayer.create(this, R.raw.intro_video2);
        SurfaceHolder holder = introVideo.getHolder();
        player.setDisplay(holder);
        player.start();

        player.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mp)
            {
                startActivity(new Intent(Main.this, Story.class));
                releasePlayer();
            }
        });
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        releasePlayer();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        releasePlayer();
    }

    private void releasePlayer()
    {
        if (player != null)
        {
            player.release();
        }
    }
}

but all I experience is the sound of the video, the screen keeps blank on my Samsung GalaxyTab. The source file is an mp4 file (H.264 AVC, 960x640, 30fps) and can be played perfectly fine with Quicktime and VLC.

I tried to downscale and resize the original video with Handbrake, down to 480x320 and 25fps, I tried several settings in handbrake, everything without success.

Is there anything obviously wrong with my code or is it the video format or something else - what am I doing wrong?

Thanks in advance,
Thomas.

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

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

发布评论

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

评论(3

依 靠 2024-11-26 06:36:38

我无法使用上述方式工作,但我发现当我单独使用 Android 的 VideoView 提供的功能时,它可以工作,如下所示:

...
String videoUri = "android.resource://my.package.path/raw/intro_video";
introVideo.setVideoURI(Uri.parse(videoUri));
introVideo.start();
...

I couldn't get the above way to work, but I found that it worked when I solely used the provided functionality of Android's VideoView like this:

...
String videoUri = "android.resource://my.package.path/raw/intro_video";
introVideo.setVideoURI(Uri.parse(videoUri));
introVideo.start();
...
栀梦 2024-11-26 06:36:38

它是 10.1v 选项卡(运行 3.0 或 3.1)还是运行 2.2 的“旧”选项卡?

根据此: http://developer.android.com/guide/appendix/media -formats.html 您似乎需要 3.0+ 才能使用 H.264 AVC。
我自己在使用 H 264 时遇到了麻烦,即使使用推荐值,也不得不进一步降低声音。可能值得研究一下。

Is it the 10.1v tab (running 3.0 or 3.1) or an "old" one running 2.2?

According to this: http://developer.android.com/guide/appendix/media-formats.html you seem to need 3.0+ for H.264 AVC.
I have had troubles with H 264 myself even with recommended values, had to downscale the sound even more. Might be worth looking in to.

数理化全能战士 2024-11-26 06:36:38

为 MediaPlayer 提供一个PreparedListener。并在PreparedListener中调用MediaPlayer.start()。

Give MediaPlayer a PreparedListener. And call MediaPlayer.start() in PreparedListener.

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