gpt4 book ai didi

ios - 在钢琴应用程序中演奏声音的最佳方式是什么?

转载 作者:行者123 更新时间:2023-11-29 03:51:35 27 4
gpt4 key购买 nike

我正在开发一个类似钢琴的应用程序并尝试了这两个库:

AudioToolbox/AudioToolbox.h

-(void)playSound :(NSString *)fName :(NSString *) ext{
SystemSoundID audioEffect;
NSString *path = [[NSBundle mainBundle] pathForResource : fName ofType :ext];
if ([[NSFileManager defaultManager] fileExistsAtPath : path]) {
NSURL *pathURL = [NSURL fileURLWithPath: path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
}
else {
NSLog(@"error, file not found: %@", path);
}
}

然后我就用类似的东西来调用它

[self playSound:@"c_sharp" :@"mp3"];

这种方法的问题是,当我点击调用 playSound 方法的按钮时,会出现 1-2 秒的延迟。此外,AudioServicesPlaySystemSound 似乎在系统音量上播放,如果禁用该选项,则无法通过音量摇杆控制。


我还尝试了不同的方法来播放声音

AVFoundation/AVFoundation.h

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
-(void)viewDidLoad
{
[super viewDidLoad];

NSBundle *mainBundle = [NSBundle mainBundle];
NSString *fileC2 = [mainBundle pathForResource:@"c_sharp" ofType:@"mp3"];
NSData *soundC2 = [NSData dataWithContentsOfFile:fileC2];
NSError *error = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:soundC2
error:&error];
[self.audioPlayer prepareToPlay];
}

然后要播放声音,我会

- (IBAction)c_sharp_press:(UIButton *)sender {
[self.audioPlayer play];
}

这种方法的问题是我一次只能播放一个声音。如果我敲击同一个钢琴键,而不是添加另一个类似的声音,那么在第一个声音播放完毕之前它不会执行任何操作。还有 1/2 秒的小延迟。


我的问题是:

我使用什么库来实现钢琴键盘,以便:

  1. 播放声音时没有或不明显的延迟。
  2. 声音由音量摇杆控制。
  3. 我一次可以播放多种声音。

最佳答案

查看 Apple loadPresetDemo 的示例代码。此代码演示了如何将 AUSampler Unit 与自定义音频剪辑结合使用。这是制作乐器类型应用程序的最佳方法,因为 AUSampler 针对响应式播放、低延迟、内存预加载和使用等进行了优化。在 iOS 中播放音频的其他方法不太适合快速连续地播放短促的声音。

关于ios - 在钢琴应用程序中演奏声音的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17004647/

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