ios中设置app音效音效和震动
来源:互联网 发布:网络作家系列小说 编辑:程序博客网 时间:2024/06/04 18:07
在项目中使用AudioServicesPlaySystemSound 这个接口来进行声音和震动的播放, 当然需要在工程中加入AudioToolBox.framework
我们可以写一个文件来封装声音和震动的各项功能,调用时使用单例比较方便。
我写的文件messageSound
在messageSound.h文件中
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface messageSound :NSObject
{
SystemSoundID soundID;
}
@property (nonatomic,assign)BOOL isON;
//分别为震动和声音设置的系统单列
+ (id) sharedInstanceForVibrate;
+ (id) sharedInstanceForSound;
/**
*@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;
-(void)cancleSound;
@end
#import "messageSound.h"
@implementation messageSound
static messageSound *_sharedInstance;
static messageSound *_sharedInstanceForSound;
+(id)sharedInstanceForVibrate
{
@synchronized ([messageSoundclass]) {
if (_sharedInstance ==nil) {
_sharedInstance = [[messageSoundalloc] initForPlayingVibrate];
}
}
return_sharedInstance;
}
+ (id) sharedInstanceForSound
{
@synchronized ([messageSoundclass]) {
if (_sharedInstanceForSound ==nil) {
_sharedInstanceForSound = [[messageSoundalloc] initForPlayingSystemSoundEffectWith:@"sms-received2"ofType:@"caf"];
}
}
return_sharedInstanceForSound;
}
-(id)initForPlayingVibrate
{
self=[superinit];
if(self){
soundID=kSystemSoundID_Vibrate;
}
returnself;
}
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type
{
self=[superinit];
if(self){
// NSString *path=[[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:resourceName ofType:type];
NSString *path = [NSStringstringWithFormat:@"/System/Library/Audio/UISounds/%@.%@",resourceName,type];
if(path){
SystemSoundID theSoundID;
OSStatus error =AudioServicesCreateSystemSoundID((__bridgeCFURLRef)[NSURLfileURLWithPath:path],&theSoundID);
if(error ==kAudioServicesNoError){
soundID=theSoundID;
}else{
NSLog(@"Failed to create sound");
}
}
}
return self;
}
-(id)initForPlayingSoundEffectWith:(NSString *)filename
{
self=[superinit];
if(self){
NSURL *fileURL=[[NSBundlemainBundle]URLForResource:filenamewithExtension:nil];
if(fileURL!=nil){
SystemSoundID theSoundID;
OSStatus error=AudioServicesCreateSystemSoundID((__bridgeCFURLRef)fileURL, &theSoundID);
if(error ==kAudioServicesNoError){
soundID=theSoundID;
}else{
NSLog(@"Failed to create sound");
}
}
}
returnself;
}
-(void)play
{
AudioServicesPlaySystemSound(soundID);
}
-(void)cancleSound
{
_sharedInstance=nil;
//AudioServicesRemoveSystemSoundCompletion(soundID);
}
-(void)dealloc
{
AudioServicesDisposeSystemSoundID(soundID);
}
@end
//设置系统音效
[messageSoundsharedInstanceForSound];
//设置系统震动
[messageSoundsharedInstanceForVibrate];
在app设置中,把声音和震动着两个开关的状态保存在本地设置文件中,在播放系统音效的地方,读取本地设置文件的开关状态,根据状态来判断播放声音还是播放震动或者二者兼有,代码如下。(音效文件我是通过yyCache保存在本地的)messageSound *ms=[messageSoundsharedInstanceForVibrate];
messageSound *ms1=[messageSoundsharedInstanceForSound];
NSDictionary *localDict;
if([[YYCachesharedInstance]containsObjectForKey:[NSStringstringWithFormat:@"%@%@",[AccountToolssharedAccountTools].currentAccount.uid,@"messageSoundSetting"]]==YES)
{
localDict=[[YYCachesharedInstance] objectForKey:[NSStringstringWithFormat:@"%@%@",[AccountToolssharedAccountTools].currentAccount.uid,@"messageSoundSetting"]];
if([[localDictobjectForKey:@"Sound"]intValue]==1)
{
[ms1 play];
}
if([[localDictobjectForKey:@"Vibrate"]intValue]==1)
{
[ms play];
}
}
- ios中设置app音效音效和震动
- iOS开发中让手机震动一下 播放系统音效
- ios 自定义键盘的按键音效和震动效果
- iOS-高仿微信摇一摇动画效果加震动音效
- IOS中音效播放
- iOS 声音和音效
- iOS中播放音乐和音效
- Android 关于震动问题和音效问题
- iOS音效
- iOS音效和音乐播放
- ios之使用录音功能和设置音效播放
- ios播放系统自带音效以及震动
- ios播放系统自带音效以及震动
- IOS音效播放 添加音效
- android:为你的app设置音效
- 音效
- 音效
- Unity3d之音效播放和调用手机震动
- LocalBroadcastManager-应用内部的Broadcast
- MySQL 5.7 java 运行 mysql.exe还原数据库兼容性问题
- 简述Android SDK制作流程
- recyclerview23+出现多个item只显示第一个item的问题
- laravel的基础学习(二)
- ios中设置app音效音效和震动
- storm中初始化类
- 使用数据库保存session的方法
- 日志6.12
- 易宝支付开发实现
- SwipeBackActivity的另一种方法(二)
- Qt 4.7.2编译过程的简要记录
- JDBC学习
- laravel的基础学习(三)