iOS開發——進階篇——音頻

來源:互聯網
上載者:User

標籤:

一、簡介

簡單來說,音頻可以分為2種
音效
又稱“短音頻”,通常在程式中的播放時間長度為1~2秒
在應用程式中起到點綴效果,提升整體使用者體驗

音樂
比如遊戲中的“背景音樂”,一般播放時間較長

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

 

二、音效

1、音效的播放

// 1.獲得音效檔案的路徑NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];// 2.載入音效檔案,得到對應的音效IDSystemSoundID soundID = 0;AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);// 3.播放音效AudioServicesPlaySystemSound(soundID);

音效檔案只需要載入1次

 

2、音效播放常見函數總結
載入音效檔案

AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)

釋放音效資源
AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)

播放音效
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)

播放音效帶點震動
AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)

 

三、音樂

1、音樂的播放
音樂播放用到一個叫做AVAudioPlayer的類

AVAudioPlayer常用方法
載入音樂檔案
- (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
- (id)initWithData:(NSData *)data error:(NSError **)outError;

準備播放(緩衝,提高播放的流暢性)
- (BOOL)prepareToPlay;

播放(非同步播放)
- (BOOL)play;

暫停
- (void)pause;

停止
- (void)stop;

是否現正播放
@property(readonly, getter=isPlaying) BOOL playing;

時間長度
@property(readonly) NSTimeInterval duration;

當前的播放位置
@property NSTimeInterval currentTime;

播放次數(-1代表無限迴圈播放,其他代表播放numberOfLoops+1次)
@property NSInteger numberOfLoops;

音量
@property float volume;

是否允許更改速率
@property BOOL enableRate;

播放速率(1是正常速率,0.5是一般速率,2是雙倍速率)
@property float rate;

有多少個聲道
@property(readonly) NSUInteger numberOfChannels;

聲道(-1是左聲道,1是右聲道,0是中間)
@property float pan;

是否允許測量音量
@property(getter=isMeteringEnabled) BOOL meteringEnabled;

更新測量值
- (void)updateMeters;

獲得當前的平均音量
- (float)averagePowerForChannel:(NSUInteger)channelNumber;

iOS開發——進階篇——音頻

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.