iOS 播放音效

來源:互聯網
上載者:User

標籤:style   blog   ar   io   color   os   sp   for   on   

音效:通俗的說是短音頻,音頻檔案必須滿足下面的條件

  1、播放的時間不能超過30秒

  2、資料必須是 PCM或者IMA4流格式

  3、必須被打包成下面三個格式之一:Core Audio Format (.caf), Waveform audio (.wav), 或者 Audio Interchange File (.aiff)

1.引入架構

 1 #import <AudioToolbox/AudioToolbox.h> 

 

2.播放音效

 1 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 2 { 3     // 聲明SoundID 4     SystemSoundID soundID ; 5     self.soundID = soundID; 6     // 擷取音效檔案的url 7     NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_10.wav" withExtension:nil]; 8     // 根據url建立 9     AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);10     AudioServicesPlaySystemSound(soundID);11 };

  記憶技巧:

  1.播放音效的核心代碼,申明音效ID,然後播放音效

   1 SystemSoundID soundID ; 2 AudioServicesPlaySystemSound(soundID); 

  2.用下面這句給soundID賦值

   1 AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID); 

  3.指派陳述式有2個參數,一個是音效檔案資源路徑,另外一個就是之前申明soundID了

   1 NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_10.wav" withExtension:nil]; 

 

3.播放完音效後,應該銷毀

1 - (void)didReceiveMemoryWarning2 {3     [super didReceiveMemoryWarning];4     AudioServicesDisposeSystemSoundID(self.soundID);5     self.soundID = 0;6 }

 

 

重構上述代碼,提供一個播放音效的工具類,重構代碼來自MJ老師

1.工具類的.h檔案

 1 #import <Foundation/Foundation.h> 2  3 @interface MJAudioTool : NSObject 4  5 /** 6  *  播放音效 7  * 8  *  @param filename 音效檔案名稱 9  */10 + (void)playSound:(NSString *)filename;11 12 /**13  *  銷毀音效14  *15  *  @param filename 音效檔案名稱16  */17 + (void)disposeSound:(NSString *)filename;18 @end

2.工具類.m檔案

 1 #import "MJAudioTool.h" 2 #import <AudioToolbox/AudioToolbox.h> 3  4 @implementation MJAudioTool 5  6 // 字典: filename作為key, soundID作為value 7 // 存放所有的音頻ID 8 static NSMutableDictionary *_soundIDDict; 9 + (void)initialize10 {11     _soundIDDict = [NSMutableDictionary dictionary];12 }13 14 + (void)playSound:(NSString *)filename15 {16     if (!filename) return;17     18     // 1.從字典中取出soundID19     SystemSoundID soundID = [_soundIDDict[filename] unsignedLongValue];20     if (!soundID) { // 建立21         // 載入音效檔案22         NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];23         24         if (!url) return;25         26         // 建立音效ID27         AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);28         29         // 放入字典30         _soundIDDict[filename] = @(soundID);31     }32     33     // 2.播放34     AudioServicesPlaySystemSound(soundID);35 }36 37 + (void)disposeSound:(NSString *)filename38 {39     if (!filename) return;40     41     SystemSoundID soundID = [_soundIDDict[filename] unsignedLongValue];42     if (soundID) {43         // 銷毀音效ID44         AudioServicesDisposeSystemSoundID(soundID);45         46         // 從字典中移除47         [_soundIDDict removeObjectForKey:filename];48     }49 }50 51 @end

3.控制器的.m檔案

播放音效

1 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event2 {3     [MJAudioTool playSound:@"buyao.wav"];4     [MJAudioTool playSound:@"m_03.wav"];5 }

銷毀音效

1 - (void)didReceiveMemoryWarning2 {3     [super didReceiveMemoryWarning];4     5     [MJAudioTool disposeSound:@"m_03.wav"];6     [MJAudioTool disposeSound:@"buyao.wav"];7 }

 

  

  

 

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.