ios中设置app音效音效和震动
在項(xiàng)目中使用AudioServicesPlaySystemSound 這個(gè)接口來(lái)進(jìn)行聲音和震動(dòng)的播放, 當(dāng)然需要在工程中加入AudioToolBox.framework
我們可以寫一個(gè)文件來(lái)封裝聲音和震動(dòng)的各項(xiàng)功能,調(diào)用時(shí)使用單例比較方便。
我寫的文件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;
//分別為震動(dòng)和聲音設(shè)置的系統(tǒng)單列
+ (id) sharedInstanceForVibrate;
+ (id) sharedInstanceForSound;
/**
?*@brief 為震動(dòng)效果初始化
?*
?*@return self
?*/
-(id)initForPlayingVibrate;
/**
?? *? @brief? 為播放系統(tǒng)音效初始化(無(wú)需提供音頻文件)
?
?? *
?
?? *? @param resourceName 系統(tǒng)音效名稱
?
?? *? @param type 系統(tǒng)音效類型
?
?? *
?? *? @return self
?? */
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;
/*
?
?* @brief 為播放特定的音頻文件初始化 (需提供音頻文件)
?
?*
?
?*@param filename 音頻文件名(加在工程中)
?
?*
?
?*@return self
?
?*/
-(id)initForPlayingSoundEffectWith:(NSString *)filename;
/*
?
?* @brief 播放音效
?
?*/
-(void)play;
-(void)cancleSound;
@end
在message.m文件中代碼如下#import "messageSound.h"
@implementation messageSound
static messageSound *_sharedInstance;
static messageSound *_sharedInstanceForSound;
+(id)sharedInstanceForVibrate
{
?? ?
? ? @synchronized ([messageSound class]) {
?? ? ? ?
? ? ? ? if (_sharedInstance == nil) {
?? ? ? ? ? ?
? ? ? ? ? ? _sharedInstance = [[messageSound alloc] initForPlayingVibrate];
?? ? ? ? ? ?
? ? ? ? }
? ? }
? ? return _sharedInstance;
?? ?
}
+ (id) sharedInstanceForSound
{
? ? @synchronized ([messageSound class]) {
?? ? ? ?
? ? ? ? if (_sharedInstanceForSound == nil) {
?? ? ? ? ? ?
? ? ? ? ? ? _sharedInstanceForSound = [[messageSound alloc] initForPlayingSystemSoundEffectWith:@"sms-received2" ofType:@"caf"];
?? ? ? ? ? ?
? ? ? ? }
? ? }
? ? return _sharedInstanceForSound;
}
-(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];
? ? ? ? NSString *path = [NSString stringWithFormat:@"/System/Library/Audio/UISounds/%@.%@",resourceName,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)cancleSound
{
? ? _sharedInstance=nil;
? ? //AudioServicesRemoveSystemSoundCompletion(soundID);
}
-(void)dealloc
{
? ?
AudioServicesDisposeSystemSoundID(soundID);}
@end
至于怎么使用,我是這么用的,在appdelegate.m文件中開啟系統(tǒng)音效設(shè)置的單例? ? //設(shè)置系統(tǒng)音效
? ? [messageSound sharedInstanceForSound];
? ? //設(shè)置系統(tǒng)震動(dòng)
? ? [messageSound sharedInstanceForVibrate];
在app設(shè)置中,把聲音和震動(dòng)著兩個(gè)開關(guān)的狀態(tài)保存在本地設(shè)置文件中,在播放系統(tǒng)音效的地方,讀取本地設(shè)置文件的開關(guān)狀態(tài),根據(jù)狀態(tài)來(lái)判斷播放聲音還是播放震動(dòng)或者二者兼有,代碼如下。(音效文件我是通過(guò)yyCache保存在本地的)?messageSound *ms=[messageSound sharedInstanceForVibrate];
? ?
? ? messageSound *ms1=[messageSound sharedInstanceForSound];
?? ?
? ? NSDictionary *localDict;
? ? if([[YYCache sharedInstance]containsObjectForKey:[NSString stringWithFormat:@"%@%@",[AccountTools sharedAccountTools].currentAccount.uid,@"messageSoundSetting"]]==YES)
? ? {
? ? ? ? localDict=[[YYCache sharedInstance] objectForKey:[NSString stringWithFormat:@"%@%@",[AccountTools sharedAccountTools].currentAccount.uid,@"messageSoundSetting"]];
? ? ? ? if([[localDict objectForKey:@"Sound"] intValue]==1)
? ? ? ? {
? ? ? ? ? ? [ms1 play];
? ? ? ? }
?? ? ? ?
? ? ? ? if([[localDict objectForKey:@"Vibrate"] intValue]==1)
? ? ? ? {
? ? ? ? ? ? [ms play];
? ? ? ? }
?? ? ? ?
? ? }
這樣就實(shí)現(xiàn)了,微信設(shè)置中那種單獨(dú)設(shè)置聲音和震動(dòng)的效果。 iOS菜逼的第一篇技術(shù)博客,謝謝,其中部分代碼是參考別人的代碼仿寫的。總結(jié)
以上是生活随笔為你收集整理的ios中设置app音效音效和震动的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python day46
- 下一篇: 带你学微信小程序开发