使用J2ME的Shoutcast无线电

时间:2013-12-22 10:25:49

标签: java-me shoutcast

我的应用程序正在播放shoutcast流媒体流。我在互联网上搜索了2天,但我的应用程序仍然无法播放此网址。我不知道如何解决这个问题。

这是我的示例源代码。

 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();

2 个答案:

答案 0 :(得分: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();
                    }

答案 1 :(得分: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;
}