SOUND SERVICE系统音效服务+震动功能实现

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



PlaySoundDemo1ViewController.m

//----------- 限制 ------------
//声音长度要小于 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];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值