gpt4 book ai didi

android - 如何下载 sdcard 上的视频列表并在 videoview 上播放?

转载 作者:行者123 更新时间:2023-11-30 01:31:09 25 4
gpt4 key购买 nike

我已经使用 videoview 在本地播放原始文件夹中的视频,但现在我试图先在 sdcard 上下载视频列表,然后再将其播放到媒体播放器。这是我的视频 View 。

公共(public)类 MainActivity 扩展 Activity {

/* Full Screen Mode-Sticky */
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
View decorView = getWindow().getDecorView();
if (hasFocus) {
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);}
}



public void downloadVideoFile(String url, String dest_file_name) {
try {

URL domain = new URL("http://192.168.0.22");
String video_folder = "video";
String sdcard_path = Environment.getExternalStorageDirectory().getAbsolutePath();
String dest_video_path = sdcard_path + File.separator + video_folder + File.separator + dest_file_name;
File dest_file = new File(dest_video_path);
URL u = new URL(domain + "/files/video/");
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(dest_file));
fos.write(buffer);
fos.flush();
fos.close();

} catch(FileNotFoundException e) {

return;

} catch (IOException e) {

return;

}
}


private VideoView myVideo1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.activity_main);

String video_folder = "myvideos";
String sdcard_path = Environment.getExternalStorageDirectory().getAbsolutePath();
File fvideo_path = new File(sdcard_path + File.separator + video_folder);
File videolist[] = fvideo_path.listFiles();
String play_path = videolist[0].getAbsolutePath();

myVideo1=(VideoView)findViewById(R.id.myvideoview);
myVideo1.setVideoPath(play_path);
myVideo1.start();
myVideo1.requestFocus();

myVideo1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
}

最佳答案

第 1 步:从 sdcard 上的特定路径构建视频列表

String video_folder = "myvideos";
String sdcard_path = Environment.getExternalStorageDirectory();

File fvideo_path = new File(sdcard_path + File.separator + video_folder);

File videolist[] = fvideo_path.listFiles();

第 2 步:按索引播放列表中的任意视频

//you can next or prev index from 0 - list lenght;
String play_path = videolist[0].getAbsolutePath();

第 3 步:将 play_path 设置为媒体播放器

   myVideo1.setVideoPath(play_path);
myVideo1.start();
myVideo1.requestFocus();

从服务器下载文件的示例代码:

public void downloadVideoFile(String url, String dest_file_name) {
try {


String video_folder = "myvideos";
String sdcard_path = nvironment.getExternalStorageDirectory();
String dest_video_path = sdcard_path + File.separator + video_folder + File.separator + dest_file_name;
File dest_file = new File(dest_video_path);
URL u = new URL(url);
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(dest_file));
fos.write(buffer);
fos.flush();
fos.close();

} catch(FileNotFoundException e) {

return;

} catch (IOException e) {

return;

}
}

关于android - 如何下载 sdcard 上的视频列表并在 videoview 上播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35749648/

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