【问题标题】:Streaming audio not working in Android流式音频在 Android 中不起作用
【发布时间】:2010-12-26 02:45:33
【问题描述】:

我确定以前有人问过这个问题,但我一直找不到可靠的答案。我正在尝试从服务器加载流式音频。它是一个音频/aac 文件

http://3363.live.streamtheworld.com:80/CHUMFMAACCMP3

我使用的代码是

private void playAudio(String str) {
  try {
   final String path = str;

   if (path == null || path.length() == 0) {
    Toast.makeText(RadioPlayer.this, "File URL/path is empty",
      Toast.LENGTH_LONG).show();

   } else {
    // If the path has not changed, just start the media player


    MediaPlayer mp = new MediaPlayer();

    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);


    try{
     mp.setDataSource(getDataSource(path));
     mp.prepareAsync();
     mp.start();
    }catch(IOException e){
     Log.i("ONCREATE IOEXCEPTION", e.getMessage());
    }catch(Exception e){
     Log.i("ONCREATE EXCEPTION", e.getMessage());
    } 


   }
  } catch (Exception e) {
   Log.e("RPLAYER EXCEPTION", "error: " + e.getMessage(), e);

  }
 }

 private String getDataSource(String path) throws IOException {
  if (!URLUtil.isNetworkUrl(path)) {
   return path;
  } else {
   URL url = new URL(path);
   URLConnection cn = url.openConnection();
   cn.connect();
   InputStream stream = cn.getInputStream();
   if (stream == null)
    throw new RuntimeException("stream is null");
   File temp = File.createTempFile("mediaplayertmp", ".dat");
   temp.deleteOnExit();
   String tempPath = temp.getAbsolutePath();
   FileOutputStream out = new FileOutputStream(temp);
   byte buf[] = new byte[128];
   do {
    int numread = stream.read(buf);
    if (numread <= 0)
     break;
    out.write(buf, 0, numread);
   } while (true);
   try {
    stream.close();
   } catch (IOException ex) {
    Log.e("RPLAYER IOEXCEPTION", "error: " + ex.getMessage(), ex);
   }
   return tempPath;
  }
 }

这是正确的实现吗?我不确定我哪里出错了。有人可以请帮助我吗?

【问题讨论】:

标签: android audio streaming aac


【解决方案1】:

prepareAsync() 方法是异步的——引用文档:

准备播放器进行异步播放。

您需要致电setOnPreparedListener(),提供MediaPlayer.OnPreparedListener。然后,在那个听众的onPrepared(),你可以调用start()

【讨论】:

  • 我这样做了,但我仍然收到错误命令 PLAYER_INIT completed with an error or info PVMFErrCorrupt
【解决方案2】:

不支持AAC流格式,安卓2.2以下无法直接播放Shoutcast流..

【讨论】:

  • 嗨...流是 AAC,它位于 FLV 容器中。我使用 NPR 应用程序中使用的 StreamProxy 方法成功播放了 mp3 流。你对此有什么想法吗?
  • NPR 应用仅播放 Mp3 流。您可以使用 FFMPEG-android 播放其他格式
  • 嗨 Eby.. 我正在考虑使用 FFMPEG-Android,但找不到一个好的示例。您是否有任何指向此类工作示例的链接/或者您能帮我提供一些代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
相关资源
最近更新 更多