使用AVPlayer流式传输mp3音频 [英] Streaming mp3 audio with AVPlayer

查看:131
本文介绍了使用AVPlayer流式传输mp3音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听到一些有关此事的相互矛盾的报道。我想要做的是从URL流式传输mp3文件。我做了几个小时的研究,但是我找不到任何关于如何做到这一点的好指南,甚至找不到我应该使用什么样的音频播放器。

I'm hearing some conflicting reports about this. What I'm trying to do is stream an mp3 file from a URL. I've done hours of research, but I cannot find any good guides on how to do this, or even what kind of audio player I should use.

有些朋友告诉我,AVPlayer可以播放mp3,但Apple文档说不能。我已经倾倒了Matt Gallagher的音频流(http://www.cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html),但是这段代码很久以前就已经发布了,我对此很新,很难通过自动释放和保留以及所有这些。

Some friends tell me that AVPlayer can stream mp3, but the Apple documentation says it can't. I've poured over Matt Gallagher's audio streamer (http://www.cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html), but that code was made a good while ago, and I'm new enough to this that it's hard to work through the autoreleases and retains and all that.

我正在尝试流式传输的音频是一个相当大的mp3文件来自libsyn服务器,格式为URL ..

The audio I'm trying to stream is a fairly large mp3 file from a libsyn server, with a URL of format..

http://traffic.libsyn.com/podcastname/episode.mp3

我需要做的就是抓住它并开始播放,能够暂停和擦洗。首先,CAN AVPlayer流MP3是什么?如果是这样,是否有人可以指向我的任何指南或代码?如果没有,是否有任何类型的音频播放器类可以传输音频?

All I need to do is grab it and start playing, with the ability to pause and scrub. So first things first, CAN AVPlayer stream mp3's? And if so, does anybody have any guides or code they can point me to? And if not, is there any kind of audio player class that can stream audio?

我尝试创建一个AVPlayerItem,用URL初始化,然后将其添加到AVPlayer,但我收到了大量错误加载...和符号找不到......错误。我很感激有关这方面的任何信息,谢谢!

I've tried creating an AVPlayerItem, initialized with the URL, then adding it to an AVPlayer, but I'm getting a ton of Error Loading... and Symbol Not Found... errors. I'd appreciate any information on this, thank you!

推荐答案

试试这个

 -(void)playselectedsong{

        AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]];
        self.songPlayer = player;
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(playerItemDidReachEnd:)
                                                     name:AVPlayerItemDidPlayToEndTimeNotification
                                                   object:[songPlayer currentItem]];
        [self.songPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];



    }
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

        if (object == songPlayer && [keyPath isEqualToString:@"status"]) {
            if (songPlayer.status == AVPlayerStatusFailed) {
                NSLog(@"AVPlayer Failed");

            } else if (songPlayer.status == AVPlayerStatusReadyToPlay) {
                NSLog(@"AVPlayerStatusReadyToPlay");
                [self.songPlayer play];


            } else if (songPlayer.status == AVPlayerItemStatusUnknown) {
                NSLog(@"AVPlayer Unknown");

            }
        }
    }

    - (void)playerItemDidReachEnd:(NSNotification *)notification {

     //  code here to play next sound file

    }

这篇关于使用AVPlayer流式传输mp3音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆