gpt4 book ai didi

ios - 重叠的声音

转载 作者:行者123 更新时间:2023-11-28 20:44:07 25 4
gpt4 key购买 nike

为什么我的音乐重叠?有什么办法可以一一播放吗?

- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *array = [NSMutableArray array];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"Master of Puppets" ofType:@"mp3"] atIndex:0];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"] atIndex:1];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"Start" ofType:@"caf"] atIndex:2];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"thx" ofType:@"m4v"] atIndex:3];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"aif"] atIndex:4];

for (int i=0; i < [array count]; i++)
{
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[array objectAtIndex:i]] error:NULL];
theAudio.delegate = self;
[theAudio prepareToPlay];
[theAudio play];
}

在 .h 中:

@interface STAMP_AudioViewController : UIViewController <AVAudioPlayerDelegate>{
AVAudioPlayer* theAudio;
}

@end

最佳答案

实现来自 AVAudioPlayerDelegate 的 audioPlayerDidFinishPlaying 委托(delegate)然后从那里开始第二首歌......

NSMutableArray *array ;
int index = 0;

- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *array = [NSMutableArray array];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"Master of Puppets" ofType:@"mp3"] atIndex:0];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"] atIndex:1];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"Start" ofType:@"caf"] atIndex:2];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"thx" ofType:@"m4v"] atIndex:3];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"aif"] atIndex:4];

theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[array objectAtIndex:index]] error:NULL];
theAudio.delegate = self;
[theAudio prepareToPlay];
[theAudio play];

index++;
}

现在播放器播放第一首歌曲..下面给定的代表将在歌曲完成后触发..然后播放第二首歌曲..记住你必须释放第一个播放器并初始化新的 AVAudioPlayer 并用第二首歌曲初始化..

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[theAudio release];

if(index > 4)//your array highest index
return;

theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[array objectAtIndex:index]] error:NULL];
theAudio.delegate = self;
[theAudio prepareToPlay];
[theAudio play];

index++;
}

这个逻辑应该可行..我在 AVAudioPlayer 中找不到任何函数来在初始化后更改歌曲..所以我们需要创建新对象..:(

关于ios - 重叠的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7188455/

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