gpt4 book ai didi

ios - 延迟背景音乐开始

转载 作者:行者123 更新时间:2023-11-29 03:25:33 25 4
gpt4 key购买 nike

我正在研究一个应用程序并添加我想要的功能,但在进行过程中遇到了一些问题。

我希望有人能回答这个问题。

我想在我的应用程序中播放一些背景音乐,我已经通过在我的 AppDelegate 中使用这段代码实现了这一点

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


NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/smb1-1.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 = 0.5;

}

// Override point for customization after application launch.
return YES;
}

所以一切都很好。但是,我希望音频在应用程序完成启动后 3 秒开始播放,而不是立即开始播放。

有谁知道我如何对此设置延迟或解释不同的方式。我不会在每次音乐修复之前都有延迟,只是为了第一次启动。

提前致谢。

最佳答案

希望对您有所帮助:

  1. 在您的 appDelegate 中创建一个 BOOL iVar:

    @implementation aaaAppDelegate        
    {
    BOOL firstTime;
    }
  2. 将此添加到您的 didFinishLaunchingWithOptions 方法中:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    // Override point for customization after application launch.
    firstTime = YES;
    return YES;
    }
  3. 将此添加到 appDelegate 中的 applicationDidBecomeActive 方法。

        - (void)applicationDidBecomeActive:(UIApplication *)application
    {
    if (firstTime)
    {
    firstTime = NO;
    [self performSelector:@selector(playMusic) withObject:nil afterDelay:3.0];
    }
    }
  4. 将您的音乐播放代码从 ​​didFinishLaunchingWithOptions 移至名为 playMusic 的单独方法:

     -(void)playMusic
    {
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    resourcePath = [resourcePath stringByAppendingString:@"/smb1-1.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 = 0.5;

    }
    }

关于ios - 延迟背景音乐开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20499822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com