Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i play remote .mp3 file in my ios application?

I'm trying to play remote .mp3 file

but its giving the following error.

The operation couldn’t be completed. (OSStatus error -43.)

here is my code :

- (void)viewDidLoad
{
    [super viewDidLoad];

    isPlaying = NO;

/*   

 NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]


pathForResource:@"Dar-e-Nabi-Per" ofType:@"mp3"]];

*/

    NSURL *url = [NSURL 

URLWithString:@"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];

    NSError *error;

    audioPlayer = [[AVAudioPlayer alloc]

                   initWithContentsOfURL:url

                   error:&error];

    if (error)

    {

        NSLog(@"Error in audioPlayer: %@",


              [error localizedDescription]);

 }

 else

 {

   audioPlayer.delegate = self;


   [audioPlayer prepareToPlay];


    }


}

Can anyone please guide me where i'm doing mistake

like image 532
zeeshan shaikh Avatar asked Dec 05 '22 10:12

zeeshan shaikh


1 Answers

For steaming audio from a remote server, use AVPlayer instead of AVAudioPLayer. AVPlayer Documentation

Sample Code:

- (void)playSampleSong:(NSString *)iSongName {
    NSString *aSongURL = [NSString stringWithFormat:@"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
    // NSLog(@"Song URL : %@", aSongURL);

    AVPlayerItem *aPlayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:aSongURL]];
    AVPlayer *anAudioStreamer = [[AVPlayer alloc] initWithPlayerItem:aPlayerItem];
    [anAudioStreamer play];

    // Access Current Time
    NSTimeInterval aCurrentTime = CMTimeGetSeconds(anAudioStreamer.currentTime);

    // Access Duration
    NSTimeInterval aDuration = CMTimeGetSeconds(anAudioStreamer.currentItem.asset.duration);
}
like image 60
Roshit Avatar answered Jan 09 '23 02:01

Roshit