应用加载时播放声音

时间:2011-03-04 05:16:48

标签: iphone ios4 iphone-sdk-3.0 audio

尝试在应用加载时播放声音

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    //sleep(2);
    [NSThread sleepForTimeInterval:2.0];

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"austinpowers", CFSTR("wav"), NULL); 

    if (soundFileURLRef) {
        CFStringRef url = CFURLGetString(soundFileURLRef);
    }
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);



    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

它播放声音,但仅在加载壁纸(default.png)消失时才播放。 如果有人可以帮助我,或指出我正确的方向

感谢

2 个答案:

答案 0 :(得分:3)

不要使用Default.png,而是创建自定义初始屏幕视图控制器,您可以在其中显示图像和播放声音。

答案 1 :(得分:0)

通过在AppDelegate.m文件中执行以下操作,我解决了这个问题

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {

   NSURL *clip = [[NSBundle mainBundle] URLForResource: @"intro" withExtension:@"caf"];
   self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
   [self.startupPlayer play]; 

}

这对你也有用。