[英]avplayer is not playing the URL
I am using avplayer for play audio url, but it is not working, I don't know where i am wrong 我正在使用avplayer播放音频网址,但它无法正常工作,我不知道我错在哪里
NSString *radioURL = @"https://www.example.com";
radioPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:radioURL]] ;
// [radioPlayer seekToTime:kCMTimeZero];
NSLog(@"radio player %@",radioPlayer.currentItem);
[radioPlayer play];
Any help would be appreciated. 任何帮助,将不胜感激。
I strongly recommended the code below to play radio streaming: please take a look also AVPlayer_Class 我强烈推荐下面的代码来播放无线电流:请看看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
}
Ref link is - Streaming mp3 audio with AVPlayer 参考链接是 - 用AVPlayer流式播放mp3音频
I had the same issue and just realized that I wasn't retaining the player (using ARC)! 我有同样的问题,只是意识到我没有保留播放器(使用ARC)! So it gets deallocated and stop playing immediately after start.
所以它被取消分配并在开始后立即停止播放。
You need to make sure that you have a strong property radioPlayer and use self.radioPlayer instead of radioPlayer. 您需要确保拥有强大的属性radioPlayer并使用self.radioPlayer而不是radioPlayer。
You are doing good but your method need to correct on one place.Check using the below method: 你做得很好,但你的方法需要在一个地方纠正。使用以下方法检查:
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];
and add proper delegate methods for AVAudioPlayer. 并为AVAudioPlayer添加适当的委托方法。
Did you forgot to set the attribute App Transport Security Settings -> Allow Arbitrary Loads (Yes)
? 您是否忘记设置属性
App Transport Security Settings -> Allow Arbitrary Loads (Yes)
?
Make Sure URL is AddingPercentEscapes
. 确保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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.