AVPlayer无法播放URL。

9
我正在使用AVPlayer播放音频网址,但它无法工作,我不知道哪里出了问题。
NSString *radioURL = @"https://www.example.com";

radioPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:radioURL]] ;
// [radioPlayer seekToTime:kCMTimeZero];
NSLog(@"radio player %@",radioPlayer.currentItem);
[radioPlayer play];

任何帮助都会受到欢迎。


1
我认为URL必须有一个扩展名,例如https://example.com/myfile.mp3。 - Nitin Gohel
radioURL 不是一个可以播放的有效文件。 - Akshit Zaveri
谢谢回复,但是如果我在浏览器或其他地方播放它,URL就可以正常工作。 - Prachi Rajput
展示真实的URL,而不是example.com - Akshit Zaveri
使用AVPlayer检查此链接 http://stackoverflow.com/questions/15850447/how-to-get-audio-to-play-from-an-online-url-on-iphone,否则您可以使用AVAudioPlayer。 - Nitin Gohel
获得响应“AVPlayerStatusReadyToPlay”,但音频未播放。 - Prachi Rajput
5个回答

11

我强烈推荐以下代码来播放广播流:请同时查看AVPlayer_Class

 -(void)Play{
        NSString *radioURL = @"https://www.example.com"; //this url must valid 
        AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:radioURL]];
        self.songPlayer = player;    //self.songPlayer is a globle object of avplayer
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(playerItemDidReachEnd:)
                                                     name:AVPlayerItemDidPlayToEndTimeNotification
                                                   object:[songPlayer currentItem]];
        [self.songPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
}


- (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音频


2
收到 AVPlayerStatusReadyToPlay 的响应,但音频没有播放 :( - Prachi Rajput
2
你是在设备上还是模拟器上测试的?如果是设备,请检查 iPhone 是否处于静音模式。 - Nitin Gohel
请问您能分享一个流媒体的链接吗? - Nitin Gohel
@NitinGohel,上面的代码对我不起作用。iPhone没有静音模式。有什么想法吗? - Pramuka Dias
我需要为HTTPS做些什么? - Devang Goswami
显示剩余3条评论

7

我曾经遇到过同样的问题,并且刚刚意识到我没有保留播放器(使用ARC)!因此,它在开始后立即被释放并停止播放。

你需要确保你有一个强属性radioPlayer并使用self.radioPlayer而不是radioPlayer。


@ Borzh,我做了和你一样的事情。在我的情况下,我得到了AVPlayerStatusReadyToPlay的响应,但是音频没有播放。请尽快回复。谢谢。 - Ravi
@Monu Singh,可能你有其他问题,我只是说了我自己遇到的问题。 - Borzh

-1
你做得很好,但是你的方法需要在一个地方进行更正。请使用以下方法进行检查:
NSURL *audioURL = [NSURL URLWithString:radioURL];
NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:nil];
self.audioPlayer.numberOfLoops = -1;
self.audioPlayer.delegate = self;
[self.audioPlayer prepareToPlay];
self.audioPlayer.volume=1.0;
[self.audioPlayer autorelease];
[self.audioPlayer Play];

并为AVAudioPlayer添加适当的委托方法。


No need to Convert to NSData - Toseef Khilji

-1

确保 URL 使用 AddingPercentEscapes

NSURL *audioURL = // Your Url;
NSString *encodedString = [@"Your Url" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *myURL = [[NSURL alloc] initWithString:encodedString]
AVPlayer *player = [AVPlayer playerWithURL:myURL];
[player prepareToPlay];
player play];

奇怪...prepareToPlay是从哪里来的?在AVPlayer中吗?我似乎在.h源文件中找不到它了,难道被移除了吗? - NFerocious
由于它是 AVAudioPlayer 上的方法而不是 AVPlayer,所以很可能是错误的。 - Jonas

-1

你是否忘记设置属性 App Transport Security Settings -> Allow Arbitrary Loads (Yes)

App Transport Security Settings


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接