In my application I have a uitabbar contoller for managing the three view controllers. now I want to add the sound if the user clicked any one of the tab. Source code:
// Declare all three view controllers
nextview *con = [[nextview alloc]init];
nextview1 *con1= [[nextview1 alloc] init];
nextview2 *con2 = [[nextview2 alloc] init];
//declare tab barcontroller
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 480);
// Set each tab to show an appropriate view controller
[tabBarContr开发者_StackOverflowoller setViewControllers:[NSArray arrayWithObjects:con,con1,con2, nil]];
Set the delegate of the UITabBarController
and use tabBarController:didSelectViewController:
(info here) to play your sound. Playing a sound on iOS is somewhat tricky, but Google is your friend.
track the tabbar button in delegate - tabBarController:didSelectViewController:
and play sound with AVAudioPlayer .. code here -
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
精彩评论