iOS中的音頻

一、簡介

1.音頻可以分為2種
(1)音效
又稱“短音頻”,通常在程序中的播放時(shí)長為1~2秒
在應(yīng)用程序中起到點(diǎn)綴效果,提升整體用戶體驗(yàn)
(2)音樂
比如游戲中的“背景音樂”,一般播放時(shí)間較

2.播放音頻可以使用框架
AVFoundation.framework

二、音效

1.音效的播放

// 1.獲得音效文件的路徑
NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];

// 2.加載音效文件,得到對(duì)應(yīng)的音效ID
SystemSoundID soundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);

// 3.播放音效
AudioServicesPlaySystemSound(soundID);

音效文件只需要加載1次
2.音效播放常見函數(shù)總結(jié)

音效播放常見函數(shù)總結(jié)
加載音效文件
AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)

釋放音效資源
AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)

播放音效
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)

播放音效帶點(diǎn)震動(dòng)
AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)

3.音效格式

屏幕快照 2017-06-22 下午2.35.22.png

注意:硬件解碼器一次只能對(duì)一個(gè)音頻文件解碼。在實(shí)際應(yīng)用中通常使用非壓縮的音頻格式(AIFF)或者CAF音頻格式,從而減低系統(tǒng)在音頻解碼上的消耗,達(dá)到省電的目的
4.聲音和音效小結(jié)——音頻轉(zhuǎn)換工具

轉(zhuǎn)換aiff格式
afconvert -f AIFF -d I8 filename

轉(zhuǎn)換caf格式
afconvert -f caff -d aac -b 32000 filename

批量轉(zhuǎn)換
find . -name '*.mp3' -exec afconvert -f caff -d aac -b 32000 {} \;

5.音效播放工具類

#import <Foundation/Foundation.h>

@interface AudioTool : NSObject

+ (void)playSoundWithSoundname:(NSString *)soundname;

@end


#import "AudioTool.h"
#import <AVFoundation/AVFoundation.h>

@implementation XMGAudioTool

static NSMutableDictionary *_soundIDs;

+ (void)initialize
{
    _soundIDs = [NSMutableDictionary dictionary];
}

//+ (NSMutableDictionary *)soundIDs
//{
//    if (_soundIDs == nil) {
//        _soundIDs = [NSMutableDictionary dictionary];
//    }
//    
//    return _soundIDs;
//}

+ (void)playSoundWithSoundname:(NSString *)soundname
{
    // 1.定義SystemSoundID
    SystemSoundID soundID = 0;
    
    // 2.從字典中取出對(duì)應(yīng)soundID,如果取出是nil,表示之前沒有存放在字典
    soundID = [_soundIDs[soundname] unsignedIntValue];
    if (soundID == 0) {
        CFURLRef url = (__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:soundname withExtension:nil];
        AudioServicesCreateSystemSoundID(url, &soundID);
        
        // 將soundID存入字典
        [_soundIDs setObject:@(soundID) forKey:soundname];
    }
    
    // 3.播放音效
    AudioServicesPlaySystemSound(soundID);
}

@end


三、音樂

1>音樂播放用到一個(gè)叫做AVAudioPlayer的類,AVAudioPlayer只能播放本地的音頻文件
AVAudioPlayer常用方法
(1)加載音樂文件

- (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
- (id)initWithData:(NSData *)data error:(NSError **)outError;

(2)準(zhǔn)備播放(緩沖,提高播放的流暢性)

- (BOOL)prepareToPlay;

(3)播放(異步播放)

- (BOOL)play;

(4)暫停

- (void)pause;

(5)停止

- (void)stop;

(6)是否正在播放

@property(readonly, getter=isPlaying) BOOL playing;

(7)時(shí)長

@property(readonly) NSTimeInterval duration;

(8)當(dāng)前的播放位置

@property NSTimeInterval currentTime;

(9)播放次數(shù)(-1代表無限循環(huán)播放,其他代表播放numberOfLoops+1次)

@property NSInteger numberOfLoops;

(10)音量

@property float volume;

(11)是否允許更改速率

@property BOOL enableRate;

(12)播放速率(1是正常速率,0.5是一般速率,2是雙倍速率)

@property float rate;

(13)有多少個(gè)聲道

@property(readonly) NSUInteger numberOfChannels;

(14)聲道(-1是左聲道,1是右聲道,0是中間)

@property float pan;

(15)是否允許測量音量

@property(getter=isMeteringEnabled) BOOL meteringEnabled;

(16)更新測量值

- (void)updateMeters;

(17)獲得當(dāng)前的平均音量

- (float)averagePowerForChannel:(NSUInteger)channelNumber;

音效、音樂工具類

#import <Foundation/Foundation.h>

@interface AudioTool : NSObject

#pragma mark - 播放音樂
// 播放音樂 musicName : 音樂的名稱
+ (void)playMusicWithMusicName:(NSString *)musicName;
// 暫停音樂 musicName : 音樂的名稱
+ (void)pauseMusicWithMusicName:(NSString *)musicName;
// 停止音樂 musicName : 音樂的名稱
+ (void)stopMusicWithMusicName:(NSString *)musicName;

#pragma mark - 音效播放
// 播放聲音文件soundName : 音效文件的名稱
+ (void)playSoundWithSoundname:(NSString *)soundname;

@end


#import "XMGAudioTool.h"
#import <AVFoundation/AVFoundation.h>

@implementation XMGAudioTool

static NSMutableDictionary *_soundIDs;
static NSMutableDictionary *_players;

+ (void)initialize
{
    _soundIDs = [NSMutableDictionary dictionary];
    _players = [NSMutableDictionary dictionary];
}

+ (void)playMusicWithMusicName:(NSString *)musicName
{
    assert(musicName);
    
    // 1.定義播放器
    AVAudioPlayer *player = nil;
    
    // 2.從字典中取player,如果取出出來是空,則對(duì)應(yīng)創(chuàng)建對(duì)應(yīng)的播放器
    player = _players[musicName];
    if (player == nil) {
        // 2.1.獲取對(duì)應(yīng)音樂資源
        NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil];
        
        if (fileUrl == nil) return;
        
        // 2.2.創(chuàng)建對(duì)應(yīng)的播放器
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
        
        // 2.3.將player存入字典中
        [_players setObject:player forKey:musicName];
        
        // 2.4.準(zhǔn)備播放
        [player prepareToPlay];
    }
    
    // 3.播放音樂
    [player play];
}

+ (void)pauseMusicWithMusicName:(NSString *)musicName
{
    assert(musicName);
    
    // 1.取出對(duì)應(yīng)的播放
    AVAudioPlayer *player = _players[musicName];
    
    // 2.判斷player是否nil
    if (player) {
        [player pause];
    }
}

+ (void)stopMusicWithMusicName:(NSString *)musicName
{
    assert(musicName);
    
    // 1.取出對(duì)應(yīng)的播放
    AVAudioPlayer *player = _players[musicName];
    
    // 2.判斷player是否nil
    if (player) {
        [player stop];
        [_players removeObjectForKey:musicName];
        player = nil;
    }
}

#pragma mark - 音效的播放
+ (void)playSoundWithSoundname:(NSString *)soundname
{
    // 1.定義SystemSoundID
    SystemSoundID soundID = 0;
    
    // 2.從字典中取出對(duì)應(yīng)soundID,如果取出是nil,表示之前沒有存放在字典
    soundID = [_soundIDs[soundname] unsignedIntValue];
    if (soundID == 0) {
        CFURLRef url = (__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:soundname withExtension:nil];
        
        if (url == NULL) return;
        
        AudioServicesCreateSystemSoundID(url, &soundID);
        
        // 將soundID存入字典
        [_soundIDs setObject:@(soundID) forKey:soundname];
    }
    
    // 3.播放音效
    AudioServicesPlaySystemSound(soundID);
}

@end

2> AVPlayer
能播放本地、遠(yuǎn)程的音頻、視頻文件
基于Layer顯示,得自己去編寫控制面板

3> MPMoviePlayerController
能播放本地、遠(yuǎn)程的音頻、視頻文件
自帶播放控制面板(暫停、播放、播放進(jìn)度、是否要全屏)

4> MPMoviePlayerViewController
能播放本地、遠(yuǎn)程的音頻、視頻文件
內(nèi)部是封裝了MPMoviePlayerController
播放界面默認(rèn)就是全屏的
如果播放功能比較簡單,僅僅是簡單地播放遠(yuǎn)程、本地的視頻文件,建議用這個(gè)

5> DOUAudioStreamer
能播放遠(yuǎn)程、本地的音頻文件
監(jiān)聽緩沖進(jìn)度、下載速度、下載進(jìn)度

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容