avplayer没有播放该网址

时间:2014-03-06 06:07:13

标签: ios avplayer

我正在使用avplayer播放音频网址,但它无法正常工作,我不知道我错在哪里

NSString *radioURL = @"https://www.example.com";

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

任何帮助都将不胜感激。

5 个答案:

答案 0 :(得分: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

    }

参考链接是 - Streaming mp3 audio with AVPlayer

答案 1 :(得分:6)

我遇到了同样的问题,并且意识到我并非保留播放器(使用ARC)!所以它被取消分配并在开始后立即停止播放。

您需要确保拥有强大的属性 radioPlayer 并使用 self.radioPlayer 而不是radioPlayer。

答案 2 :(得分:-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添加适当的委托方法。

答案 3 :(得分:-1)

您是否忘记设置属性6499 == 2644 + 3855

App Transport Security Settings

答案 4 :(得分:-2)

确定网址为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];