ios播放系统自带音效以及震动

int systemSoundID;
AudioServicesPlaySystemSound(systemSoundID);
//systemSoundID的取值范围在1000-2000
//播放自己的声音,但此种播放方法主要播放一些较短的声音文件
NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], 

@"/jad0007a.wav"];

id SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);


需求大致分为三种:
1.震动
2.系统音效(无需提供音频文件)
3.自定义音效(需提供音频文件)
我的工具类的封装:
[cpp] 
// 
//  WQPlaySound.h 
//  WQSound 
// 
//  Created by 念茜 on 12-7-20. 
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 
// 
 
#import <UIKit/UIKit.h> 
#import <AudioToolbox/AudioToolbox.h> 
 
@interface WQPlaySound : NSObject 
{ 
    SystemSoundID soundID; 
} 
 
 
-(id)initForPlayingVibrate; 
 
 
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type; 
 
 
-(id)initForPlayingSoundEffectWith:(NSString *)filename; 
 
 
-(void)play; 
 
@end 

[cpp]
// 
//  WQPlaySound.m 
//  WQSound 
// 
//  Created by 念茜 on 12-7-20. 
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 
// 
 
#import "WQPlaySound.h" 
 
@implementation WQPlaySound 
 
-(id)initForPlayingVibrate 
{ 
    self = [super init]; 
    if (self) { 
        soundID = kSystemSoundID_Vibrate; 
    } 
    return self;     
} 
 
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type 
{ 
    self = [super init]; 
    if (self) { 
        NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:resourceName ofType:type]; 
        if (path) { 
            SystemSoundID theSoundID; 
            OSStatus error =  AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &theSoundID); 
            if (error == kAudioServicesNoError) { 
                soundID = theSoundID; 
            }else { 
                NSLog(@"Failed to create sound "); 
            } 
        } 
         
    } 
    return self; 
} 
 
-(id)initForPlayingSoundEffectWith:(NSString *)filename 
{ 
    self = [super init]; 
    if (self) { 
        NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; 
        if (fileURL != nil) 
        { 
            SystemSoundID theSoundID; 
            OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID); 
            if (error == kAudioServicesNoError){ 
                soundID = theSoundID; 
            }else { 
                NSLog(@"Failed to create sound "); 
            } 
        } 
    } 
    return self; 
} 
 
-(void)play 
{ 
    AudioServicesPlaySystemSound(soundID); 
} 
 
-(void)dealloc 
{  
    AudioServicesDisposeSystemSoundID(soundID); 
} 
@end 

调用方法步骤:
1.加入AudioToolbox.framework到工程中
2.调用WQPlaySound工具类
2.1震动
[cpp] 
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingVibrate]; 
[sound play]; 
2.2系统音效,以Tock为例
[cpp] 
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"]; 
[sound play]; 
2.3自定义音效,将tap.aif音频文件加入到工程
[cpp] 
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSoundEffectWith:@"tap.aif"]; 
[sound play]; 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值