iOS 開發之音頻播放

來源:互聯網
上載者:User

標籤:

 

前段時間做項目用到音頻播放功能,在網上也查了好多資料,最後終於搞懂他們的原理.

本文是借鑒別人的,網址是:http://www.jb51.net/article/74666.htm 

小子之所以還要寫這篇部落格,是為了自己以後能方便查詢,也方便大家查閱,如果原作者認為小子有不妥的地方,請留言聯絡我,我會刪博的....~_~

a,音頻播放我們使用的AVAudioPlayer ,AVAudioPlayer是屬於AVFoudation.framework架構之中的,所以在我們使用的時候需要把AVFoudation.framework架構添加到項目中.

b,音頻播放又分為2中:

  (1),較短的音頻播放,一般播放時間在1~2秒;

  (2),相對比較長的音頻播放;

一,這裡先寫較短音訊播放

  1,首先添加項目需要的依賴庫AVFoudation.framework (同時記得匯入標頭檔 #import <AVFoundation/AVFoundation.h>)

  2,擷取音訊路徑

    

1 //擷取音頻路徑2     NSString * musicPath = [[NSBundle mainBundle]pathForResource:@"aaa" ofType:@"m4a"];

  3,建立音訊url路徑

1     //建立音訊url路徑2     NSURL * musicUrl = [[NSURL alloc]initFileURLWithPath:musicPath];

  4,載入音效檔案,同時建立音效ID

1 //載入音效檔案 ,建立音效id  一個id 對應一個音效檔案2     SystemSoundID soundID = 0;3     AudioServicesCreateSystemSoundID((__bridge CFURLRef)musicUrl, &soundID);

  5,播放音效檔案

1 //播放音效檔案2     //    AudioServicesPlayAlertSound(soundID); //伴隨有震動效果3     AudioServicesPlaySystemSound(soundID);

二,這裡寫較長時間的音頻播放 (這個我就照搬原作者的了)

  較長時間的播放用到一個叫做AVAudioPlayer的類,這個類可以用於播放手機本地的音樂檔案。

注意:

  (1)該類(AVAudioPlayer)只能用於播放本地音頻。

  (2)時間比較短的音頻使用AudioServicesCreateSystemSoundID來建立,而本地時間較長的音頻使用AVAudioPlayer類。

  同樣需要匯入AVFoundation架構,匯入標頭檔(#import <AVFoundation/AVFoundation.h>)

  

1 //2 //  YYViewController.m3 //  15-播放音樂4 //5 #import "YYViewController.h"6 #import <AVFoundation/AVFoundation.h>7 @interface YYViewController ()8 @end
 1 @implementation YYViewController 2 - (void)viewDidLoad 3 { 4     [super viewDidLoad]; 5      6 } 7 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 8 { 9     10     //1.音頻檔案的url路徑11     NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];12     13     //2.建立播放器(注意:一個AVAudioPlayer只能播放一個url)14     AVAudioPlayer *audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];15     16     //3.緩衝17     [audioPlayer prepareToPlay];18     19     //4.播放20     [audioPlayer play];21 }22 @end

代碼說明:運行程式,點擊模擬器介面,卻並沒有能夠播放音頻檔案,原因是代碼中建立的AVAudioPlayer播放器是一個局部變數,應該調整為全域屬性。

 

可將代碼調整如下,即可播放音頻:

1 #import "YYViewController.h"2 #import <AVFoundation/AVFoundation.h>3 @interface YYViewController ()4 @property(nonatomic,strong)AVAudioPlayer *audioplayer;5 @end
 1 @implementation YYViewController 2 - (void)viewDidLoad 3 { 4     [super viewDidLoad]; 5      6 } 7 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 8 { 9     10     //1.音頻檔案的url路徑11     NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];12     13     //2.建立播放器(注意:一個AVAudioPlayer只能播放一個url)14     self.audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];15     16     //3.緩衝17     [self.audioplayer prepareToPlay];18     19     //4.播放20     [self.audioplayer play];21 }22 @end

注意:一個AVAudioPlayer只能播放一個url,如果想要播放多個檔案,那麼就得建立多個播放器。

 

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.