使用缓冲/流媒体块播放远程 mp3 文件

发布于 2024-12-15 22:10:43 字数 864 浏览 1 评论 0原文


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

  1. 它在加载时播放,因此我不必等待整个内容加载即可播放。
  2. 它将有某种委托来显示播放曲目的进度。

我尝试过使用 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。



I'm currently using CocosDenshion for small sound effects (half a second long), And now
I need to play short 30-second audio clips (mp3) that are hosted on a server, not inside the app resource bundle. I'm trying to get some code to play it with two "requirements":

  1. that it plays as it loads, so i wouldn't have to wait for the entire thing to load just to play it.
  2. That it would have some sort of delegate to show the progress of the played track.

I've tried using AVAudioPlayer but it doesnt work for me, plus it doesn't "Buffer" the data, it waits for the entire thing to load (Tried on simulator, if it matters). What i tried is:

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

Would love your help an experience on this :)
Cheers,
Shai.

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

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

发布评论

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

评论(1

﹏半生如梦愿梦如真 2024-12-22 22:10:43

在你的 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");
    }
});

In your didLoad method

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