gpt4 book ai didi

ios - iOS上相同声音的多个实例

转载 作者:行者123 更新时间:2023-12-03 02:34:17 26 4
gpt4 key购买 nike

我在做一个泡泡游戏,碰到一点障碍:/
我有声音(pop.mp3),每次听到气泡之一时,都需要播放此声音
所有气泡都是调用相同方法的按钮(-(IBACTION)bubblePop)
我在Viewdidload方法中初始化了AVAudioPlayer
并在bubblePop方法中调用它的播放功能
这显然是行不通的,但是因为气泡的流行声音一次只能播放一次,所以它不会像我期望的那样重叠
有谁知道我该如何解决?
额外信息
如果我在bubblePop中初始化音频播放器,我什么都听不到

最佳答案

您应该在bubblePop中初始化AVPlayer并对其保持强烈引用。
试试这个:

@interface ViewController()
@property (strong, nonatomic) NSMutableDictionary *players;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.players = [[NSMutableDictionary alloc] init];
}

- (void)bubblePop {
NSURL *URL = your sound URL;

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:URL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
self.players[playerItem] = player;

[player play];
}


-(void)itemDidFinishPlaying:(AVPlayerItem *)sender {
[self.players removeObjectForKey:sender];
}
@end

关于ios - iOS上相同声音的多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19610439/

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