【问题标题】:play audio from internet using AVAudioPlayer使用 AVAudioPlayer 从互联网播放音频
【发布时间】:2010-09-03 12:40:09
【问题描述】:

我正在实现 AVAudioPlayer 来播放音频,它在播放本地存储在 PC 中的文件时效果很好。

但是当我在互联网上提供一些音频文件的 url 时,它很遗憾地失败了。 代码如下所示:

NSString *url = [[NSString alloc] init];  
url = @"http://files.website.net/audio/files/audioFile.mp3";  
NSURL *fileURL = [[NSURL alloc] initWithString: url];  
AVAudioPlayer *newPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];

谁能指出问题和可以做些什么?
谢谢!

【问题讨论】:

  • 您是否尝试过提供错误对象以查看它是否包含错误描述?
  • 不,但应用程序也没有崩溃……只是视图出现了,没有任何反应。

标签: ios objective-c streaming avaudioplayer


【解决方案1】:

使用 AVPlayer 根据 http url 流式传输音频/视频。它会正常工作。 AVAudioPlayer 用于本地文件。这是代码

NSURL *url = [NSURL URLWithString:url];    
self.avAsset = [AVURLAsset URLAssetWithURL:url options:nil];    
self.playerItem = [AVPlayerItem playerItemWithAsset:avAsset];    
self.audioPlayer = [AVPlayer playerWithPlayerItem:playerItem];    
[self.audioPlayer play];

【讨论】:

    【解决方案2】:

    这是 Apple 文档所说的:

    AVAudioPlayer 类不支持基于 HTTP URL 的流式音频。与initWithContentsOfURL: 一起使用的 URL 必须是文件 URL (file://)。即本地路径。

    【讨论】:

    • 我花了很长时间试图解决这个问题,直到我在这里遇到你的答案。感谢互联网陌生人。这帮助很大。
    【解决方案3】:

    我在 AVAudioPlayer 上尝试了其他方法 initWithData 而不是 initWithContentsOfURL。首先尝试将 MP3 文件放入 NSData,然后播放此数据。

    看看我的代码here

    【讨论】:

      【解决方案4】:

      使用AVPlayer 并监控其状态以开始播放。

      这是一个可行的例子,希望对你有所帮助。

      @implementation AudioStream
      
      - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
      {
          if (context == PlayerStatusContext) {
              AVPlayer *thePlayer = (AVPlayer *)object;
              switch ([thePlayer status]) {
                  case AVPlayerStatusReadyToPlay:
                      NSLog(@"player status ready to play");
                      [thePlayer play];
                      break;
                  case AVPlayerStatusFailed:
                      NSLog(@"player status failed");
                      break;
                  default:
                      break;
              }
              return;
          } else if (context == ItemStatusContext) {
              AVPlayerItem *thePlayerItem = (AVPlayerItem *)object;
              switch ([thePlayerItem status]) {
                  case AVPlayerItemStatusReadyToPlay:
                      NSLog(@"player item ready to play");
                      break;
                  case AVPlayerItemStatusFailed:
                      NSLog(@"player item failed");
                      break;
                  default:
                      break;
              }
              return;
          }
      
          [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
      }
      
      - (void)playAudioStream
      {
          NSURL *audioUrl = [NSURL URLWithString:@"your_stream_url"];
          AVURLAsset *audioAsset = [AVURLAsset assetWithURL:audioUrl];
          AVPlayerItem *audioPlayerItem = [AVPlayerItem playerItemWithAsset:audioAsset];
          [audioPlayerItem addObserver:self forKeyPath:@"status" options:0 context:ItemStatusContext];
          self.player = [AVPlayer playerWithPlayerItem:audioPlayerItem];
          [self.player addObserver:self forKeyPath:@"status" options:0 context:PlayerStatusContext];
      }
      
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多