1个按钮,播放10个声音?
问题描述:
1个按钮,播放10个声音? 如何获得按钮以便按顺序播放一些声音?1个按钮,播放10个声音?
如何为此操作添加额外的声音?
-(IBAction)sound1
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound1", CFSTR("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
答
我有最好的运气AVAudioPlayer - 这是一个叫AVFoundation库,您可以通过“构建阶段”导入(第一次点击左上方的蓝色Xcode项目名称),然后选择“链接二进制与图书馆“
那就试试这个非常简单的YouTube教程做一个按钮播放声音:
这是我用了2年前创造我的第一个音板相同的视频。 Xcode 5有点不同,但代码都可以工作。
好的,现在你需要创建一个这些声音的数组,它们将循环通过它们。从树屋看看这个链接:
https://teamtreehouse.com/forum/creating-an-array-with-mp3-sound-files
+0
我不能upvote答案,因为我不熟悉的主题,但如果你发布一个链接到你想插入的图片,我可以为你插入它。 – 2014-01-07 23:45:10
答
如果声音是名sound0 ... soundN,你可以介绍给高德 - 一个跟踪当前索引,一个定义声音的数量。
@implementation MyClass {
NSUInteger soundIdx;
NSUInteger soundCount;
}
-(instancetype) init //or any other entry point method like viewDidLoad,....
{
self = [super init];
if (self) {
soundCount = 10;
}
return self;
}
-(IBAction)sound
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) [NSString stringWithFormat:@"sound%lu", soundIdx], CFSTR("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
soundIdx = (++soundIdx) % soundCount;
}
@end
如果声音名称不遵循任何特定的命名约定,你可以把它们放入数组
@implementation MyClass {
NSUInteger soundIdx;
NSArray *soundNames;
}
-(instancetype) init //or any other entry point method like viewDidLoad,....
{
self = [super init];
if (self) {
soundNames = @[@"sound1",@"hello", @"ping"];
}
return self;
}
-(IBAction)sound
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) soundNames[soundIdx], CFSTR("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
soundIdx = (++soundIdx) % [soundNames count];
}
@end
难道你知道所有的声音的持续时间? – 2013-04-07 14:38:41
为什么不连接音频文件并播放一个文件? ;) – HAS 2013-04-08 17:56:59
2-3秒。 我想做一个计数应用程序,所以在第一次触摸它说1和第二次触摸2.等等... – 2013-04-14 00:04:26