I'm implementing a basic audio player in order to play remote audio files. Files are in format mp3. The code I wrote is working fine on the simulator but doesn't work on a real device. However the same url I use within my app works fine if I load it by using safari (on the same real device) so I'm not really getting the missing point. Below is my code:
self.musicPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:urlTrack]];
[self.musicPlayer play];
something extremely easy. The music player property is defined as
@property (nonatomic, retain) AVPlayer *musicPlayer;
I also tried using an AVPlayerItem but the result is the same. Here is the code I have used
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:urlTrack]];
self.musicPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[self.musicPlayer play];
Finally I tried to use the code below
self.musicPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:urlTrack]];
NSLog(@"Player created:%d",self.musicPlayer.status);
[self.musicPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"Player created:%d",self.musicPlayer.status);
if (object == self.musicPlayer && [keyPath isEqualToString:@"status"]) {
if (self.musicPlayer.status == AVPlayerStatusReadyToPlay) {
[self.musicPlayer play];
} else if (self.musicPlayer.status == AVPlayerStatusFailed) {
// something went wrong
}
}
}
When the method observeValueForKeyPath is invoked the player status is 1 and the play is exectuted but still not sound. I tried several files like:
http://www.nimh.nih.gov/audio/neurogenesis.mp3
http://www.robtowns.com/music/blind_willie.mp3
Any idea? Tnx