请帮忙, 如何在原始文件夹中播放Android设备中的视频以进行离线模式?
成功示例1:我可以使用以下代码从SDcard播放视频。
Intent intent = new Intent(Intent.ACTION_VIEW);
String type = "video/mp4";
Uri uri = Uri.parse("file:///sdcard/test.mp4");
intent.setDataAndType(uri, type);
startActivity(intent);
示例2失败: 问题:我可以将test.mp4放到res / raw文件夹中吗?
Intent intent = new Intent(Intent.ACTION_VIEW);
String type = "video/mp4";
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.taipei);
intent.setDataAndType(uri, type);
startActivity(intent);
有人可以帮助我吗?请。
答案 0 :(得分:46)
将视频复制到项目的res / raw文件夹中。在res文件夹下创建原始文件夹。它必须采用支持的格式(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();
在xml文件中创建videoView。
答案 1 :(得分:5)
// To get files from any resource folder (eg: raw, drawable, etc.)
// Use the resource id
int rawId = getResources().getIdentifier(file_name_without_extension, "raw", getPackageName());
// URI formation
String path = "android.resource://" + getPackageName() + "/" + rawId;
// Set the URI to play video file
videoView.setVideoURI(Uri.parse(path));
答案 2 :(得分:3)
检查此解决方案How to play videos in android from assets folder or raw folder?
VideoView videoHolder = new VideoView(this);
//if you want the controls to appear
videoHolder.setMediaController(new MediaController(this));
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.your_raw_file); //do not add any extension
//if your file is named sherif.mp4 and placed in /raw
//use R.raw.sherif
答案 3 :(得分:1)
当你在RAW文件夹中播放任何内容时,需要一个处理程序,例如videoview或内置媒体播放器,这样可以得出结论:你的RAW文件夹中的任何内容都应该被Android操作系统支持/读取。
但是,这里的线程启动器想要使用第三方媒体播放器播放他的RAW文件。
答案 4 :(得分:1)
是我的视频文件的名称。
String video_url = "android.resource://" + context.getPackageName() + "/" + R.raw.applicationdemo;
final VideoView videoView = findViewById(R.id.dialog_video);
Uri videoUri = Uri.parse(video_url);
MediaController mediaController= new MediaController(context);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(videoUri);
videoView.requestFocus();
videoView.start();
答案 5 :(得分:0)
我为动态视频名称而苦苦挣扎。对我有用的解决方案是:
//Somewhere set the video name variable
String video+name="myvideo";
//setup up and play video
VideoView videoView=(VideoView)findViewById(R.id.video);
videoView.setVisibility(View.VISIBLE);
String uriPath = "android.resource://"+getPackageName()+"/raw/"+ video_name;
Uri UrlPath=Uri.parse(uriPath);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(UrlPath);
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
if (position == 0) {
try{
videoView.requestFocus();
videoView.start();
}catch (Exception e){
System.out.printf("Error playing video %s\n", e);
}
}else{
videoView.pause();
}
}
});
以XML格式
<VideoView android:layout_width="300dp"
android:id="@+id/video"
android:layout_height="300dp"
android:orientation="horizontal"
android:layout_gravity="center"
android:keepScreenOn="true"
/>
答案 6 :(得分:0)
此解决方案将帮助您实现所需目标。
VideoView myVideo;
private MediaController media_control;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideo = (VideoView) findViewById(R.id.playVideo);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bootanimation_nexus);
media_control = new MediaController(this);
myVideo.setMediaController(media_control);
myVideo.setVideoURI(uri);
myVideo.start();
}
答案 7 :(得分:0)
player = ExoPlayerFactory.newSimpleInstance(requireActivity(), new DefaultTrackSelector(), new DefaultLoadControl());
Uri uri = RawResourceDataSource.buildRawResourceUri(getOfflineVideo(offlinePosition));
MediaSource mediaSource = new ExtractorMediaSource(uri, new DefaultDataSourceFactory(requireActivity(),
"MyExoplayer"), new DefaultExtractorsFactory(), null, null);
//setup player with mediaSource