0

I have checked through many solutions here, and it seems to boil down to these 2 ways of playing remote MP3:

Written in Swift (since all the answers are in Objective-C):

    let url = "http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"
    let playerItem = AVPlayerItem(URL:NSURL(string:url))
    let player = AVPlayer(playerItem:playerItem)
    player.play()

or

    let fileData = NSData(contentsOfURL:NSURL(string:"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"))    
    let audioPlayer = AVAudioPlayer(data:fileData, error:nil)       
    audioPlayer.play()

But neither seems to work. Am I missing anything?

Viper
  • 1,408
  • 9
  • 13
Lim Thye Chean
  • 8,704
  • 9
  • 49
  • 88

1 Answers1

-1

Play the file to convert in NSData.

 NSData  *audioData = [NSData dataWithContentsOfFile:filename];
 AVAudioPlayer *audio= [[AVAudioPlayer alloc] initWithData:audioData error:nil];
 self.player = audio;
 [[self player] play];
modusCell
  • 13,151
  • 9
  • 53
  • 80
Shubham Narang
  • 514
  • 2
  • 14
  • 1
    Isn't this the 2nd way I did it (I just did it in Swift)? – Lim Thye Chean Aug 02 '14 at 09:21
  • Does AVAudioPlayer work for streaming audio, tried this code but can't make it work yet. – danielsalare Sep 27 '14 at 14:15
  • This was exactly the problem I was having. AVAudioPlayer is only working when converting to NSData. – Billy Coover Oct 03 '14 at 22:49
  • NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:streaming_path]]; NSError *error; AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error]; audioPlayer.numberOfLoops = 0; audioPlayer.volume = 1.0f; [audioPlayer prepareToPlay]; [audioPlayer play]; [[self player] play]; – Shubham Narang Oct 07 '14 at 14:36