简体   繁体   English

使用按钮iPhone SDK进行简单的声音播放

[英]Simple sound play with button iPhone SDK

I have been trying for hours to make a simple sound playing after clicking a button, but I could not make it. 单击按钮后,我已经尝试了数小时来播放简单的声音,但我做不到。 After trying several tutorials, I followed one in this link: 在尝试了几本教程之后,我在此链接中关注了一个:

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

This is .h file: 这是.h文件:

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

}
-(IBAction) playSound;

@end

This is the .m file: 这是.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 

However, the Build Results gives errors: 但是,生成结果给出了错误:

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

在此处输入图片说明 Can you show what I did wrong or any other more simple working tutorial on making a sound play in iPhone SDK? 您能否显示我做错了什么,或者在iPhone SDK中播放声音方面有任何其他更简单的工作教程?

[In addition to massimobio's correct answer] [除了massimobio的正确答案]

The tutorial link you provided mentions Xcode 4 for iOS, but "setenv MACOSX_DEPLOYMENT_TARGET 10.6" line in your Build Results indicate your deployment target as Mac. 您提供的教程链接提到了适用于iOS的Xcode 4,但是“构建结果”中的“ setenv MACOSX_DEPLOYMENT_TARGET 10.6”行指示您的部署目标为Mac。

You need to change your Project Build Architecture to iOS and check that your target is an IOS, not OSX. 您需要将Project Build Architecture更改为iOS,并检查您的目标是IOS,而不是OSX。

For eg to change your build architecture, click on your Project and select Build Settings to display the Base SDK. 例如,要更改构建体系结构,请单击项目,然后选择构建设置以显示基本SDK。 在此处输入图片说明

Also your IBAction is missing the sender. 另外,您的IBAction缺少发送者。 It should be declared in your controller header file as 应该在您的控制器头文件中将其声明为

-(IBAction) playSound:(id)sender;

and implemented in your controller .m file as 并在您的Controller .m文件中实现为

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

Also, don't forget to link your IBAction in your code to your XIB using control-drag to create an action-target pair. 另外,不要忘记使用control-drag将代码中的IBAction链接到XIB,以创建一个操作目标对。

Simplest Demo of playing sound is SysSound. 播放声音的最简单演示是SysSound。 It's available if you search Xcode documentation or via Apple Developer website under Sample Code. 如果您搜索Xcode文档或通过Apple Developer网站的“示例代码”下可用。

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

#import <AudioToolbox/AudioToolbox.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM