iOS 音訊開發經驗彙總
http://blog.csdn.net/work4blue/article/details/47841317
一.音樂播放類概念
iOS 下能支援歌曲和聲音播放的的類有幾個:
- SystemSound
-
AVFoundtion庫中的AVAudioPlayer #重要
-
MediMPMusicPlayerController
常用音訊控制元件
3. MPMediaPickerController 本地音樂庫選擇器
5. MPVolumeView 播放進度條
這裡有一個PPT在解釋幾種概念:
聲音視覺化的設計
如果想要程式中輸出聲音,波形,頻譜以及其它特效,
一定要看一下這一篇教程:
iPodVisualizer
它是種用AVAudioPlayer 的averagePowerForChannel 這樣介面來輸出波形檔案。
MPMusicPlayerController沒有發現支援這一功能
aurioTouch
另外Apple官方給出一個輸出例子:aurioTouch 錄音資料的波形,其中帶普通波形檔案,以及經過FFT運算得到頻譜資料。可以參考。
PitchDetector
SpeakHere
AvTouch
選擇系統歌曲檔案
音樂App的歌曲來源有三種,一個是本地沙盒自帶歌曲,另一個網路歌曲,
選擇系統音樂庫
第三個就係統音樂庫帶的歌曲,
它可以由 MPMediaPickerController 類來呼叫
- (IBAction)addPressed:(id)sender {
MPMediaType mediaType = MPMediaTypeMusic;
MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:mediaType];
picker.delegate = self;
[picker setAllowsPickingMultipleItems:YES];
picker.prompt = NSLocalizedString(@"Select items to play", @"Select items to play");
[self presentViewController:picker animated:YES completion:nil];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
它通過MPMediaPickerControllerDelegate介面返回一個
MPMediaItemCollection 選擇的歌曲列列表,只需對MPMediaPickerController呼叫如下介面即可進行播放,以及上一首,下一首
[self.player setQueueWithItemCollection:self.collection];
如果是AVAudioPlayer來播放需要做得更多一點。即可把MPMediaItemCollection裡歌曲URL取出來,直接傳給AVAudioPlayer即可,這個URL格式類似於
ipod-library://item/item.m4a?id=1529654720874100371
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) collection {
MPMediaItem *item = [[collection items] objectAtIndex:0];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
// Play the item using AVPlayer
self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.avAudioPlayer play];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
列表播放
MMMediaPlayer是自動支援,播放下一首,上一首可用如下方法:
上一首:
- (IBAction)rewindPressed:(id)sender {
if ([self.player indexOfNowPlayingItem] == 0) {
[self.player skipToBeginning];
} else {
[self.player endSeeking];
[self.player skipToPreviousItem];
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
下一首:
- (IBAction)fastForwardPressed:(id)sender {
NSUInteger nowPlayingIndex = [self.player indexOfNowPlayingItem];
[self.player endSeeking];
[self.player skipToNextItem];
if ([self.player nowPlayingItem] == nil) {
if ([self.collection count] > nowPlayingIndex+1) {
// added more songs while playing
[self.player setQueueWithItemCollection:self.collection];
MPMediaItem *item = [[self.collection items] objectAtIndex:nowPlayingIndex+1];
[self.player setNowPlayingItem:item];
[self.player play];
}
else {
// no more songs
[self.player stop];
NSMutableArray *items = [NSMutableArray arrayWithArray:[self.toolbar items]];
[items replaceObjectAtIndex:3 withObject:self.play];
[self.toolbar setItems:items];
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
暫停和恢復播放:
- (IBAction)playPausePressed:(id)sender {
[self.pause setTintColor:[UIColor blackColor]];
MPMusicPlaybackState playbackState = [self.player playbackState];
NSMutableArray *items = [NSMutableArray arrayWithArray:[self.toolbar items]];
if (playbackState == MPMusicPlaybackStateStopped || playbackState == MPMusicPlaybackStatePaused) {
[self.player play];
[items replaceObjectAtIndex:3 withObject:self.pause];
} else if (playbackState == MPMusicPlaybackStatePlaying) {
[self.player pause];
[items replaceObjectAtIndex:3 withObject:self.play];
}
[self.toolbar setItems:items animated:NO];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
AVAudioPlayer 有播放結束的呼叫,因此在的上一首播完,重設一下一首歌即可
音樂後臺播放
如果需要後臺播放音樂,需要在應有的info.plist宣告
否則會被系統強行幹掉。
網路音訊播放
AVAudioPlayer 不直接支援網路音訊播放,可以先下載資料到一個NSData ,然後進行播放。
NSData *mydata=[[NSDataalloc]initWithContentsOfURL:[NSURLURLWithString:command]];
AVAudioPlayer *player=[[AVAudioPlayeralloc]initWithData:mydata error:nil];
[player prepareToPlay];
[player play];
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
但這種對於流媒體就無能為例了。因此可以使用更新的AVPlayer,它能直接播放(但是底層介面較少)
AVPlayer * _player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:@"http://stream.jewishmusicstream.com:8000"]];
- 1
- 1
音樂按鍵控制
AudioPlayer 可以接收 線控及藍芽耳機的按鍵控制,
前題的要真的有一首的歌曲播放時,才能捕獲按鍵。
這個可以在AppDelegate 中remoteControlReceivedWithEvent來捕獲各種按鍵。
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
NSLog(@"remoteControlReceivedWithEvent %d",event.subtype);
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self postNotificationWithName:remoteControlPlayButtonTapped];
break;
case UIEventSubtypeRemoteControlPause:
[self postNotificationWithName:remoteControlPauseButtonTapped];
break;
case UIEventSubtypeRemoteControlStop:
[self postNotificationWithName:remoteControlStopButtonTapped];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self postNotificationWithName:remoteControlForwardButtonTapped];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self postNotificationWithName:remoteControlBackwardButtonTapped];
break;
default:
[self postNotificationWithName:remoteControlOtherButtonTapped];
break;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
理論上,可以在後臺不斷播放一段音訊(音量設為0)來實現在應用中捕獲耳機按鍵。比如藍點工坊就用這個方法把藍芽耳機充當自拍器用。
但是這種用法是過不了App Store 的稽核 ,所以只能用Ad hoc發行方法。
它是在後臺不斷播放一個網路電臺節目來達到目的,實際藍點工坊測試測放App自帶一個聲音檔案,如caf效果是一樣的。
相關推薦
iOS 音訊開發經驗彙總
http://blog.csdn.net/work4blue/article/details/47841317 一.音樂播放類概念 iOS 下能支援歌曲和聲音播放的的類有幾個: SystemSound AVFoundtion庫中的AVAudioPlayer #重要
iOS 音訊開發AVAudioPlayer的使用,音效播放AudioServicesPlaySystemSound使用
音訊開發AVAudioPlayer的使用 AVAudioPlayer是iOS系統自帶的可以播放本地mp3檔案的一個類,(注意:只能播放本地) 參考:http://www.jianshu.com/p/589999e53461 1,先匯入庫AVFoundation/AVFoundati
IOS音訊開發
首先學習音訊開發之前,必須瞭解音訊的一些基礎知識,比如檔案格式與資料格式(編碼格式),位元率,取樣率,軌道,聲道,FFT(傅立葉快速變化),頻譜等。查了網上很多資料,到現在還是一知半解啊。。。擦擦擦。。。。 接著我們要整體瞭解下ios為我們提供處理音訊的基礎技術:核心
多年iOS開發經驗總結
pla tostring 技術 draw zed 權限 provide cst gre 1、禁止手機睡眠 [UIApplication sharedApplication].idleTimerDisabled = YES; 2、隱藏某行cell - (CG
iOS開發經驗分享:UITableViewCell復用問題
owa 添加 gre 現在 ack ret container con ext 很多朋友覺得UITableViewCell復用問題很難處理,百思不得其解,甚至有很多朋友自己琢磨很久也不明白個究竟。現在分享一下個人的一些經驗,希望對大家有幫助,如果有好的意見或者
iOS開發經驗總結
圓形 service selector prot 區域 location dsi layout tty 1、設置UILabel行間距 NSMutableAttributedString* attrString = [[NSMutableAttributedString
iOS 項目的文件夾結構能看出你的開發經驗
add pop div 子文件夾 line ren -m cti mar 近期有師弟去面試iOS開發,他談論到,面試官既然問他怎麽分文件夾結構的,並且還詳細問到每一個子文件夾的文件名稱。 文件夾結構確實非常重要。面試官問他這些無疑是想窺探他的開
多年iOS開發經驗總結(轉)
1、設定UILabel行間距 NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:label.text];
iOS 開發經驗 - 轉載
1、禁止手機睡眠 [UIApplication sharedApplication].idleTimerDisabled = YES;2、隱藏某行cell - (CGFloat)tableView:(UITableView *)tableView heightForRo
react開發,日常經驗彙總
npm升級package.json依賴包到最新版本號 使用工具包:npm-check-updates 全域性安裝ncu:npm install -g npm-check-updates 使用 檢查package.json中dependencies的最新版本:ncu
iOS程式碼程式設計規範 根據專案經驗彙總
帶出幾十位從零開始學iOS的實習生或試用期的開發人員後,覺得真的是千人千面,每個人寫的程式碼都風格迥異,如果沒有一個文件規範,每次都和新人進行口頭的說教,大概自己是不用敲程式碼了,所以吃了虧了就開始編寫iOS的程式設計規範。由於本人在寫iOS程式碼前一直是C語言的開發,所以很多規範都受C語言的影響。
iOS開發經驗總結2
整理了下這個幾年的筆記,看到很多的知識點都是iOS7, iOS6,iOS5的,更新換代好快啊。僅僅來回味下常用到基礎點,大神們請繞行。有不對的地方請大家指出,會及時修改。 一、調節UINavigationBar的leftBarButtonItem離左邊的距離 (iOS11 不可用) U
微信小程式開發經驗總結(遇到的坑和問題彙總)
小編推薦:Fundebug專注於JavaScript、微信小程式、微信小遊戲,Node.js和Java實時BUG監控。真的是一個很好用的bug監控費服務,眾多大佬公司都在使用。 前言: 前段時間公司打算做一款基於線下服務的小程式,涉及到掃碼支付,地圖,充值,會員體系等功能。由於
Xcode IOS開發錯誤彙總
【錯誤資訊】真機除錯時出現如下錯誤:Code Signing ErrorSigning for "*****" requires a development team.Select a development team in the project editor.Code s
多年iOS開發經驗總結(一)
總結了幾個月的東西終於能和大家分享了,不多說,直接看東西! 1、禁止手機睡眠 [UIApplication sharedApplication].idleTimerDisabled = YES; 2、隱藏某行cell - (CGFloat)
iOS開發經驗分享:如何在業餘時間開發出百萬使用者的 App?
有過不少做開發的朋友跟我說想在業餘時間做些有趣的個人專案,一來練練技術,萬一專案火了,也額外賺一筆額外之財,運氣好的話說不定還整個小小財務自由呢。事實上,作為一個屌絲程式設計師,我之前也認識過不少朋友趕上移動網際網路發展的好時候,我自己身邊也有不少屌絲程式設計師朋友、愛各種折
資料中心效能指標採集及彙總需求開發經驗
在這次開發中吸取到的最重要的經驗就是 關於軟體架構的設計,及面向物件的思想。 在最開始的設計中,我將資料中心的儲存,CPU,記憶體的使用率當作物件去設計,而沒有考慮到他們本身是資料中心的屬性,而我最終要彙總分析的是各個資料中心的所有屬性,而不是將每一個屬性進行單獨的計算。所
三年iOS開發經驗程式媛帶你專案實戰(第一篇建立專案)
這篇文章主要是是給零基礎的小夥伴看的 開發前的的準備工作 一個mac電腦肯定是必備的,appstore下載xcode開發工具,如果是學習的話這就夠了,但是我們最終的目的,還是去公司找一個iOS開發的工作,所以我這裡以企業應用做示例,所以得準備一個iphone手機,
給1~3年iOS開發 經驗朋友們的一些建議(附BAT面試題)
拉鋸戰 高考 關系 很慢 發展 面試經驗 機會 考研 焦慮 前言 由於筆者是做 iOS 開發的,因此本文也僅對做 iOS 的同行們有針對性,其他方向僅供參考。 1,如果你: 1~3年左右工作經驗,本科,非計算機相關科班出生,學校又比較一般。 實習企業不理想沒有簽,校
【iOS越獄開發】怎樣將應用打包成.ipa文件
ria font 配置文件 例如 方法 col stat pack 應該 在項目開發中。我們經常須要將project文件打包成.ipa文件。提供給越獄的iphone安裝。 以下是一種方法: 1、首先應該給project安裝好配置文件(這裏不再敖述),在ios de