objective c - Sound not playing in iOS Simulator. No errors -
i have constructed code supposed play audio when button clicked. there no errors in code, when open ios simulator , press button, no sound plays. button tap animation, nothing happens. have both avfoundation , audiotoolbox frameworks. have audio file in resources. not using breakpoints. button set use 'playaudio:' first responder upon touch inside.
here 'viewcontroller.h' file:
#import <uikit/uikit.h> #import <audiotoolbox/audiotoolbox.h> #import <avfoundation/avfoundation.h> @interface viewcontroller : uiviewcontroller @property (strong, nonatomic) avaudioplayer *audioplayer; - (ibaction)playaudio:(id)sender; @end
and here 'viewcontroller.m' file:
#import "viewcontroller.h" #import <avfoundation/avfoundation.h> #import <audiotoolbox/audiotoolbox.h> @interface viewcontroller () @end @implementation viewcontroller - (ibaction)playaudio:(id)sender { avaudioplayer *audioplayer; nsstring *audiopath = [[nsbundle mainbundle] pathforresource:@"audiofile" oftype:@"wav"]; nsurl *audiourl = [nsurl fileurlwithpath:audiopath]; audioplayer = [[avaudioplayer alloc] initwithcontentsofurl:audiourl error:nil]; [audioplayer play]; sleep(5); }; -(void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end
the audio segment 3 seconds.
any appreciated! thanks!
i quite new ios had same problem , able fix it.
i declared avaudioplayer variable global variable , called
[audioplayer preparetoplay];
before
[audioplayer play];
just calling preparetoplay didnt fix it, had declare global variable work. not sure why makes difference simulator works now.
Comments
Post a Comment