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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/3/13 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios中设置app音效音效和震动 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在項目中使用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

在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文件中開啟系統音效設置的單例

? ? //設置系統音效

? ? [messageSound sharedInstanceForSound];

? ? //設置系統震動

? ? [messageSound sharedInstanceForVibrate];

在app設置中,把聲音和震動著兩個開關的狀態保存在本地設置文件中,在播放系統音效的地方,讀取本地設置文件的開關狀態,根據狀態來判斷播放聲音還是播放震動或者二者兼有,代碼如下。(音效文件我是通過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];

? ? ? ? }

?? ? ? ?

? ? }

這樣就實現了,微信設置中那種單獨設置聲音和震動的效果。 iOS菜逼的第一篇技術博客,謝謝,其中部分代碼是參考別人的代碼仿寫的。


總結

以上是生活随笔為你收集整理的ios中设置app音效音效和震动的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。