4

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

Mario Catena
  • 575
  • 1
  • 8
  • 20

2 Answers2

17

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

Swissdude
  • 3,486
  • 3
  • 35
  • 68
  • I tried as per your suggestion, adding these two lines before my player init and no issue. Right now I tried on my personal iPhone and a different network than the iPad and the network I'm facing this issue. At least I can say the code works fine. I will do the same test on the iPad where I'm working on. But I don't understand why, when I try this same code on this iPad i'm using for development it doesn't work but the same mp3 url works trying on safari (always on the iPad). – Mario Catena Jul 14 '13 at 08:29
  • I think I found it - you're not allocating your player, so it gets released right away. Try `self.musicPlayer = [[AVPlayer alloc] initWithContentsOfURL: [NSURL URLWithString:urlTrack]];` - Also, add `[self.musicPlayer prepareToPlay]` right before `[self. musicPlayer play]` – Swissdude Jul 14 '13 at 10:45
  • The issue is solved with the line that you posted that I missed in my code. By adding this it's working fine. Tnx :) – Mario Catena Jul 16 '13 at 09:37
  • My ringer was off! Grrrrrr! I want two hours of my life back. – Brad Mar 14 '15 at 01:55
1

i have tried all the things but did not work, the only think that work for me AVAudioSession to playBack
here is the code i used

private var audioPlayer: AVAudioPlayer!

guard let path = Bundle.main.path(forResource: name, ofType: "mp3") else {
        print("can not find path")
        return
    }
    let url = URL(fileURLWithPath: path)
    do {
        try AVAudioSession.sharedInstance().setCategory(.playback)
        audioPlayer = try AVAudioPlayer(contentsOf: url)
    } catch {
        print("some thing went wrong: \(error)")
    }
    audioPlayer!.prepareToPlay()
    audioPlayer!.play()

please do not ignore the line in the above code
try AVAudioSession.sharedInstance().setCategory(.playback)

Sultan Ali
  • 2,497
  • 28
  • 25