ios - playing sound and changing image in UIImageView when button is pressed -
as title describes i've been able add sound when button pressed, wondering if there way add image change main uiimageview when same button pressed. there way stick picture code in "playfaceofaudiotype" method presented?
here's code far:
#import "fourthviewcontroller.h" @interface fourthviewcontroller () @end @implementation fourthviewcontroller @synthesize audioplayer; - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization } return self; } - (void)viewdidload { [super viewdidload]; // additional setup after loading view. } - (ibaction)facesaudioaction:(id)sender { uibutton *btn=(uibutton *)sender; [self playfaceaudiooftype:btn.tag];//tag button set on xib.. } -(void)playfaceaudiooftype:(int)type{ [self stopaudio]; nsstring *sound=@""; switch (type) { case 1: sound=@"1"; break; case 2: sound=@"2"; break; case 3: sound=@"3"; break; case 4: sound=@"4"; break; case 5: sound=@"5"; break; default: break; } nsurl *url = [nsurl fileurlwithpath:[[nsbundle mainbundle] pathforresource:sound oftype:@"mp3"]]; nserror *error; if(url){ audioplayer = [[avaudioplayer alloc] initwithcontentsofurl:url error:&error]; if (error) { nslog(@"error in audioplayer: %@", [error localizeddescription]); } else { audioplayer.delegate = self; [audioplayer preparetoplay]; } [audioplayer play]; } } -(void)stopaudio{ if(audioplayer && [audioplayer isplaying]){ [audioplayer stop]; audioplayer=nil; } } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end
i understand looking around done doing if / else if type of approach wondering if possible include along switch statement bit.
you can use either facesaudioaction
or playfaceofaudiotype
method changing image.
also, can rid off switch statement with;
nsstring *sound = [nsstring stringwithformat:@"%d", type];
you can set image this;
nsstring *imagename = [nsstring stringwithformat:@"%d.jpg", type]; yourimageview.image = [uiimage imagenamed:imagename];
Comments
Post a Comment