「Swift學習筆記」使用AV Foundation架構為APP加上背景音樂並進行簡單控制

來源:互聯網
上載者:User

標籤:http   io   ar   os   使用   sp   for   strong   檔案   

用Swift語言進行iOS開發,如需加上背景音樂並進行簡單地控制可以使用AV Foundation架構,而對於壓縮過的Audio檔案或者超過30秒的音頻檔案,則使用AVAudioPlayer類。

為APP加上背景音樂

?   首先,在ViewController.swift的檔案頂部添加?行代碼:

import AVFoundation

//引入一個新的AVFoundation架構

?   其次,在類中定義其它執行個體變數的代碼下?再添加?行代碼:

var audioPlayer: AVAudioPlayer!

//添加一個AVAudioPlayer類型的播放器變數

?   添加一個新的方法:

func playBgMusic(){

let musicPath = NSBundle.mainBundle().pathForResource("bgmusic", ofType: "mp3")

//指定音樂路徑

let url = NSURL(fileURLWithPath: musicPath!)

audioPlayer = AVAudioPlayer(contentsOfURL: url, error: nil)

audioPlayer.numberOfLoops = -1

//設定音樂播放次數,-1為迴圈播放

maudioPlayer.volume = 1

//設定音樂音量,可用範圍為0~1

audioPlayer.prepareToPlay()

audioPlayer.play()

}

有關AVAudioPlayer類的更多資訊,請參見AVAudioPlayer類參考。

?   在適當位置調用函數:

override func viewDidLoad() {

super.viewDidLoad()

playBgMusic()

}

//每次視圖控制器載入時調用函數

override func viewWillAppear(animated: Bool) {

playBgMusic()

}

//每次視圖控制器的視圖出現前調用函數

viewwillappear是每次視圖控制器的視圖出現前執行的代碼。而viewdidload是每次視圖控制器載入是執行的代碼。 比如說:當a視圖控制器的視圖第一次出現是兩個都要執行,但當a被push後有pop回來時,只有viewwillappear執行。

使用音樂開關按鈕對音樂播放進行控制

對AVAudioPlayer對象執行播放、暫停、或者停止操作,可以通過playing屬性來檢測當前是否現正播放。以下方法實現了使用UIButton對音樂進行播放/暫停控制,並更新了按鈕的圖片顯示。

?   在類中定義按鈕變數:

@IBOutlet weak var voiceButton: UIButton!

?   在viewDidLoad()為voiceButton添加動作:

 voiceButton.addTarget(self, action:"voice", forControlEvents: .TouchUpInside)

?   添加voice方法:

func voice() {

 if !audioPlayer.playing { 

 audioPlayer.play() 

 voiceButton.setImage(UIImage(named:"[email protected]"),forState:.Normal)

//如果背景音樂沒有播放,令其開始播放並將按鈕顯示為播放圖片

 }else{ 

 audioPlayer.stop()

 voiceButton.setImage(UIImage(named:"[email protected]"), forState:.Normal)

//如果背景音樂現正播放,令其停止播放並將按鈕顯示為靜音圖片

 } 

}

以上操作也可直接對按鈕關聯方法。

AB頁面反覆跳轉,背景音樂僅調用一次

在正常情況下,如果只對A頁面進行背景音樂設定,跳轉至B頁面時音樂不停止可正常播放,但返回A頁面時會再次播放一層音樂,此時可以先對audioPlayer是否現正播放音樂進行判斷:

if !audioPlayer.playing { 

audioPlayer.play() 

}

//如果audioplayer沒有播放,則令其開始播放

A頁面跳轉至B頁面,背景音樂正常播放;B頁面跳轉至C頁面,背景音樂切換

可以利用頁面傳值進行解決,適用於背景音樂不同的多頁面向B頁面進行跳轉,音樂不切換,但B頁面向C頁面跳轉需要進行音樂切換的情況。

? 事先為A、B、C頁面分別引入AVFoundation架構,並在A頁面添加avcAudioPlayer、B頁面添加bvcAudioPlayer、C頁面添加bvcAudioPlayer和cvcAudioPlayer。

? 在A、B頁面viewDidLoad()為跳轉按鈕添加動作:

 turnButton.addTarget(self, action:"turn", forControlEvents: .TouchUpInside) 

? 在A頁面添加turn方法 :

 func turn(){ 

 var sb = UIStoryboard(name: "Main", bundle:nil) 

//使用Storyboard製作頁面使用此方法進行跳轉 

 var bvc = sb.instantiateViewControllerWithIdentifier("B") as BViewController

 bvc.bvcAudioPlayer = self.avcAudioPlayer 

//頁面傳值

self.presentViewController(bvc, animated: true, completion: nil)

//頁面跳轉

 } 

B頁面跳轉至C頁面(cvc)方法相同。

? 在C頁面對B頁面bvcaudioplayer是否播放進行判斷並調用自己的背景音樂。

if (bvcAudioPlayer != nil) { 

 bvcAudioPlayer.stop() 

 playBgMusic()

} else {

playBgMusic()

}

//需要提前為C頁面定義playBgMusic方法,播放器為cvcAudioPlayer


 



「Swift學習筆記」使用AV Foundation架構為APP加上背景音樂並進行簡單控制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.