Android流m3u无线电在移动数据上失败(g3 /移动数据)

时间:2014-01-05 20:47:48

标签: android media-player 3g

不太容易解释:

我有这个应用程序用于流媒体在线广播。问题首先是m3u格式(android某种方式通常不能像pls一样流),所以我必须使用这个ParserM3UToURL(我在某处找到)来解析url ...就像这样:

Uri u = Uri.parse(ParserM3UToURL.parse(STREAM_URL, sdkVersion, c));
player = MediaPlayer.create(c, u);

大多数情况下它可以正常但它有一个错误...

我正在两个旧设备2.2.2上测试这个。 (api等级17),其他4.3(api等级23)。旧设备工作正常。 它可以通过wifi或移动数据传输无线电,但是较新的设备在移动数据上流式传输存在一些问题(在wifi上工作正常)。应用程序崩溃,因为解析函数返回null:{{3 }}

我认为还有更多的手机4.x而不是2.x android。这当然对我来说非常痛苦。不知怎的,我必须解决这个问题。所以我真的希望有人会对此有所了解。我希望我的解释不是混淆......

这是ParserM3UToURL.parse()函数:

public static String parse(String paramString, int sdkVersion, Context c)
{
    try
    {
      StrictModeWrapper.init(c);
      HttpURLConnection localHttpURLConnection = (HttpURLConnection)new URL(paramString).openConnection();
      InputStream localInputStream = localHttpURLConnection.getInputStream();
      BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream));
      StringBuffer localStringBuffer = new StringBuffer();
      while (true)
      {
        String str = localBufferedReader.readLine();
        if (str == null)
        {
          localHttpURLConnection.disconnect();
          localBufferedReader.close();
          localInputStream.close();
          break;
        }
        if (str.contains("http"))
        {
          localHttpURLConnection.disconnect();
          localBufferedReader.close();
          localInputStream.close();
          return str;
        }
        localStringBuffer.append(str);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return null;
}

2 个答案:

答案 0 :(得分:5)

以下是我用于流式广播(m3Urls)的工作。以下示例使用服务。启动服务时,将解析URL。请注意,在onPostExecute中,已准备好已解析的文件。准备好文件(完成缓冲)后,文件将在完成时播放/启动并停止。

  public class BackgroundRadioService extends Service implements
        OnCompletionListener, OnPreparedListener{
     MediaPlayer mediaPlayer;


@Override
public void onCreate() {

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnPreparedListener(this);

}

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
              parseM3uUrlAndPrepare("http://listen.radionomy.com/andalousse.m3u");
    return START_STICKY;
    }



  private void parseM3uUrlAndPrepare(final String url){

AsyncTask<String, Integer, String> asyn = new  AsyncTask<String, Integer, String>(){

        HttpClient httpClient;
        HttpGet getRequest;
        HttpResponse httpResponse = null;
        String filePath = "";

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                 httpClient = new DefaultHttpClient();


                    getRequest = new HttpGet(url);

            }

        @Override
        protected String doInBackground(String... params) {

                try {
                    httpResponse = httpClient.execute(getRequest);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if(httpResponse != null)
                if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                    // ERROR MESSAGE


                } else {
                    InputStream inputStream = null;
                    try {
                        inputStream = httpResponse.getEntity().getContent();
                    } catch (IllegalStateException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } 
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    String line;
                    try {
                        while ((line = bufferedReader.readLine()) != null) {
                            //Log.v("PLAYLISTLINE", "ORIG: " + line);
                            if (line.startsWith("#")) { // Metadata

                            } else if (line.length() > 0) {
                                filePath = "";

                                if (line.startsWith("http://")) { // Assume it's a full URL
                                    filePath = line;
                                } else { // Assume it's relative
                                    try{
                                    filePath = getRequest.getURI().resolve(line).toString();
                                    }catch(IllegalArgumentException e){

                                    }catch(Exception e){

                                    }
                                }


                            }
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            return filePath;
        }

        @Override
        protected void onPostExecute(String filePath) {
            try {
            mediaPlayer.setDataSource(filePath);
            mediaPlayer.prepareAsync(); //this will prepare file a.k.a buffering

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }
    };          
        asyn.execute("");

}

@Override
        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub      
            mediaPlayer.start();    
}

@Override
    public void onCompletion(MediaPlayer mp) {  
        mediaPlayer.stop();
      }

}//end of Service class declaration

注意:这会忽略播放列表,因此假设已解析的m3u将只返回一个文件。如果您想处理播放列表,请告诉我,以便我修改我的答案:)

答案 1 :(得分:1)

由于这个问题的评论,我解决了这个问题:POST request failing when in 3G

问题实际上是代理3G。因此,如果代理被禁用,则没有奇怪的http请求。

我修改了我的代码。感谢Nana的回答,我不再需要m3u解析器了。我也不再使用HttpClient而是使用HttpURLConnection。因此,当调用URL.openConnection()时,我将Proxy.NO_PROXY参数添加到该函数和bam!

所以解决方案是“使用HttpURLConnection而不是HttpClient并添加NO_PROXY参数”:

conn = (HttpURLConnection) the_url.openConnection( Proxy.NO_PROXY );