iphone - 使用缓冲/流媒体 block 播放远程 mp3 文件

标签 iphone ios streaming avaudioplayer buffering


我目前正在使用 CocosDenshion 来制作小音效(半秒长),现在 我需要播放服务器上托管的 30 秒短音频剪辑 (mp3),而不是在应用程序资源包中。我正在尝试获取一些代码来满足两个“要求”:

  1. 它在加载时播放,因此我不必等待整个内容加载完毕就可以播放。
  2. 它将有某种委托(delegate)来显示播放轨道的进度。

我试过使用 AVAudioPlayer,但它对我不起作用,而且它不会“缓冲”数据,它会等待整个加载(如果重要的话,在模拟器上试过)。我尝试的是:

- (IBAction)play:(id)sender{
    NSString *_mp3file = @"http://www.somesite.com/somefile.mp3";
    NSData *_mp3data = [NSData dataWithContentsOfURL:[NSURL URLWithString: _mp3file]];

    NSError *error;

    AVAudioPlayer *avp = [[AVAudioPlayer alloc] initWithData:_mp3data error:&error];
    avp.numberOfLoops = 0;
    avp.volume = 1.0f;
    avp.delegate = self;
    [avp prepareToPlay];

    if(avp == nil)
        GTMLoggerError(@"%@", error);
    else
        [avp play];
}

希望得到您的帮助:)
干杯,
Shai。

最佳答案

在你的 didLoad 方法中

dispatch_queue_t dispatchQueue = 
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(dispatchQueue, ^(void){
    NSString *_mp3file = @"http://cs4405.vk.me/u360087/audios/4efe178f2608.mp3";
    NSData   *_mp3Data = [NSData dataWithContentsOfURL:[NSURL URLWithString: _mp3file]];
    NSError  *_error   = nil;

    self._audioPlayer = [[AVAudioPlayer alloc] initWithData:_mp3Data error:&_error];

    if (self._audioPlayer != nil)
    {
        self._audioPlayer.delegate = self;
        if ([self._audioPlayer prepareToPlay] && [self._audioPlayer play])
        {
            NSLog(@"Successfully started playing");
        } else {
            NSLog(@"Failed to play");
        }
    } else {
        NSLog(@"Failed to instanciate player");
    }
});

关于iphone - 使用缓冲/流媒体 block 播放远程 mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8160271/

相关文章:

javascript - 使用 UIWebview 中的 JSContext 会使我的应用程序被拒绝吗?

ios - 是否可以在没有表格 View 的情况下创建拉动刷新?

silverlight - 有没有人使用过silverlight流sdk?

iphone - 从 Detailview 中删除 Tableview 的行

ios - NSOperationQueue addOperationWithBlock 返回mainQueue的操作顺序

iphone - 我如何从用户未激活/触摸的应用程序外部收到的推送通知中接收信息?

multithreading - Python 2.7 : streaming HTTP server supporting multiple connections on one port

javascript - m3u8 中的 htm5 视频 src 无法在 Chrome 中播放

php - 403 禁止错误 Objective c to PHP

ios - iBeacon 监控具有相同 UUID 和不同主要、次要的多个信标