流式音频在 Android 中不工作

发布于 2024-10-09 12:25:29 字数 1890 浏览 3 评论 0原文

我确信这个问题以前曾被问过,但我一直无法找到可靠的答案。我正在尝试从服务器加载流音频。它是一个音频/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;
  }
 }

这是正确的实现吗?我不确定我哪里错了。有人可以帮我解决这个问题吗?

I'm sure that this question has been asked before but I've been unable to find a solid answer. I'm trying to load a streaming audio from a server. Its a audio/aac file

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

The code that I'm using is

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

Is this the correct implementation? I'm not sure where I'm going wrong. Can someone please please help me on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋你朝朝暮暮 2024-10-16 12:25:29

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

准备播放器异步播放。

您需要调用 setOnPreparedListener(),并提供 MediaPlayer.OnPreparedListener。然后,在该侦听器的 onPrepared() 中,您可以调用 start()

The method prepareAsync() is asynchronous -- quoting the documentation:

Prepares the player for playback, asynchronously.

You need to call setOnPreparedListener(), supplying a MediaPlayer.OnPreparedListener. Then, in that listener's onPrepared(), you can call start().

盗琴音 2024-10-16 12:25:29

不支持 AAC 流媒体格式,并且您无法在低于 2.2 的 Android 版本中直接播放 Shoutcast 流。

AAC streaming format is not supported and you cannot directly play Shoutcast stream in Android version less than 2.2..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文