Как скачать список видео на SDCard и воспроизвести на видеовиде?
Я использовал videoview для локального воспроизведения видео из необработанной папки, но теперь я пытаюсь загрузить список видео сначала на SDCard, а затем воспроизвести его в медиаплеере. Вот мой видеовид.
открытый класс 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);
}
});
}
}
2 ответа
Шаг 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;
}
}
Я использую приведенный ниже код для загрузки видео с сервера
class DownloadFileFromURL extends AsyncTask<Object, String, Integer> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onCancelled() {
super.onCancelled();
}
/**
* Downloading file in background thread
* */
@Override
protected Integer doInBackground(Object... params) {
try {
URL url = new URL((String) params[1]);
name = ((String) params[1]).substring(((String) params[1])
.lastIndexOf("/") + 1);
// Log.v("log_tag", "name Substring ::: " + name);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),
8192);
File download = new File(Environment.getExternalStorageDirectory()
+ "/download/");
if (!download.exists()) {
download.mkdir();
}
String strDownloaDuRL = download + "/" + name;
Log.v("log_tag", " down url " + strDownloaDuRL);
FileOutputStream output = new FileOutputStream(strDownloaDuRL);
byte data[] = new byte[1024];
long total = 0;
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return 0;
}
protected void onPostExecute(String file_url) {
// Do after downloaded file
}
}