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音效音效和震动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python day46
- 下一篇: 带你学微信小程序开发