我有一个包含以下两个键和值的api:
videoUrl:http://ip.json/master.m3u8
srtUrl:http://ip/The_Last_Lions.srt
我已经完成了联网和解析,并在媒体播放器中播放了视频
现在我正尝试将带有视频视图的srt文件的文本显示为该视频的字幕
所以我这样使用 AsynsTask :
public class ShashaDetails extends AsyncTask<String, Void, ArrayList<DetailItem>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mLoadingIndicator.setVisibility(View.VISIBLE);
}
@Override
protected ArrayList<DetailItem> doInBackground(String... params) {
if (params.length == 0) {
return null;
}
String searchUrl = params[0];
URL channelRequestUrl = NetworkShasha.buildUrl(searchUrl);
try {
String results = NetworkShasha.getResponseFromHttpUrl(channelRequestUrl);
ArrayList<DetailItem> simpleJsonTVData = JsonShashaDetail.getSimpleMovieStringsFromJson(ShashaDetail.this, results);
String srt = simpleJsonTVData.get(0).getSrt();
Log.d("tag", "test: " + srts);
al = new ArrayList<>();
try{
URL urlsrt = new URL(srts);
URLConnection conn = urlsrt.openConnection();
conn.setDoOutput(true);
conn.connect();
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line;
try {
while ((line = br.readLine()) != null) {
al.add(line);
}
} finally {
br.close();
}
}catch (IOException e){
e.printStackTrace();
}
return simpleJsonTVData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
我已经定义了ArrayList al;在上课之初
当我登录或调试srt变量时,该值是真正的链接,如下所示:
然后 onPostExecute :
@Override
protected void onPostExecute(final ArrayList<DetailItem> results) {
if (results != null && !results.equals("")) {
String url = results.get(0).getUrladaptive();
String finalSrt = al.get(0);
InputStream srtStream = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
srtStream = new ByteArrayInputStream(finalSrt.getBytes(StandardCharsets.UTF_8));
}
您会看到我将字符串finalSrt转换为InputStream srtStream,因为它需要在此处使用InputStream类型:
videoView.setMediaController(playerControlView.getMediaControllerWrapper());
videoView.seekTo(position);
videoView.start();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
videoView.addSubtitleSource(srtStream,
MediaFormat.createSubtitleFormat("text/srt",Locale.ENGLISH.getLanguage()));
}
所以我得到了这个错误:
致命异常:主进程:android.os.NetworkOnMainThreadException
然后使用以下代码解决它:
setContentView(R.layout.activity_shasha_detail);
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
但是现在,当我运行该应用程序时,它只是在打开此活动时崩溃,并且出现此错误:
beginning of crash
09-18 08:21:19.271 25904-25904/giga.net.world.GN E/AndroidRuntime: FATAL EXCEPTION: main
Process: giga.net.world.GN, PID: 25904
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at giga.net.world.GN.ShashaDetail$ShashaDetails.onPostExecute(ShashaDetail.java:513)
at giga.net.world.GN.ShashaDetail$ShashaDetails.onPostExecute(ShashaDetail.java:450)
at android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
问题在于以下几行:
public class ShashaDetails extends AsyncTask<String, Void, ArrayList<DetailItem>> {
和:
String finalSrt = al.get(0);
这是logcat的另一个例外:
09-18 09:15:29.267 9637-9637/giga.net.world.GN E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
09-18 09:15:37.108 9637-9653/giga.net.world.GN W/System.err: java.io.FileNotFoundException: http://ip/The_Last_Lions.srt
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:254)
at giga.net.world.GN.ShashaDetail$ShashaDetails.doInBackground(ShashaDetail.java:481)
at giga.net.world.GN.ShashaDetail$ShashaDetails.doInBackground(ShashaDetail.java:450)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
如果我直接在浏览器中打开链接并下载到设备中的srt文件,我不明白为什么它会超出限制范围
我也尝试过使用公共IP上传srt文件,您可以通过以下链接进行检查:
并通过添加以下行将代码更改为使用此静态网址的网络:
String srts = "http://172.104.238.92/text.srt";
代替原来的:
String srts = simpleJsonTVData.get(0).getSrt();
然后,当我运行ap并打开它时,我得到相同的错误,这是它的日志:
09-18 10:42:48.219 11961-11961/giga.net.world.GN E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
09-18 10:42:54.212 11961-11995/giga.net.world.GN W/System.err: java.io.FileNotFoundException: http://172.104.238.92/text.srt
09-18 10:42:54.325 11961-11961/giga.net.world.GN E/AndroidRuntime: FATAL EXCEPTION: main
Process: giga.net.world.GN, PID: 11961
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at giga.net.world.GN.ShashaDetail$ShashaDetails.onPostExecute(ShashaDetail.java:514)
at giga.net.world.GN.ShashaDetail$ShashaDetails.onPostExecute(ShashaDetail.java:450)
at android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
不知道出什么问题