开发者

iPhone play sound on splash screen

开发者 https://www.devze.com 2023-02-10 00:09 出处:网络
I just want to play a little ding on when the splash screen loads.I\'m guesing that goes in the RootViewControler.m under viewDidLoad.As for playing the audio file I\'m googled and found a lot of thin

I just want to play a little ding on when the splash screen loads. I'm guesing that goes in the RootViewControler.m under viewDidLoad. As for playing the audio file I'm googled and found a lot of things I haven't been able to make work.

What type of audio file should it be? wav mp3 etc. or does it matter.

I'm not sure what ofType@"caf" is, but here is what I have found, when I try it I get AVAudioplayer undeclared (which I'm thinking is a total newb error but don't know):

// Implement viewDidLoad to do additional setup afte开发者_如何学Pythonr loading the view, typically from a nib.
- (void)viewDidLoad {
    NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
                                               pathForResource:@"click"
                                               ofType:@"caf"]];
    AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
    [click play];
    [click release];
    [super viewDidLoad];
}

Thank you for any help, R


From the Apple Documentation : pathForResource:ofType:

Returns the full pathname for the resource identified by the specified name and file extension.

  - (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension

The program looks for a file called click.caf in your main bundle. The warning about AVPlayer being undeclared means that you have to add the AVFoundationFramework to your project and import the proper .h - file with #import <AVFoundation/AVAudioPlayer.h>.


I think the problem is that you haven't added the AVFoundation framework. Once you add the AVFoundation.framework, you'll need to #import <AVFoundation/AVAudioPlayer.h> and hopefully it will work.

0

精彩评论

暂无评论...
验证码 换一张
取 消