每当我打开iOS应用程序时如何播放声音

时间:2013-10-24 02:40:49

标签: ios objective-c

我尝试了这段代码,但没有用,

-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"paying with coins and bills" ofType:@"wav"];
AudioServicesCreateSystemSoundID(CFBridgingRetain([NSURL fileURLWithPath:soundPath]), &(soundID));
AudioServicesPlaySystemSound (soundID);

}

只有在我运行xcode项目时才会播放。

我的要求是,在我的ipad中打开我的应用程序时我需要播放声音。请帮帮我

1 个答案:

答案 0 :(得分:0)

您可以尝试将代码移至AppDelegate.m

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

当您的应用变为有效时,系统会调用此方法,这可能适用于您的情况。