gpt4 book ai didi

ios - 当我从一个 View 导航到另一个 View 时,如何让音乐持续播放?

转载 作者:可可西里 更新时间:2023-11-01 02:22:50 25 4
gpt4 key购买 nike

所以,我已经掌握了在 iOS 中使用一个 View 播放声音/音乐的艺术,但我现在正在尝试为音乐艺术家制作一个更强大的应用程序。到目前为止,它涉及从“菜单”ViewController 到他的 Bio、Show Times 和“Listening Room”等。到目前为止,我创建了一个“AudioManager”类:

import UIKit
import AVFoundation
import MediaPlayer

class AudioManager: NSObject {

let defaltSong = ["Hell&BackCaf/01 Black Sheep", "caf"]
weak var delegate : PlayerDelegate?
var musicPlayer1 = AVAudioPlayer()
var trackNumber = 0

var musicAudioPath = NSBundle.mainBundle().pathForResource("Hell&BackCaf/01 Black Sheep", ofType: "caf")
var musicAudioPathURL = NSURL()
var error:NSError? = nil


var songList = [["Hell&BackCaf/01 Black Sheep", "caf"], ["Hell&BackCaf/02 Hell & Back", "caf"], ["Hell&BackCaf/03 Save Me", "caf"], ["Hell&BackCaf/04 Broken feat. Hillary Dodson", "caf"], ["Hell&BackCaf/05 Do Or Die", "caf"], ["Hell&BackCaf/06 Divided", "caf"]]

func ButtonPlay(song: NSString, type: NSString) {

error = nil

musicAudioPath = NSBundle.mainBundle().pathForResource(song as String, ofType: type as String)

musicAudioPathURL = NSURL(fileURLWithPath: self.musicAudioPath!)!

musicPlayer1 = AVAudioPlayer(contentsOfURL: musicAudioPathURL, error: &error)

musicPlayer1.prepareToPlay()

musicPlayer1.play()
}

func loadFirstSong() {

error = nil

musicAudioPath = NSBundle.mainBundle().pathForResource("Hell&BackCaf/01 Black Sheep", ofType: "caf")
musicAudioPathURL = NSURL(fileURLWithPath: self.musicAudioPath!)!
musicPlayer1 = AVAudioPlayer(contentsOfURL: musicAudioPathURL, error: &error)
if error == nil {
musicPlayer1.prepareToPlay()
} else {
println(error)
}


}


func advanceTrack(){

if trackNumber < songList.count - 1 {

self.trackNumber++

ButtonPlay(songList[trackNumber][0], type: songList[trackNumber][1])
} else {

trackNumber = 0

ButtonPlay(songList[trackNumber][0], type: songList[trackNumber][1])
}
}

func previousTrack(){

if trackNumber > 0 {

trackNumber--

ButtonPlay(songList[trackNumber][0], type: songList[trackNumber][1])

} else {

trackNumber = songList.count - 1

ButtonPlay(songList[trackNumber][0], type: songList[trackNumber][1])
}

}

func audioPlayerDidFinishPlaying(AVAudioPlayer!, successfully: Bool) {
self.delegate?.soundFinished(self)
println("song over")
}



}

然后我在我的 MusicRoomViewController 中使用了它:

import UIKit
import AVFoundation

class MusicRoomViewController: UIViewController, AVAudioPlayerDelegate {

let audioManager = AudioManager()

@IBAction func pressedBackButton(sender: UIButton) {
audioManager.previousTrack()
}

@IBAction func pressedPlayButton(sender: UIButton) {
audioManager.musicPlayer1.play()
}

@IBAction func pressedForwardButton(sender: UIButton) {
audioManager.advanceTrack()
}

override func viewDidLoad() {
super.viewDidLoad()
audioManager.loadFirstSong()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func viewWillDisappear(animated: Bool) {

if self.isMovingFromParentViewController()
{
audioManager.musicPlayer1.play()
}
}

}

但现在当我导航到应用程序的任何其他“页面”时,音乐停止了。经过研究,我知道我做错了,但我似乎无法弄清楚我需要做什么才能保持音频播放直到应用程序关闭或用户按下停止键(这还不存在,我知道)。有什么想法吗??

最佳答案

您必须将 AudioManager 类设为单例。正在发生的事情是,一旦您离开创建它的 View ,您正在创建的 AudioManager 实例就会被释放。单例对象在首次访问后创建,并将一直存在于内存中,直到应用程序的生命周期结束或它被显式释放。有关 Swift 单例的更多信息,请查看 this page .这是一个很有用的学习模式。

对AudioManager进行如下修改(取自上述网站):

private let _AudioManagerSharedInstance = AudioManager()

class AudioManager {
static let sharedInstance = AudioManager()
}

使用AudioManager.sharedInstance访问AudioManager,例如您可以调用AudioManager.sharedInstance.previousTrack()

关于ios - 当我从一个 View 导航到另一个 View 时,如何让音乐持续播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com