Android:如何从资产中播放视频?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11356601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:09:22  来源:igfitidea点击:

Android: how to play video from assets?

androidvideoandroid-videoview

提问by AkashG

I am making an application in which I have to show video from assets folder in a Fragment. Can anyone help me do this? Do I need to use VideoView in XML?

我正在制作一个应用程序,我必须在其中显示Fragment 中资产文件夹中的视频。谁能帮我做到这一点?我需要在 XML 中使用 VideoView 吗?

回答by AkashG

Instead of accessing from assests,You must copy the video into your project's res/raw folder. Create raw folder under res folder. It must be in a supported format (3gp, wmv, mp4 ) and named with lower case, numerics, underscores and dots in its filename likewise:video_file.mp4.

您必须将视频复制到项目的 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();

回答by Arman

VideoView view = (VideoView)findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
view.setVideoURI(Uri.parse(path));
view.start();

It's AkashG's code, but I remember that R here is not from the Android class. It's from your own project.

这是 AkashG 的代码,但我记得这里的 R 不是来自 Android 类。它来自您自己的项目。

回答by clasher

You first need to convert your video into the InputStreamand then save it to the user's internal storage, then display it and delete that file when the video is finished.

您首先需要将您的视频转换为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){
}

回答by Tejaswini Hadpe

I have already suffered from same problem, u should prefer project's res/raw folder instead of assets. Create raw folder under res folder. Save video file in a supported format (3gp, wmv, mp4 )and named with lowercase, numerics, underscores and dotsin its filename likewise:filename.3gpinto the raw folder.

我已经遇到了同样的问题,你应该更喜欢项目的 res/raw 文件夹而不是资产。在 res 文件夹下创建 raw 文件夹。以支持的格式(3gp、wmv、mp4)保存视频文件,并在其文件名中以小写、数字、下划线和点命名,同样: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();

回答by Surya Tej

Playing Video(sample.mp4) present in res/ raw folder, along with the Media Controller

播放存在于 res/raw 文件夹中的视频(sample.mp4)以及媒体控制器

// Import Statements

// 导入语句

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 Code

// XML 代码

<VideoView
        android:id="@+id/video_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />