i am trying to develop a small app which should stream audio from a particular link. I am using AVPlayer for that but i dont know why its not working. Tried google but nothing seems to be relevant there. Kindly Help.. thank you
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://108.166.161.206:8826/stream.mp3"];
self->playerItem = [AVPlayerItem playerItemWithURL:url];
self->player = [AVPlayer playerWithPlayerItem:playerItem];
self->player = [AVPlayer playerWithURL:url];
[player play];
}
don`t know why you first init your player with a playeritem and after that init it with an NSURL. Anyways, here is the way to go:
-(void) viewDidLoad{
player = [[AVPlayer alloc] initWithURL:
[NSURL URLWithString:@"http://xxx/yourfile.mp3"]];
[player addObserver:self forKeyPath@"status" options:0 context:nil];
}
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (object == player && [keyPath isEqualToString:@"status"]) {
if (player.status == AVPlayerStatusReadyToPlay) {
[player play];
}
if (player.status == AVPlayerStatusFailed) {
NSLog(@"Something went wrong: %@", player.error);
}
}
}
The AV Foundation Programming Guide should be a good start point to find out how the AVPlayer stuff works
this line isn't necessary self->player = [AVPlayer playerWithURL:url];
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://108.166.161.206:8826/stream.mp3"];
self->playerItem = [AVPlayerItem playerItemWithURL:url];
self->player = [AVPlayer playerWithPlayerItem:playerItem];
[player play];
}
also don't forget to add the NSAllowsArbitraryLoads key to info.plist
You can try this with AVAudioPlayer :
NSData *fileData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://108.166.161.206:8826/stream.mp3"]];
NSError *error = nil;
self.player = [[AVAudioPlayer alloc] initWithData:fileData error:&error];
[self.player play];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.