日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

ios中设置app音效音效和震动

發(fā)布時(shí)間:2024/3/13 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios中设置app音效音效和震动 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在項(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。