2013-12-22 30 views
0

我有我的应用程序在播放流式播放的电台流。我在网上搜索了2天,但我的应用程序仍然无法播放此网址。我不知道如何解决这个问题。与J2ME的Shoutcast无线电

这是我的示例源代码。

String url1 = "http://203.150.224.142:8003/; 
HttpConnection con= (HttpConnection)Connector.open(url1); 
InputStream is = con.openInputStream(); 
player = Manager.createPlayer(is, "audio/mpeg"); 
player.realize(); 
player.prefetch(); 
player.start(); 
+0

我没有一个解决方案,但我认为,如果你能解决的RTSP协议,这将是一个容易得多。我认为(大多数情况下)JavaME手机支持rtsp协议。 –

回答

0

我已经在生产中使用这个代码,它也应该为你工作。

HttpConnection conn = (HttpConnection) Connector.open(music.getTrack_url() + "?streamable=true", Connector.READ_WRITE); 
        if (conn.getResponseCode() == HttpConnection.HTTP_OK) { 
         is = conn.openInputStream(); 

         player = Manager.createPlayer(is, "audio/mp3"); 
         player.addPlayerListener(thisObj); 
         player.realize(); 
         player.prefetch(); 
         player.start(); 
        } 
0

试试这个。

public void loadShoutcast(String url) { StreamConnection connection = null; int BUFFER_SIZE = 1024; DataOutputStream dos_ = null; OutputStream os_ = null; 尝试 {

 System.out.println("opening connection " + url); 

     //Shoutcast URL 
     connection = (StreamConnection) Connector.open(url); 
     os_ = connection.openOutputStream(); 
     dos_ = new DataOutputStream(os_); 
     // send the HTTP request 
     String req = "GET/HTTP/1.1\r\nUser-Agent: Profile/MIDP-1.0 Configuration/CLDC-1.0\r\n\r\n"; 
     dos_.write(req.getBytes()); 
     is = null; 
     is = connection.openInputStream(); 
     long byteDone = 0; 
     int byteCount; 
     byte[] buffer = new byte[BUFFER_SIZE]; 

     //Connection to file where you want to save the content of shoutcast radio 
     //It can be skipped in case you dont want to save the contents 
     out = tempFilecon.openDataOutputStream(); 
     System.out.println("starting download"); 
     while (byteCount = is.read(buffer)) >= 0) 
     { 
      out.write(buffer, 0, byteCount); 
      byteDone += byteCount; 
      done += byteCount; 
     } 
     return; 
    } 

    catch (InterruptedIOException e) 
    { 
     System.out.println("InterruptedIOException 1" + e.getMessage()); 
     return; 
    } 

    catch (Exception e) 
    { 
     System.out.println("ERROR - 51 " + e.getMessage()); 
     return; 
    } 
    finally 
    { 
      if (dos_ != null) 
      { 
       dos_.close(); 
       dos_ = null; 
      } 
      if (os_ != null) 
      { 
       os_.close(); 
       os_ = null; 
      } 
      if (is != null) 
      { 
       is.close(); 
       is = null; 
      } 
      if (out != null) 
      { 
       out.close(); 
       out = null; 
      } 
      System.out.println("closing connection"); 
      if (connection != null) 
      { 
       connection.close(); 
       connection = null; 
      } 
    } 
    // return false; 
}