2010-9-26 22:19:18 上传
下载附件 (42.9 KB)
PlaySoundDemo1ViewController.h
- #import <UIKit/UIKit.h>
- #include <AudioToolbox/AudioToolbox.h>
-
- @interface PlaySoundDemo1ViewController : UIViewController {
- IBOutlet UISwitch *swcallback;
- IBOutlet UIPickerView *soundPicker;
- NSArray *soundData;
- SystemSoundID soundFileObject;
- }
-
- @property (nonatomic,retain) UISwitch *swcallback;
- @property (nonatomic,retain) UIPickerView *soundPicker;
- @property (nonatomic,retain) NSArray *soundData;
- @property (readonly) SystemSoundID soundFileObject;
-
- static void completionCallback (SystemSoundID mySSID, void* myself) ;
- - (IBAction) playSystemSound;
- - (IBAction) playAlertSound;
- - (IBAction) vibrate;
- -(IBAction) stopplaysound;
- -(void) GetPlaysound;
-
- @end
- //----------- 限制 ------------
- //声音长度要小于 30 秒
- // In linear PCM 或者 IMA4 (IMA/ADPCM) 格式的
- //需为 .caf, .aif, 或者 .wav 的档案
- //不能控制播放的进度
- //调用方法后立即播放声音
- //没有循环播放和立体声控制
- //参考数据:
- //http://blog.xiang.li/2009/02/iphone-dev-audio-play/
- //https://developer.apple.com/iphone/library/samplecode/SysSound/
-
- #import "PlaySoundDemo1ViewController.h"
-
- @implementation PlaySoundDemo1ViewController
-
- @synthesize swcallback,soundPicker,soundData,soundFileObject;
-
- //停止当前音效的播放
- -(IBAction) stopplaysound{
- AudioServicesRemoveSystemSoundCompletion (self.soundFileObject);
- }
-
- //建立声音对象
- -(void) GetPlaysound{
- [self stopplaysound];
-
- //取出使用者所选择的音效项目
- NSInteger row=[ soundPicker selectedRowInComponent:0];
-
- //指定不同的音效文件
- NSString *soundfilename;
- switch (row) {
- case 0:
- soundfilename=@"sound1.aiff";
- break;
- case 1:
- soundfilename=@"sound2.aiff";
- break;
- case 2:
- soundfilename=@"sound3.aiff";
- break;
- default:
- break;
- }
-
-
- NSString *Path=[[NSBundle mainBundle] bundlePath];
- NSURL *soundfileURL=[NSURL fileURLWithPath:[Path stringByAppendingPathComponent:soundfilename]];
-
- //建立音效对象
- AudioServicesCreateSystemSoundID((CFURLRef)soundfileURL, &soundFileObject);
-
- //判断是否连续播放
- if ([swcallback isOn]){
- // Add sound completion callback
- AudioServicesAddSystemSoundCompletion (soundFileObject, NULL, NULL,completionCallback,(void*) self);
- }
- }
-
- //当音效播放完毕后的处理方式,这里设定为再一次播放
- static void completionCallback (SystemSoundID mySSID, void* myself) {
- AudioServicesPlaySystemSound(mySSID);
- }
-
- - (IBAction) playSystemSound{
- [self GetPlaysound];
- AudioServicesPlaySystemSound (self.soundFileObject);
- // AudioServicesDisposeSystemSoundID (self.soundFileObject);
- }
-
- -(IBAction) playAlertSound{
- [self GetPlaysound];
- //播放音效
- AudioServicesPlayAlertSound (self.soundFileObject);
- }
- - (IBAction) vibrate{
- //震动
- AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
- }
-
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- //建立音效清单数组
- NSArray *array=[[NSArray alloc] initWithObjects:@"音效1",@"音效2",@"音效3",nil];
- self.soundData=array;
- [array release];
- }
- - (void)dealloc {
- [super dealloc];
- AudioServicesDisposeSystemSoundID (self.soundFileObject);
- }
- #pragma mark -
- #pragma mark soundPicker Data Soure
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
- return 1;
- }
- - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
- return [soundData count];
- }
- #pragma mark -
- #pragma mark soundPicker Delegate
- - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
- return [soundData objectAtIndex:row];
- }