gpt4 book ai didi

java - 如何将视频文件 (mp4) 添加到我的 android 应用程序中?

转载 作者:行者123 更新时间:2023-11-29 07:00:18 24 4
gpt4 key购买 nike

我希望能够添加我创建的视频,当它出现在屏幕上时会自动开始播放,目前我的代码如下所示:

VideoView videoview = (VideoView) findViewById(R.id.button10);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.group_project);
videoview.setVideoURI(uri);
videoview.start();

目前,当我点击并进入该屏幕时,没有任何反应。当我使用媒体播放器时它播放声音,但我真的想添加这个视频。任何帮助将不胜感激。

最佳答案

setOnPreparedListener() 方法的使用非常重要,并且确保在/raw 文件夹中有一个名为group_project.mp4 的视频。

  final VideoView videoview = (VideoView) findViewById(R.id.button10);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.group_project);
videoview.setVideoURI(uri);
videoview.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoview.start();
}
});

阅读文档:

A MediaPlayer object must first enter the Prepared state before playback can be started. There are two ways (synchronous vs. asynchronous) that the Prepared state can be reached: either a call to prepare() (synchronous) which transfers the object to the Prepared state once the method call returns, or a call to prepareAsync() (asynchronous) which first transfers the object to the Preparing state after the call returns (which occurs almost right way) while the internal player engine continues working on the rest of preparation work until the preparation work completes. When the preparation completes or when prepare() call returns, the internal player engine then calls a user supplied callback method, onPrepared() of the OnPreparedListener interface, if an OnPreparedListener is registered beforehand via setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener).

关于java - 如何将视频文件 (mp4) 添加到我的 android 应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27281396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com