ios - How to get audio to play from an online url on iPhone -
this question has answer here:
i know it's been asked before plenty of people, have no idea why isn't working in application in x-code , objective c!
basically, want play audio in application url. using testing audio apple site, code is:
nsstring *playstring = @"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"; //converts songurl playable nsurl nsurl *playurl = [nsurl urlwithstring:playstring]; avaudioplayer *backgroundmusicplayer = [[avaudioplayer alloc] initwithcontentsofurl:playurl error:&error]; if (backgroundmusicplayer == nil) { nslog(@"%@", [error description]); } else { [backgroundmusicplayer preparetoplay]; [backgroundmusicplayer play]; } it comes following error:
error domain=nsosstatuserrordomain code=-43 "the operation couldn’t completed. (osstatus error -43.)
and can't find when google it. ideas? should try audio stream? or have example works? because using iphone simulator not actual iphone?
edit:
please note - when url mean actual website, "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8" don't want have download song first, want able play downloading! thanks
use avplayer play mp3 file url
-(void)playselectedsong{ avplayer *player = [[avplayer alloc]initwithurl:[nsurl urlwithstring:urlstring]]; self.songplayer = player; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(playeritemdidreachend:) name:avplayeritemdidplaytoendtimenotification object:[songplayer currentitem]]; [self.songplayer addobserver:self forkeypath:@"status" options:0 context:nil]; [nstimer scheduledtimerwithtimeinterval:0.1 target:self selector:@selector(updateprogress:) userinfo:nil repeats:yes]; [self.songplayer play]; } - (void)observevalueforkeypath:(nsstring *)keypath ofobject:(id)object change:(nsdictionary *)change context:(void *)context { if (object == songplayer && [keypath isequaltostring:@"status"]) { if (songplayer.status == avplayerstatusfailed) { nslog(@"avplayer failed"); } else if (songplayer.status == avplayerstatusreadytoplay) { nslog(@"avplayerstatusreadytoplay"); } else if (songplayer.status == avplayeritemstatusunknown) { nslog(@"avplayer unknown"); } } } - (void)playeritemdidreachend:(nsnotification *)notification { // code here play next sound file }
Comments
Post a Comment