AVPlayer plays on simulator but doesn't on a real device

风流意气都作罢 提交于 2019-12-11 02:09:52

问题


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


回答1:


Check the spelling of your filename. The device is case-sensitive, the simulator is not...

Also, check if your ringer is off, you won't hear any sound when it's off. To prevent that, use

NSError *_error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &_error];

right before where you init the player



来源:https://stackoverflow.com/questions/17617066/avplayer-plays-on-simulator-but-doesnt-on-a-real-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!