按钮iPhone SDK简单的声音播放

时间:2012-07-08 17:33:21

标签: iphone sdk audio

点击一个按钮后,我一直试着播放一个简单的声音,但我无法做到。在尝试了几个教程后,我在这个链接中跟随了一个:

http://www.mybringback.com/tutorial-series/1126/xcode-4-tutorial-ios-ipad-iphone-20-playing-sound-with-button/

这是.h文件:

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface soundTestViewController : UIViewController{

}
-(IBAction) playSound;

@end

这是.m文件:

#import "soundTestViewController.h"

@implementation soundTestViewController
-(IBAction) playSound{
    CFBundleRef mainBundle=CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef =CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"mysound", CFSTR("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end 

但是,构建结果会出错:

Build soundTest of project soundTest with configuration Debug

Ld build/Debug-iphonesimulator/soundTest.app/soundTest normal i386
cd /Users/joegeneric/Documents/soundTest
setenv MACOSX_DEPLOYMENT_TARGET 10.6
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk -L/Users/joegeneric/Documents/soundTest/build/Debug-iphonesimulator -F/Users/joegeneric/Documents/soundTest/build/Debug-iphonesimulator -F/Users/joegeneric/Documents/soundTest -filelist /Users/joegeneric/Documents/soundTest/build/soundTest.build/Debug-iphonesimulator/soundTest.build/Objects-normal/i386/soundTest.LinkFileList -mmacosx-version-min=10.6 -Xlinker -objc_abi_version -Xlinker 2 -framework Foundation -framework UIKit -framework CoreGraphics -framework AVFoundation -framework AudioToolbox -o /Users/joegeneric/Documents/soundTest/build/Debug-iphonesimulator/soundTest.app/soundTest

ld: warning: in /Users/joegeneric/Documents/soundTest/AVFoundation.framework/AVFoundation, missing required architecture i386 in file
ld: warning: in /Users/joegeneric/Documents/soundTest/AudioToolbox.framework/AudioToolbox, missing required architecture i386 in file
Undefined symbols:
  "_AudioServicesPlaySystemSound", referenced from:
      -[soundTestViewController playSound] in soundTestViewController.o
  "_AudioServicesCreateSystemSoundID", referenced from:
      -[soundTestViewController playSound] in soundTestViewController.o
ld: symbol(s) not found

enter image description here 你能说明我做错了什么,或者在iPhone SDK中制作声音播放的其他更简单的工作教程吗?

2 个答案:

答案 0 :(得分:2)

[除了massimobio的正确答案]

您提供的教程链接提到了适用于iOS的Xcode 4,但您的构建结果中的“setenv MACOSX_DEPLOYMENT_TARGET 10.6”行表明您的部署目标为Mac。

您需要将项目构建架构更改为iOS,并检查您的目标是否是IOS,而不是OSX。

例如要更改构建体系结构,请单击您的项目并选择构建设置以显示Base SDK。 enter image description here

您的IBAction也缺少发件人。它应该在您的控制器头文件中声明为

-(IBAction) playSound:(id)sender;

并在您的控制器.m文件中实现为

-(IBAction) playSound:(id)sender {
  // your code here
}

另外,不要忘记使用control-drag将代码中的IBAction链接到XIB,以创建一个动作目标对。

最简单的播放声音演示是SysSound。如果您在示例代码下搜索Xcode文档或通过Apple Developer网站,则可以使用它。

答案 1 :(得分:1)

您是否已将AudioToolbox框架添加到Target, 并在您的代码中导入?

#import <AudioToolbox/AudioToolbox.h>