您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

實例解析:iOS 開發中系統音效與自訂音效的應用

編輯:iOS 相關教學

音效能為 App 增添不少臨場感,從提示音、震動回饋到自訂音效都有各自的 API。本文依序介紹 AudioToolbox、AVFoundation、MediaPlayer 等框架,並封裝一個方便重複使用的音效工具類。

一、存取聲音服務

首先加入 AudioToolBox 框架與要播放的聲音檔案,並在實作類別中引入標頭:#import <AudioToolbox/AudioToolbox.h>

播放系統聲音需要兩個函式:AudioServicesCreateSystemSoundID 與 AudioServicesPlaySystemSound,還需宣告一個 SystemSoundID 型別的變數:

-(IBAction) playSysSound:(id)sender {
    SystemSoundID soundID;
    // mainBundle 回傳對應程式執行目錄的 NSBundle 物件
    NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"soundeffect" ofType:@"wav"];
    // 傳入檔案位置的 CFURLRef 與 SystemSoundID 變數的指標
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
    AudioServicesPlaySystemSound(soundID);
}

二、提醒音與震動

1、提醒音:與系統聲音的差別在於,若手機處於靜音狀態,提醒音會自動觸發震動。播放提醒音要改用 AudioServicesPlayAlertSound 而非 AudioServicesPlaySystemSound。

2、震動:只需呼叫 AudioServicesPlaySystemSound() 並傳入 kSystemSoundID_Vibrate 常量即可。若裝置不支援震動(如部分 iPad),也不影響,只是不會震動。

三、AVFoundation 框架

對於壓縮的 Audio 檔,或超過 30 秒的音訊,可使用 AVAudioPlayer 類別。它需要知道音訊路徑,其 delegate 有兩個方法:audioDidFinishPlaying:successfully:(播放完成時觸發)與 audioPlayerEndInterruption:(被外部中斷後重新回到 App 時觸發)。

四、MediaPlayer 框架

可使用 MPMoviePlayerController 播放電影檔(支援 H.264、MPEG-4 Part2 video 格式),也能播放網路上的影片檔。

五、封裝音效工具類

實際需求大致分為三類:震動、系統音效(無需提供音訊檔)、自訂音效(需提供音訊檔)。以下是一個好用的工具類封装:

@interface WQPlaySound : NSObject
{
    SystemSoundID soundID;
}
-(id)initForPlayingVibrate;
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;
-(id)initForPlayingSoundEffectWith:(NSString *)filename;
-(void)play;
@end

@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

呼叫方式

步驟一:加入 AudioToolbox.framework 到專案。

步驟二:呼叫 WQPlaySound 工具類。

震動:

WQPlaySound *sound = [[WQPlaySound alloc] initForPlayingVibrate];
[sound play];

系統音效(以 Tock 為例):

WQPlaySound *sound = [[WQPlaySound alloc] initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"];
[sound play];

自訂音效(把 tap.aif 加入專案):

WQPlaySound *sound = [[WQPlaySound alloc] initForPlayingSoundEffectWith:@"tap.aif"];
[sound play];

透過這個封裝,無論是震動、系統音效還是自訂音效,都能用一致的介面輕鬆呼叫,大幅簡化專案中的音效管理。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved