2013-03-06 52 views
0

我使用WebView來查看一些網頁內容。有些頁面會將我重定向到流媒體音頻。當我使用設備瀏覽器訪問流媒體音頻URL時,它會提示我打開外部播放器或僅播放它。當我用我的WebView打開它時,我看到一個空白頁。如何識別流式音頻URL?

我的問題是:我怎麼知道一個鏈接(或一個URL)實際上是一個流式音頻並相應地處理它?

URL例如:

http://mobile.internet-radio.com/MobileInternetRadioPlayer.php?radio=dWszLmludGVybmV0LXJhZGlvLmNvbToxMDY5Nw== 
+0

我有現在同樣的問題?你如何識別音頻文件?從這裏,我沒有得到。請幫幫我。 – Dhasneem 2013-06-06 04:26:56

+0

我發佈了我的解決方案 – 2013-06-06 06:09:15

回答

0

這是響應的HTTP標頭:

HTTP/1.1 200 OK => 
Date => Wed, 06 Mar 2013 10:53:40 GMT 
Server => Apache/2.2.16 
X-Powered-By => PHP/5.3.3-7+squeeze14 
Cache-Control => max-age=600 
Expires => Wed, 06 Mar 2013 11:03:40 GMT 
Connection => close 
Content-Type => audio/mpeg 

所以,返回Content-TypeAudio MimeType's之一。這意味着音頻數據正在返回。

您可以使用HttpResponse.getHeaders()方法獲取標題列表。

+0

有沒有辦法從webview中獲取它,或者我應該使用WebViewClient.shouldOverrideUrlLoading檢查每個重定向,決定它是否是一個音頻,然後相應地處理它? – 2013-03-06 11:27:24

+0

@Pinhassi我建議在WebView外部過濾,顯示一些不同的HTML,告訴他這不是音頻媒體等。 – 2013-03-06 11:31:02

+0

@ user117我現在遇到同樣的問題了嗎?你如何識別音頻文件?從這裏,我沒有得到。請幫幫我。 – Dhasneem 2013-06-06 04:23:52

0

這是我做的:

public String getMimeType(String strUrl) throws IOException, Exception { 
    HttpHead httpHead = new HttpHead(strUrl); 

    HttpResponse response = getHttpClient().execute(httpHead); 

    Header[] headersArr = response.getHeaders("Content-Type"); 
    String mimeType = headersArr[0].getValue(); 
    return mimeType; 
    } 

要使用它:

   // check for special content 
      String mimeType = null; 
      try { 
       mimeType = getMimeType(urlStr).toLowerCase(); 
      } catch (Exception e) { 
      } 

      if (mimeType != null) { 
       // AUDIO 
       if (mimeType.startsWith("audio")){ 
        Uri uri = Uri.parse(urlStr); 
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
        intent.setDataAndType(uri, "audio/*"); 
        startActivity(intent); 
        return true; 
       } else if (mimeType.startsWith("video")){ 
        Uri uri = Uri.parse(urlStr); 
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
        intent.setDataAndType(uri, "video/*"); 
        startActivity(intent); 
        return true; 
       } 
      }