播放系统音效、自定义音效工具类
需求大致分为三种:
1.震动
2.系统音效(无需提供音频文件)
3.自定义音效(需提供音频文件)
我的工具类的封装:
//
// 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;
}
/**
* @brief 为播放震动效果初始化
*
* @return self
*/
-(id)initForPlayingVibrate;
/**
* @brief 为播放系统音效初始化(无需提供音频文件)
*
* @param resourceName 系统音效名称
* @param type 系统音效类型
*
* @return self
*/
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;
/**
* @brief 为播放特定的音频文件初始化(需提供音频文件)
*
* @param filename 音频文件名(加在工程中)
*
* @return self
*/
-(id)initForPlayingSoundEffectWith:(NSString *)filename;
/**
* @brief 播放音效
*/
-(void)play;
@end
//
// 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震动
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingVibrate];
[sound play];
2.2系统音效,以Tock为例
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"];
[sound play];
2.3自定义音效,将tap.aif音频文件加入到工程
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSoundEffectWith:@"tap.aif"];
[sound play];
tap.aif音频文件样例下载点击
欢迎挑错,改进
分享到:
相关推荐
IOS应用源码——使用CoreAnimation自定义的UIControl.zip
IOS应用源码——使用CoreAnimation自定义的UIControl.rar
IOS应用源码——用于ipad的自定义侧边栏导航效果.zip
IOS应用源码——tab bar controller 自定义颜色和演示demo.zip
IOS源码——ios适用于iOS 5、iOS 6和iOS 7的自定义NavigationBar.zip
IOS应用源码——用于ipad的自定义侧边栏导航效果.rar
IOS应用源码——tab bar controller 自定义颜色和演示demo.rar
IOS 播放自定义音效 系统音效 背景音乐 震动
IOS源码——ios适用于iOS 5、iOS 6和iOS 7的自定义NavigationBar.7z
IOS应用源码——ios slide 自定义效果.zip
IOS应用源码——超炫的自定义 iOS Filter Control.zip
IOS应用源码——超炫的自定义 iOS Filter Control.rar
IOS应用源码——一个自定义的照相demo.zip
IOS应用源码——一个自定义的照相demo.rar