在iPhone中为启动画面添加声音

时间:2012-07-03 11:36:41

标签: iphone audio

我想在iPhone的启动画面中添加一些背景音乐。我已经将启动画面时间增加了几秒钟..而且音乐时间不长......但我不知道该怎么做?需要一些指导...

3 个答案:

答案 0 :(得分:5)

使用以下功能播放声音文件。

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

在应用程序委托中调用此函数didfinishlaunchingwithoptions方法......

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

}

答案 1 :(得分:5)

我的个人品味:不要打扰你的用户,尽可能快地摆脱困境

答案 2 :(得分:2)

如何将它放在你application didFinishLaunching中,但一定要在你.h和.m中实例化它。

这样的事情可以解决你的问题:

- (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;
    }
}

然后,如果你想阻止它:

[player stop];

或暂停:

[player pause];

并将其导入头文件中:

#import <AVFoundation/AVFoundation.h>

并将此标题添加到粗体部分:

@interface ViewController:UIViewController&lt; AVAudioPlayerDelegate &gt;