iOS 播放系统音效,自定义音效
2016-02-26 17:03
501 查看
播放系统音效、自定义音效工具类
需求大致分为三种:
1.震动
2.系统音效(无需提供音频文件)
3.自定义音效(需提供音频文件)
我的工具类的封装:
//
// WQPlaySound.h
// WQSound
//
// Created by 念茜 on 12-7-20.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import
#import
@interface WQPlaySound : NSObject
{
SystemSoundID soundID;
}
-(id)initForPlayingVibrate;
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;
-(id)initForPlayingSoundEffectWith:(NSString *)filename;
-(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工具类
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingVibrate];
[sound play];
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"];
[sound play];
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSoundEffectWith:@"tap.aif"];
[sound play];
需求大致分为三种:
1.震动
2.系统音效(无需提供音频文件)
3.自定义音效(需提供音频文件)
我的工具类的封装:
//
// WQPlaySound.h
// WQSound
//
// Created by 念茜 on 12-7-20.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import
#import
@interface WQPlaySound : NSObject
{
SystemSoundID soundID;
}
-(id)initForPlayingVibrate;
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;
-(id)initForPlayingSoundEffectWith:(NSString *)filename;
-(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];
相关文章推荐
- iOS常用控件尺寸大合集
- IOS文件上传
- iOS 代码设置自动布局
- IOS常用正则表达式
- iOS 开发切割圆角图片
- iOS开发全面解析多线程
- ios开发根据Label的字体和宽度算出它的高度
- iOS企业发布问题记录三(Log Guru 使用方法)
- iOS 常见的加密形式: AES加密, DES加密, MD5加密,SHA1加密, BASE64加密
- ios 资源很全面转载以后方便看
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- 判断字符串是否中文 & 判断是否正确身份证号
- iOS开发笔记--敏捷开发之Scrum扫盲篇
- iOS Block 的理解与学习
- iOS 蓝牙打印小票机
- iOS多线程编程总结
- iOS实战-自定义的横向滚动控件CustomScrollView
- Win8.1+Ubuntu双系统(BIOS+MBR)
- 深入浅出iOS函数式编程与响应式编程概念
- iOS推送之远程推送