0

I use WebView to view some web contents. Some of the pages redirects me to streaming audio. When I access the streaming audio URL with the device browser, it prompts me to open an external player or just plays it. When I open it with my WebView, I see a blank page.

My question is: How can I tell that a link (or a URL) is actually a streaming audio and handle it accordingly?

URL for example:

http://mobile.internet-radio.com/MobileInternetRadioPlayer.php?radio=dWszLmludGVybmV0LXJhZGlvLmNvbToxMDY5Nw==
Asaf Pinhassi
  • 15,177
  • 12
  • 106
  • 130
  • I am having the same issue now? How do u identify audio file? From this, i didn't get. Please help me. – Dhasneem Jun 06 '13 at 04:26

2 Answers2

0

This is the HTTP header of response:

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

So, returned Content-Type is one of the Audio MimeType's . Which means audio data is being returned.

You can get a list of headers using HttpResponse.getHeaders() method.

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • Is there a way to get it from the webview or should I check each redirect using WebViewClient.shouldOverrideUrlLoading, decide if its an audio and then handle it accordingly? – Asaf Pinhassi Mar 06 '13 at 11:27
  • @Pinhassi I'd recommend filtering outside the `WebView`, show some different HTML telling that this is not audio media etc. – S.D. Mar 06 '13 at 11:31
  • @user117 I am having the same issue now? How do u identify audio file? From this, i didn't get. Please help me. – Dhasneem Jun 06 '13 at 04:23
0

This is how I did it:

    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;
    }

To use it:

                // 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;
                }
            }
Asaf Pinhassi
  • 15,177
  • 12
  • 106
  • 130