0

I would like to add some background music to the splash screen in the iPhone. I have already increased the splash screen time by few seconds.. And the music isn't long... But I am not sure how to do it? Need some guidance...

4

3 回答 3

5

Use following function for playing sound file.

-(void)playSoundFromFileName:(NSString*)pstrFileName ofType:(NSString*)pstrFileType
{   
    SystemSoundID bell;
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:pstrFileName ofType:pstrFileType]], &bell);  
    AudioServicesPlaySystemSound (bell);    
}

Call this function in application delegate didfinishlaunchingwithoptions method...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
     ...
     [self playSoundFromFileName:@"yourfile" ofType:@"extension"];

}
于 2012-07-03T11:39:53.910 回答
5

my personal taste: don't bother your users, and get out of the splash as fast as you can

于 2012-07-03T11:42:37.853 回答
2

How about putting it in you application didFinishLaunching but be sure to instantiate it in you .h and .m.

Something like this should do your problem:

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

    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    resourcePath = [resourcePath stringByAppendingString:@"/YOURMUSICNAME.mp3"];
    NSLog(@"Path to play: %@", resourcePath);
    NSError* err;

    //Initialize our player pointing to the path to our resource
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:
                 [NSURL fileURLWithPath:resourcePath] error:&err];

    if( err ){
        //bail!
        NSLog(@"Failed with reason: %@", [err localizedDescription]);
    }
    else{
        //set our delegate and begin playback
        player.delegate = self;
        [player play];
        player.numberOfLoops = -1;
        player.currentTime = 0;
        player.volume = 1.0;
    }
}

Then if you want to stop it:

[player stop];

or pause it :

[player pause];

and also import it in your header file:

#import <AVFoundation/AVFoundation.h>

and add this to your header the bold part:

@interface ViewController : UIViewController <AVAudioPlayerDelegate>

于 2012-07-03T11:46:18.487 回答