【发布时间】:2012-07-06 05:55:47
【问题描述】:
我正在制作一个应用程序,我必须在 片段 中显示资产文件夹中的视频。谁能帮我做到这一点?我需要在 XML 中使用 VideoView 吗?
【问题讨论】:
-
是的,它是重复的,但请再次检查链接,没有任何代码有效
标签: android video android-videoview
我正在制作一个应用程序,我必须在 片段 中显示资产文件夹中的视频。谁能帮我做到这一点?我需要在 XML 中使用 VideoView 吗?
【问题讨论】:
标签: android video android-videoview
您必须将视频复制到项目的 res/raw 文件夹中,而不是从资产访问。 在 res 文件夹下创建 raw 文件夹。 它必须采用受支持的格式(3gp、wmv、mp4)并在其文件名中使用小写字母、数字、下划线和点命名:video_file.mp4。
VideoView view = (VideoView)findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
view.setVideoURI(Uri.parse(path));
view.start();
【讨论】:
VideoView view = (VideoView)findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
view.setVideoURI(Uri.parse(path));
view.start();
是AkashG的代码,不过我记得这里的R不是Android类的。 它来自您自己的项目。
【讨论】:
您首先需要将您的视频转换为InputStream,然后将其保存到用户的内部存储中,然后在视频播放完毕后显示并删除该文件。
try{
String path = Environment.getExternalStorageDirectory()+"/"+APP_NAME()+"/videos/"+ls+"/" ;
InputStream input = getAssets().open("vid/dal.mp4");
String name = System.currentTimeMillis() +".mp4";
File f = new File(path);
f.mkdirs();
int size = input.available();
FileOutputStream output = new FileOutputStream(new File(path+name));
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
total += count;
if (size <= total) {
break;
}
}
output.flush();
output.close();
input.close();
//Toast.makeText(VideoPlayer.this , "file created !" , Toast.LENGTH_LONG).show();
Uri uri = Uri.parse(path+name) ;
videoView.setVideoURI(uri);
videoview.start();
}cath(Exception e){
}
【讨论】:
我已经遇到了同样的问题,你应该更喜欢项目的 res/raw 文件夹而不是资产。在 res 文件夹下创建 raw 文件夹。以支持的格式保存视频文件(3gp, wmv, mp4),并在文件名中以小写、数字、下划线和点命名likewise:filename.3gp 进入原始文件夹。
VideoView videoview = (VideoView) findViewById(R.id.VideoView);
String uriPath = "android.resource://your application package name/raw/your
wmv/mp4/3gp file in res/raw path without extension";
videoview.setVideoURI(Uri.parse(uriPath));
videoview.start();
【讨论】:
播放 res/raw 文件夹中的视频(sample.mp4)以及媒体控制器
// 导入语句
import android.widget.VideoView;
import android.widget.MediaController;
public class youractiviy extends Activity {
private VideoView videoView;
private MediaController mediaController;
protected void onCreate(Bundle savedInstanceState) {
// Your Startup code
videoView = (VideoView) findViewById(R.id.video_view);
videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.sample);
mediaController = new MediaController(TestActivity.this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.start();
}
}
// XML 代码
<VideoView
android:id="@+id/video_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
【讨论】: