[英]Simple sound play with button iPhone SDK
單擊按鈕后,我已經嘗試了數小時來播放簡單的聲音,但我做不到。 在嘗試了幾本教程之后,我在此鏈接中關注了一個:
這是.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中播放聲音方面有任何其他更簡單的工作教程?
[除了massimobio的正確答案]
您提供的教程鏈接提到了適用於iOS的Xcode 4,但是“構建結果”中的“ setenv MACOSX_DEPLOYMENT_TARGET 10.6”行指示您的部署目標為Mac。
您需要將Project Build Architecture更改為iOS,並檢查您的目標是IOS,而不是OSX。
例如,要更改構建體系結構,請單擊項目,然后選擇構建設置以顯示基本SDK。
另外,您的IBAction缺少發送者。 應該在您的控制器頭文件中將其聲明為
-(IBAction) playSound:(id)sender;
並在您的Controller .m文件中實現為
-(IBAction) playSound:(id)sender {
// your code here
}
另外,不要忘記使用control-drag將代碼中的IBAction鏈接到XIB,以創建一個操作目標對。
播放聲音的最簡單演示是SysSound。 如果您搜索Xcode文檔或通過Apple Developer網站的“示例代碼”下可用。
您是否將AudioToolbox框架添加到Target中,並將其導入您的代碼中?
#import <AudioToolbox/AudioToolbox.h>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.