gpt4 book ai didi

Android:VideoView 有时会在一段时间后停止播放视频

转载 作者:行者123 更新时间:2023-11-30 02:32:12 28 4
gpt4 key购买 nike

我创建了一个持续播放来自本地 (LAN) 网站的视频的应用程序。首先,它加载一个文本文件,其中包含要播放的视频列表。然后该应用程序一个接一个地播放每个视频。它工作正常,但有时它会停止。在随机视频。当它停止并且我打开另一个 Activity 并返回到上一个 Activity (视频)时,视频再次播放。有时我可以播放播放列表中的所有视频并返回到第一个。简而言之,该应用程序能够播放所有视频(没有编码器问题)。这是我的代码。

    videoView.setOnCompletionListener(new OnCompletionListener() {          
@Override
public void onCompletion(MediaPlayer mp) {
videourl = pmgr.getNextFile();
video = Uri.parse(videourl);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.start();
}
});

videoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setVolume(actualVolume, actualVolume);
mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
}
});


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
videoView.seekTo(position);
}






@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
position = videoView.getCurrentPosition();
}

最佳答案

这是对我有用的。

videoView.setOnCompletionListener(new OnCompletionListener() {          
@Override
public void onCompletion(MediaPlayer mp) {
videoView.stopPlayback();
videourl = pmgr.getNextFile();
video = Uri.parse(videourl);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.start();
}
});

我添加了对 VideoView 类的 stopPlayback() 的调用。从那时起,我所有的视频(来自本地网站)都在连续播放。

播放列表管理类

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;

import android.os.AsyncTask;

public class PlaylistManager extends AsyncTask<String, Integer, String> {

private PlaylistLoadedListener pl;
private String urlPath = "";
private String tempList;
private ArrayList<String> playlist;
private int currentIndex = -1;
private boolean playlistLoaded = false;

HttpURLConnection urlConnection;

public boolean getPlaylistLoaded() {
return playlistLoaded;
}
public ArrayList<String> getPlaylist() {
return playlist;
}






public PlaylistManager(String urlPath,PlaylistLoadedListener pl) {
this.urlPath = urlPath;
this.pl = pl;
}





@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(this.urlPath + params[0]);
urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.setRequestMethod("GET");
urlConnection.connect();

InputStream in = urlConnection.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = in.read(buffer);
bo.write(buffer, 0, length);
tempList = bo.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tempList;
}






@Override
protected void onPostExecute(String result) {
try {
playlist = new ArrayList<String>();
Scanner scanner = new Scanner(result);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (!line.contains("--")) {
playlist.add(line);
}
}
scanner.close();
if (playlist.size() != 0) {
playlistLoaded = true;
pl.OnPlaylistLoaded();
}
}
catch (NullPointerException e) {
}
finally {
urlConnection.disconnect();
}
}






public String getNextFile() {
String ret = "";
if (playlistLoaded) {
if (playlist.size() != 0) {
currentIndex++;
if (currentIndex == playlist.size()) {
currentIndex = 0;
}
ret = urlPath + playlist.get(currentIndex);
}
}
return ret;
}

}

播放列表加载监听接口(interface)

public interface PlaylistLoadedListener {
void OnPlaylistLoaded();
}

关于Android:VideoView 有时会在一段时间后停止播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27180950/

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