gpt4 book ai didi

ios - 在 iOS 上没有任何延迟地一个接一个地播放声音

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

我正在尝试按特定顺序播放声音。我设法做到了,但声音之间有微小的延迟。以彼此适合的方式录制的声音,因此用户将不会注意到音乐已经改变。

最重要的是,我必须避免的声音之间存在延迟..

我不会把所有的声音都集中到一个长的声音中,因为我需要在不同的时间开始不同的声音。

也许有比使用 AVAudioPlayer 更好的方法?

这是我的代码:

-(id)init {
self = [super init];
if (self) {
gameMusicSounds = @[gamePlaySound1,gamePlaySound2,gamePlaySound3,gamePlaySound4,gamePlaySound5,gamePlaySound6,gamePlaySound7,gamePlaySound8,gamePlaySound9,gamePlaySound10];
}
return self;
}

-(void)gameMusicNew {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
currentMusicToPlay = [[defaults objectForKey:CURRENT_GAME_MUSIC] intValue];

if (currentMusicToPlay >= [gameMusicSounds count]){
currentMusicToPlay = 0;
}

NSString * music = [[NSBundle mainBundle] pathForResource:[gameMusicSounds objectAtIndex:currentMusicToPlay] ofType:@"mp3"];
self.backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
self.backgroundMusic.numberOfLoops = 1;
self.backgroundMusic.delegate = self;
[self.backgroundMusic play];

currentMusicToPlay ++;
[defaults setObject:[NSString stringWithFormat:@"%i",currentMusicToPlay] forKey:CURRENT_GAME_MUSIC];
[defaults synchronize];

}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[self gameMusicNew];
}

----更新-----

我想与大家分享我写的最终代码,因为我实际上并没有在网上找到很多关于这个主题的东西。

基本上我有一组声音需要一个一个地播放,当它们播放完后,我需要重新开始。我不使用一个长的 mp3 文件的原因是,有时,如果用户返回应用程序并且他正在玩游戏,我希望他不要听他上次听的音乐。

代码如下:

 - (id)init {
self = [super init];
if (self) {
gameMusicSounds = @[gamePlaySound1,gamePlaySound2,gamePlaySound3,gamePlaySound4,gamePlaySound5,gamePlaySound6,gamePlaySound7,gamePlaySound8,gamePlaySound9,gamePlaySound10];
}
return self;
}

-(void)gameMusicNew{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString * isGameOver =[defaults objectForKey:IS_GAME_OVER];
currentMusic = [[defaults objectForKey:CURRENT_GAME_MUSIC] intValue];

if([isGameOver isEqualToString:@"YES"] || currentMusic >= [gameMusicSounds count]){
currentMusic = 0;
[defaults setObject:[NSString stringWithFormat:@"%i",currentMusic] forKey:CURRENT_GAME_MUSIC];
[defaults synchronize];
}

NSMutableArray* songs = [NSMutableArray arrayWithCapacity:[gameMusicSounds count]];

NSURL* url = [[NSBundle mainBundle] URLForResource:[gameMusicSounds objectAtIndex:currentMusic] withExtension:@"mp3"];
AVPlayerItem* item = [[AVPlayerItem alloc] initWithURL:url];
[songs addObject:item];

self.queue = [AVQueuePlayer queuePlayerWithItems:songs];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.queue currentItem]];

[self.queue play];

[self addNextMusic];
}

-(void)addNextMusic{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
currentMusic = [[defaults objectForKey:CURRENT_GAME_MUSIC] intValue];

int nextMusic = currentMusic + 1;
if(nextMusic >= [gameMusicSounds count]){
nextMusic = 0;
}

NSURL* url = [[NSBundle mainBundle] URLForResource:[gameMusicSounds objectAtIndex:nextMusic] withExtension:@"mp3"];
AVPlayerItem* item = [[AVPlayerItem alloc] initWithURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:item];
[self.queue insertItem:item afterItem:nil];
}

- (void)playerItemDidReachEnd:(NSNotification *)notification {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
currentMusic = [[defaults objectForKey:CURRENT_GAME_MUSIC] intValue];

currentMusic++;
if(currentMusic >= [gameMusicSounds count]){
currentMusic = 0;
}

[defaults setObject:[NSString stringWithFormat:@"%i",currentMusic] forKey:CURRENT_GAME_MUSIC];
[defaults synchronize];

[self addNextMusic];
}

-(void)stopGameMusic{
[self.queue removeAllItems];
}

希望它对某人有用。

最佳答案

您可以使用 AVQueuePlayer 来做到这一点。它接受多种声音并按顺序播放。

要使用它,请创建一个 AVPlayerItem 对象数组(使用 initWithURL: 创建指向您的声音的每个 AVPlayerItem),然后只需添加该数组并播放即可。

这是我的一个应用程序的实际代码。它完全符合您的描述,就好像所有的“歌曲”都是一首长长的“歌曲”:

NSMutableArray* songs = [NSMutableArray arrayWithCapacity:self.tiles.count];
for (NSString* fnam in self.songsSorted) {
NSURL* url = [[NSBundle mainBundle] URLForResource:fnam withExtension:@"m4a" subdirectory:@"Songs"];
AVPlayerItem* item = [[AVPlayerItem alloc] initWithURL:url];
[songs addObject:item];
}
self.queue = [AVQueuePlayer queuePlayerWithItems:songs];
[self.queue play];

关于ios - 在 iOS 上没有任何延迟地一个接一个地播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22538976/

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