iOS声音播放及音效处理开源代码_SoundManager
2013-08-02 14:24
471 查看
最近的一个项目中需要经常播放各种音效,这里是一个比较方便使用的class,用来播放声音文件
项目中需要包含AVFoundation和AudioToolbox两个Library
[html] view
plaincopy
SoundManager.h
[html] view
plaincopy
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface SoundManager : NSObject
{
AVAudioPlayer *player;
}
+(id) sharedManager;
-(void) playSoundEffectWithFileName: (NSString *)fileName;
@end
[plain] view
plaincopy
SoundManager.m
[plain] view
plaincopy
#import "SoundManager.h"
#import <AudioToolbox/AudioToolbox.h>
static SoundManager *soundManagerInstance;
@implementation SoundManager
+(id) sharedManager
{
if(!soundManagerInstance)
{
soundManagerInstance = [[LDCSoundManager alloc] init];
}
return soundManagerInstance;
}
-(void)playSoundEffectWithFileName:(NSString *)fileName
{
SystemSoundID _soundID;
NSString *newMessageSoundPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"wav"];
if(newMessageSoundPath)
{
NSURL *newMessageSoundUrl = [NSURL fileURLWithPath:newMessageSoundPath];
OSStatus err = AudioServicesCreateSystemSoundID((CFURLRef)newMessageSoundUrl, &_soundID);
if (err != kAudioServicesNoError)
{
//add
}
else
{
AudioServicesPlaySystemSound(_soundID);
}
}
}
@end
随后,在需要播放音效时引用这个类,并添加
[plain] view
plaincopy
[[SoundManager sharedManager] playSoundEffectWithFileName:@"YourSoundFileName"];
即可
项目中需要包含AVFoundation和AudioToolbox两个Library
[html] view
plaincopy
SoundManager.h
[html] view
plaincopy
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface SoundManager : NSObject
{
AVAudioPlayer *player;
}
+(id) sharedManager;
-(void) playSoundEffectWithFileName: (NSString *)fileName;
@end
[plain] view
plaincopy
SoundManager.m
[plain] view
plaincopy
#import "SoundManager.h"
#import <AudioToolbox/AudioToolbox.h>
static SoundManager *soundManagerInstance;
@implementation SoundManager
+(id) sharedManager
{
if(!soundManagerInstance)
{
soundManagerInstance = [[LDCSoundManager alloc] init];
}
return soundManagerInstance;
}
-(void)playSoundEffectWithFileName:(NSString *)fileName
{
SystemSoundID _soundID;
NSString *newMessageSoundPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"wav"];
if(newMessageSoundPath)
{
NSURL *newMessageSoundUrl = [NSURL fileURLWithPath:newMessageSoundPath];
OSStatus err = AudioServicesCreateSystemSoundID((CFURLRef)newMessageSoundUrl, &_soundID);
if (err != kAudioServicesNoError)
{
//add
}
else
{
AudioServicesPlaySystemSound(_soundID);
}
}
}
@end
随后,在需要播放音效时引用这个类,并添加
[plain] view
plaincopy
[[SoundManager sharedManager] playSoundEffectWithFileName:@"YourSoundFileName"];
即可
相关文章推荐
- cocos2dx AudioEngine(声音播放)音效播放结束判断测试和背景音乐播放
- iOS 播放音效
- 【iOS-Cocos2d游戏开发之十四】音频/音效/视频播放(利用Cocos2D-iPhone-Extensions嵌入Cocos2d进行视频播放!)
- qml部署ios播放声音的一些问题
- iOS App中实现播放音效和音乐功能的简单示例
- iOS 音效播放
- iOS开发拓展篇—音效的播放
- Android 4.3实现类似iOS在音乐播放过程中如果有来电则音乐声音渐小铃声渐大的效果
- iOS 播放音效
- iOS-播放系统声音
- 手机游戏中声音的播放及处理
- iOS开发--音乐文件播放工具类的封装(包含了音效的封装)
- iOS开发拓展篇—音效的播放
- iOS菜鸟学习——声音的播放
- ios中有关声音的处理参数说明
- IOS音乐和音效的加载播放
- IOS -声音播放AudioServices
- ios播放声音中断后台音乐的问题
- ios-UI高级 相册的相关处理和音乐播放AVFoundation框架的内容总结
- construct2在ios下播放声音