實(shí)例解析iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用
一、訪問聲音服務(wù)
添加框架AudioToolBox以及要播放的聲音文件,另外還需要在實(shí)現(xiàn)聲音服務(wù)的類中導(dǎo)入該框架的接口文件:
#import <AudioToolbox/AudioToolbox.h>
播放系統(tǒng)聲音,需要兩個(gè)函數(shù)是AudioServicesCreateSystemSoundID和AudioServicesPlaySystemSound,還需要聲明一個(gè)類型為SystemSoundID類型的變量,它表示要使用的聲音文件。
-(IBAction) playSysSound:(id)sender {
SystemSoundID sourceID;
//調(diào)用NSBundle類的方法mainBundle返回一個(gè)NSBundle對象,該對象對應(yīng)于當(dāng)前程序可執(zhí)行二進(jìn)制文件所屬的目錄
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"soundeffect" ofType:@"wav"];
//一個(gè)指向文件位置的CFURLRef對象和一個(gè)指向要設(shè)置的SystemSoundID變量的指針
AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);
}
二、提醒音和震動(dòng)
1、提醒音
和系統(tǒng)聲音的差別:
如果手機(jī)處于靜音狀態(tài),則提醒音將自動(dòng)觸發(fā)震動(dòng);
播放提醒音需要的函數(shù)是AudioServicesPlayAlertSound而不是AudioServicesPlaySystemSound。
2、震動(dòng)
只需要調(diào)用AudioServicesPlaySystemSound()方法,傳入kSystemSoundID_Vibrate常量即可。
如果設(shè)備不支持震動(dòng)(如iPad 2),那么也沒關(guān)系,只是不會(huì)震動(dòng)。
三、AVFoundation framwork
對于壓縮的Audio文件,或者超過30秒的音頻文件,可以使用AVAudioPlayer類。
1、AVAudioPlayer也需要知道音頻文件的路徑;
2、這個(gè)類對應(yīng)的AVAudioPlayerDelegate有兩個(gè)委托方法:
1)、audioDidFinishPlaying:successfully:當(dāng)音頻播放完成之后觸發(fā);
2)、audioPlayerEndInterruption:當(dāng)程序被應(yīng)用外部打斷后,重新回到應(yīng)用程序的時(shí)候觸發(fā)。
四、MediaPlayer framwork
可以使用MPMoviePlayerController播放電影文件(好像只能播放H.264、MPEG-4 Part2 video格式),還可以播放互聯(lián)網(wǎng)上的視頻文件。
五、調(diào)用和自定義音效實(shí)例實(shí)例
需求大致分為三種:
1.震動(dòng)
2.系統(tǒng)音效(無需提供音頻文件)
3.自定義音效(需提供音頻文件)
我的工具類的封裝:
//
// WQPlaySound.h
// WQSound
//
// Created by 念茜 on 12-7-20.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface WQPlaySound : NSObject
{
SystemSoundID soundID;
}
/**
* @brief 為播放震動(dòng)效果初始化
*
* @return self
*/
-(id)initForPlayingVibrate;
/**
* @brief 為播放系統(tǒng)音效初始化(無需提供音頻文件)
*
* @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;
@end
//
// WQPlaySound.m
// WQSound
//
// Created by 念茜 on 12-7-20.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "WQPlaySound.h"
@implementation WQPlaySound
-(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];
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)dealloc
{
AudioServicesDisposeSystemSoundID(soundID);
}
@end
調(diào)用方法步驟:
1.加入AudioToolbox.framework到工程中
2.調(diào)用WQPlaySound工具類
2.1震動(dòng)
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingVibrate];
[sound play];
2.2系統(tǒng)音效,以Tock為例
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"];
[sound play];
2.3自定義音效,將tap.aif音頻文件加入到工程
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSoundEffectWith:@"tap.aif"];
[sound play];
- iOS自定義UICollectionViewLayout實(shí)現(xiàn)瀑布流布局
- iOS自定義UICollectionViewFlowLayout實(shí)現(xiàn)圖片瀏覽效果
- 詳解在iOS App中自定義和隱藏狀態(tài)欄的方法
- IOS手勢操作(拖動(dòng)、捏合、旋轉(zhuǎn)、點(diǎn)按、長按、輕掃、自定義)
- iOS自定義button抖動(dòng)效果并實(shí)現(xiàn)右上角刪除按鈕
- iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件
- iOS自定義推送消息提示框
- IOS輕松幾步實(shí)現(xiàn)自定義轉(zhuǎn)場動(dòng)畫
- Swift自定義iOS中的TabBarController并為其添加動(dòng)畫
- iOS開發(fā)之自定義UITextField的方法
- IOS 自定義UICollectionView的頭視圖或者尾視圖UICollectionReusableView
相關(guān)文章
iOS開發(fā)中使用UIWebView 屏蔽 alert警告框
這篇文章主要介紹了iOS開發(fā)中使用UIWebView 屏蔽 alert警告框的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11IOS 開發(fā)之?dāng)?shù)據(jù)存儲(chǔ)writeToFile的應(yīng)用實(shí)例
這篇文章主要介紹了IOS 開發(fā)之?dāng)?shù)據(jù)存儲(chǔ)writeToFile的應(yīng)用實(shí)例的相關(guān)資料,這里提供實(shí)例幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09iOS實(shí)現(xiàn)遠(yuǎn)程推送原理及過程
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)遠(yuǎn)程推送原理及具體過程,圖文結(jié)合的方式針對iOS遠(yuǎn)程推送進(jìn)行分析,感興趣的小伙伴們可以參考一下2016-05-05iOS獲取短信驗(yàn)證碼倒計(jì)時(shí)的兩種實(shí)現(xiàn)方法
本篇文章主要介紹了iOS獲取短信驗(yàn)證碼倒計(jì)時(shí)的兩種實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法
在iOS開發(fā)中,有時(shí)會(huì)有跳轉(zhuǎn)系統(tǒng)設(shè)置界面的需求,例如提示用戶打開藍(lán)牙或者WIFI,提醒用戶打開推送或者位置權(quán)限等,接下來通過本文給大家介紹IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法,喜歡的朋友參考下2016-02-02