gpt4 book ai didi

iphone - AVAudioSession 阻止系统音频

转载 作者:行者123 更新时间:2023-12-03 20:19:07 25 4
gpt4 key购买 nike

我正在编写一个播放背景音乐的应用程序,为此我使用 AVAudioSession ,如下所示:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"song" ofType:@"mp3"]];
player = [[AVAudioPlayer alloc] initWithData:data error:NULL];
player.delegate = self;
[player setVolume:1.0];
...
[player prepareToPlay];
[player play];

随后,用户可以选择禁用音乐。如果他们选择这样做,则会运行以下代码:

[[AVAudioSession sharedInstance] setActive:NO error:nil];
[player stop];

这成功地停止了音乐的播放。但是,如果启动音乐应用程序(或其他应用程序,例如 Spotify)中的音乐,然后切换到该应用程序,则用户的音乐将淡出并暂停。我的应用程序的音乐将无法播放,因为它仍然处于禁用状态,因此用户将保持沉默。

如何防止我的应用暂停用户的音乐?

最佳答案

当用户选择删除音频时,您必须执行以下操作:

[player stop];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

关于iphone - AVAudioSession 阻止系统音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15821000/

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