ios - Looping .wav file -
i making alarm app iphones , want continuously loop audio until button pressed again. of play audio once when pressed. here's code:
-(ibaction)playaudiobutton:(id)sender { audioservicesplaysystemsound(playsoundid); } - (void)viewdidload { nsurl *soundurl = [nsurl fileurlwithpath:[[nsbundle mainbundle] pathforresource:@"sound" oftype:@"wav"]]; audioservicescreatesystemsoundid((__bridge cfurlref)soundurl, &playsoundid); [super viewdidload]; // additional setup after loading view, typically nib. }
any suggestions?
use avaudioplayer play sound. must add avfoundation.framework
project work. start declaring avaudioplayer
object. must declared either property strong
attribute, e.g.
@property (strong, nonatomic) avaudioplayer *audioplayer;
or instance variable __strong
attribute
@interface class : superclass //or @implementation class { avaudioplayer __strong *audioplayer; }
then, load , play file,
- (void)viewdidload { nsstring *audiofilepath = [[nsbundle mainbundle] pathforresource:@"sound" oftype:@"wav"]; nsurl *audiofileurl = [nsurl fileurlwithstring:audiofilepath]; audioplayer = [[avaudioplayer alloc] initwithcontentsofurl:audiofileurl error:nil]; audioplayer.numberofloops = -1; //plays indefinitely [audioplayer preparetoplay]; } - (ibaction)playaudiobutton:(id)sender { if ([audioplayer isplaying]) [audioplayer pause]; //or "[audioplayer stop];", depending on want else [audioplayer play]; }
and, when want stop playing sound, call
[audioplayer stop];
Comments
Post a Comment