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

查看:46
本文介绍了使用 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.

我尝试流式传输的音频是来自 libsyn 服务器的相当大的 mp3 文件,其 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

我需要做的就是抓住它并开始播放,并具有暂停和擦洗功能.那么首先,可以 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天全站免登陆