2013-10-11 31 views
1

我正在嘗試使用Android的MediaPlayer庫流式傳輸音頻。如何確定某個特定端口是否對防火牆後面的傳入流量打開

在某些情況下,流URL是一個不尋常的端口。例如,http://203.150.225.77:8400。如果我嘗試從瀏覽器的防火牆後面訪問此流,它會給我一個「糟糕!Google Chrome無法連接到203.150.225.77:8400」的錯誤。

Frrom媒體播放器在我的Android設備,我得到:MusicStream(1806): Media Player Error: Stream URL Invalid (1) -1004

目前,我只是處理這個異常。不過,我希望能夠在啓動MediaPlayer之前預先測試URL以確定流是否可用。

所以,我創建了一個功能來測試使用HttpURLConnection類,如下所示的網址:

protected int isURLValid(MusicStream streamInstance, String urlString) 
{ 
    streamInstance.timeout = 0; 
    int success = 0; 
    try 
    { 

     URL url; 
     url = new URL(urlString); 
     HttpURLConnection urlConnection; 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setConnectTimeout(1000); 
     urlConnection.setReadTimeout(1000); 
     urlConnection.getHeaderFields(); 
     new Thread(new InterruptThread(Thread.currentThread(), urlConnection,streamInstance)).start(); 

     if (streamInstance.timeout == 0) 
     { 
      success = 1; 
     } 

    } 
    catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Log.w("MusicStream", "MalformedURLException"); 
     Log.w("MusicStream", e); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Log.w("MusicStream", "IOException"); 
     Log.w("MusicStream", e); 
    } catch (Exception e) { 
     this.exception = e; 
     Log.w("MusicStream", "Exception"); 
     Log.w("MusicStream", e); 

    } 


    return success; 

} 

正如你可以在上面的代碼中看到,我開始一個新的線程5000毫秒後超時。下面是我的線程:只要將我的設備從無線到

if (isURLValid(streamInstance[0],streamURL) == 1){ 
    // Valid URL! 
    Log.d("MusicStream", "The stream is valid!"); 
    this.ResultString = streamURL; 
    streamInstance[0].setFirewallError(MusicStream.GONE,streamURL); 
} 
else{ 
    // Streaming URL invalid. 
    // Firewall Error 
    errorCode = MusicStream.FIREWALL_ERROR; 
    Log.d("MusicStream", "The stream is not valid!"); 
    streamInstance[0].setFirewallError(MusicStream.VISIBLE,streamURL); 
} 

日誌輸出如下

A stream has been supplied: http://203.150.225.77:8400 
The stream is valid! 
MusicStream(1806): Media Player Error: Stream URL Invalid (1) -1004 

public class InterruptThread implements Runnable { 
    Thread parent; 
    URLConnection con; 
    MusicStream streamInstance; 

    public InterruptThread(Thread parent, URLConnection con, MusicStream streamInstance) { 
     this.parent = parent; 
     this.con = con; 
     this.streamInstance = streamInstance; 
    } 

    public void run() { 
     try { 
      Thread.sleep(5000); 
     } catch (InterruptedException e) { 

     } 
     Log.d("MusicStream", "Timer thread forcing parent to quit connection"); 
     ((HttpURLConnection)con).disconnect(); 
     Log.d("MusicStream","Timer thread closed connection held by parent, exiting"); 
     streamInstance.timeout = 1; 
    } 
} 

Nontheless,當我引用我的功能3G,流媒體的作品,所以我有點不知所措,爲什麼我的網址檢查機制在防火牆後回來時有效。任何人都可以看到任何理由,爲什麼我的支票回來有效,並可能提出一個建議,我怎樣才能以更簡單的方式檢查狀態?

額外的想法:我在想,也許一些防火牆會迴應一個「封鎖」的頁面,在這種情況下,獲取標題將無法正常工作。但是,這不是這種情況。

回答

0

這不處理所有內容,但我發現防火牆後面的流是返回有效的html標題。看到的標頭如下:

防火牆後面:

Content Length:  3469 
Content Encoding: null 
Content Type:  text/html 
Content Expiration: 0 
Content Date:  1381912000000 

在一個不同的設備,收到空響應:

Content Length:  -1 
Content Encoding: null 
Content Type:  null 
Content Expiration: 0 
Content Date:  0 

,這不是在防火牆後面返回有效的流一個mpeg頭。注意內容類型和長度:

Content Length:  -1 
Content Encoding: null 
Content Type:  audio/mpeg 
Content Expiration: 0 
Content Date:  0 

要檢查我的網址,我只是使出檢查預期的內容類型和長度的音頻流。

相關問題