Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当应用程序在iOS中暂停时如何调用方法/代码_Ios_Objective C_Xcode - Fatal编程技术网

当应用程序在iOS中暂停时如何调用方法/代码

当应用程序在iOS中暂停时如何调用方法/代码,ios,objective-c,xcode,Ios,Objective C,Xcode,如标题所示;我目前正在使用以下代码在循环中播放声音文件: NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"smoothjazz" ofType:@".mp3"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileU

如标题所示;我目前正在使用以下代码在循环中播放声音文件:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"smoothjazz" ofType:@".mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
NSFileManager *filemgr  = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath:soundFilePath])
{
    // Your code to play the sound file
    [player setVolume:1.0];
    player.numberOfLoops = 5; //infinite

    [player play];
}  
但是,当你暂停应用程序(点击home按钮)时,音乐会一直播放。当应用程序暂停时如何调用
[player setVolume:0.0]
,当应用程序恢复时如何调用
[player setVolume:1.0]


感谢您的帮助。

您可以使用
NSNotificationCenter
来收听视图控制器(或上述代码所在的任何对象)中的
UIApplicationWillResignActivityNotification
UIApplicationIDBecomeActivityNotification
通知以及播放暂停/恢复的声音

您可能不应该设置播放机的音量。打电话可能会更好


[player pause]
[player play]
有一个处理状态更改的协议:
uiapplicationelegate
。您现在感兴趣的是
willResignActive
didBecomeActive

请注意,它们不是
dienterbackground
willEnterForeground
。区别在于前者会在应用程序接管时受到打击,比如Siri,而后者则不会

您可以在audio manager类中实现此协议,并在该点设置音量,如下所示:

- (void)applicationWillResignActive:(UIApplication *)application {
   self.currentVolume = self.player.volume;
   self.player.volume = 0;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
   self.player.volume = self.currentVolume;
}
可能不需要存储当前卷,但在其上设计为现在这样使用将允许您在将来实现它

你应该考虑你是否想静默球员或者暂停比赛。我相信你有,但一般更好的做法是暂停,而不是静音,但这并不是适用于所有情况


此外,您应该知道,一些程序员的思想流派认为只有AppDelegate才能实现UIApplicationLegate协议。对此有一些很好的论据,就我个人而言,我还没有决定什么是最佳实践,但如果你想遵循这一点,那么你可以在AppDelegate中设置一个协议来委托这些,并让你的音频管理器实现该委托,或者你可以使用NSNotificationCenter向任何听众广播该事件-因此,您的音频管理器在这种情况下。在这两种方法中,我认为使用通知是一种更干净的处理方法(委托给代理对我来说有点愚蠢),但如果你不小心,它们也会变得一团糟。

以下是我添加到viewDidLoad中的代码,用于在应用程序暂停时调用该方法:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(muteMusic)
                                             name:UIApplicationWillResignActiveNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(activateMusic)
                                             name:UIApplicationDidBecomeActiveNotification object:nil];
当然,我有两种方法:

- (void) muteMusic {
[player pause];
}

- (void) activateMusic {
    [player play];
}  

享受吧

这成功了!我将在我的答案中包含我的代码-非常感谢:)我最终使用了NSNotificationCenter,但感谢您提供的所有信息!在我继续学习编码的过程中会有很大帮助:)祝你有愉快的一天!