0

单击按钮后,我已经尝试了几个小时播放简单的声音,但我无法做到。在尝试了几个教程之后,我在这个链接中关注了一个:

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

在此处输入图像描述 你能展示我做错了什么或任何其他更简单的关于在 iPhone SDK 中播放声音的工作教程吗?

4

2 回答 2

2

【除了massimobio的正确答案】

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

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

例如,要更改您的构建架构,请单击您的项目并选择构建设置以显示基本 SDK。在此处输入图像描述

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

-(IBAction) playSound:(id)sender;

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

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

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

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

于 2012-07-08T18:07:30.693 回答
1

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

#import <AudioToolbox/AudioToolbox.h>
于 2012-07-08T17:54:34.163 回答