gpt4 book ai didi

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

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

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

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

但是,当您暂停应用程序(按下主页按钮)时,音乐会继续播放。如何在应用暂停时调用 [player setVolume:0.0] 并在恢复时调用 [player setVolume:1.0]

感谢所有帮助。

最佳答案

您可以使用 NSNotificationCenter 来收听 View Controller (或上述代码所在的任何对象)中的 UIApplicationWillResignActiveNotificationUIApplicationDidBecomeActiveNotification 通知暂停/继续播放你的声音在那里。

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

[player pause][player play]

关于ios - 如何在 iOS 中暂停应用程序时调用方法/代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27208071/

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