【问题标题】:Shoutcast radio with J2ME使用 J2ME 的 Shoutcast 广播
【发布时间】:2014-03-04 16:57:01
【问题描述】:

我的应用程序正在播放广播流,即广播流。我在互联网上搜索了 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();

【问题讨论】:

  • 我没有解决方案,但我认为如果你能满足于 rtsp 协议,那会容易得多。我认为(无论如何)JavaME 手机都支持 rtsp 协议。

标签: java-me shoutcast


【解决方案1】:

我已经在生产中使用了这个代码,它应该也适合你。

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

【讨论】:

    【解决方案2】:

    试试这个。

    public void loadShoutcast(字符串 url) { StreamConnection 连接 = null; 整数缓冲区大小 = 1024; 数据输出流 dos_ = null; 输出流 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多