简体   繁体   English

Android - MediaPlayer HTTP错误处理

[英]Android - MediaPlayer HTTP Error Handling

I am attempting to create an Android app which collects a variety of remote song links and plays them out for users. 我正在尝试创建一个Android应用程序,它收集各种远程歌曲链接并为用户播放。 The app uses Android's MediaPlayer, and every single song link redirects to a Stream URL which the MediaPlayer handles and eventually plays out. 该应用程序使用Android的MediaPlayer,每个歌曲链接都会重定向到MediaPlayer处理并最终播放的Stream URL。

However, some of these song links actually redirect and provide a HTTP 404 Error as they are not available, and I want to know if there is any way to directly handle/catch for these HTTP errors instantly. 但是,这些歌曲链接中的一些实际上是重定向并提供HTTP 404错误,因为它们不可用,我想知道是否有任何方法可以立即直接处理/捕获这些HTTP错误。 As of now, what the MediaPlayer does is signal out a: 截至目前,MediaPlayer所做的是发出信号:

error (1, -2147483648)

... after it attempts to connect with the data source 10 times , which I can then catch to notify the user that the song is unavailable. ...在尝试连接数据源10次​​之后 ,我可以捕获它以通知用户该歌曲不可用。

Of course though, having the user wait for 10 retries takes far too much time, and so I want to know if there is a way to catch the very first moment the HTTP error occurs from the MediaPlayer without having to check the URL before sending it to the MediaPlayer, or having to hack away and manage the exceptions with a network proxy connected to the MediaPlayer. 当然,让用户等待10次重试需要花费太多时间,所以我想知道是否有办法捕获MediaPlayer发生HTTP错误的第一时刻,而不必在发送之前检查URL到MediaPlayer,或者必须通过连接到MediaPlayer的网络代理来破解和管理异常。

Exception given by MediaPlayer 10 times: MediaPlayer给出的例外情况10次:

07-16 05:16:30.549: E/MediaHTTPConnection(16698): IOException: java.io.IOException: Http Response: 404

If the error pops up 10 times, and each time an error is put out, then I don't see why a simple try-catch loop will not work: 如果错误弹出10次,每次发出错误,那么我不明白为什么一个简单的try-catch循环不起作用:

try {
    // download URL
} catch (IOException e) {
    // presumably an exception is thrown the first time it gets 404 so put the error handling code here
}

Also, might I suggest using multithreading to download from URLs using something like the ExectutorService which allows you to submit Runnable classes to it and run them concurrently (ie the download Tasks for each video so they download at the same time) 另外,我是否可以建议使用多线程从URL下载,使用类似ExectutorService东西,它允许您向其提交Runnable类并同时运行它们(即每个视频的下载任务,以便它们同时下载)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM