Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 切换视图控制器时连续播放音乐-Swift 4.2_Ios_Swift - Fatal编程技术网

Ios 切换视图控制器时连续播放音乐-Swift 4.2

Ios 切换视图控制器时连续播放音乐-Swift 4.2,ios,swift,Ios,Swift,我正在尝试将背景音乐应用到我在Xcode中制作的应用程序中,我很难找到在我正在使用的当前版本Swift(4.2)中工作的解决方案。 当前,当我从一个视图控制器切换到另一个视图控制器时,音乐将重新启动,但在我关闭视图控制器时不会发生这种情况 我将发布我目前正在编写的代码: ViewController.swift import UIKit import AVFoundation class ViewController: UIViewController { var audioPlayer :

我正在尝试将背景音乐应用到我在Xcode中制作的应用程序中,我很难找到在我正在使用的当前版本Swift(4.2)中工作的解决方案。 当前,当我从一个视图控制器切换到另一个视图控制器时,音乐将重新启动,但在我关闭视图控制器时不会发生这种情况

我将发布我目前正在编写的代码:

ViewController.swift

import UIKit
import AVFoundation

class ViewController: UIViewController {

var audioPlayer : AVAudioPlayer?
var selectedSoundFileName : String = ""
let phonicSoundArray = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "ai", "ar", "ch", "ck", "ee", "ie", "ng", "oa", "oi", "oo", "or", "ou", "ph", "qu", "sh", "ss", "th", "ue"]

override func viewDidLoad() {
    super.viewDidLoad()
    MusicHelper.sharedHelper.playBackgroundMusic()
}


@IBAction func dismissCurrentView(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
}

//== Phonic audio playback =================
@IBAction func phonicPracticeButtonPressed(_ sender: AnyObject) {

    selectedSoundFileName = phonicSoundArray[sender.tag - 1]+".mp3"

    let path = Bundle.main.path(forResource: selectedSoundFileName, ofType:nil)!
    let url = URL(fileURLWithPath: path)

    do {
        audioPlayer = try AVAudioPlayer(contentsOf: url)
        audioPlayer?.play()
    } catch {
        print("Couldn't load audio")
    }
}
}
import Foundation
import AVFoundation

class MusicHelper {
static let sharedHelper = MusicHelper()
var audioPlayer: AVAudioPlayer?

func playBackgroundMusic() {
    let aSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: "BGM", ofType: "mp3")!)
    do {
        audioPlayer = try AVAudioPlayer(contentsOf:aSound as URL)
        audioPlayer!.numberOfLoops = -1
        audioPlayer!.prepareToPlay()
        audioPlayer!.play()
    }
    catch {
        print("Cannot play the file")
    }
}
}
BGMSingleton.swift

import UIKit
import AVFoundation

class ViewController: UIViewController {

var audioPlayer : AVAudioPlayer?
var selectedSoundFileName : String = ""
let phonicSoundArray = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "ai", "ar", "ch", "ck", "ee", "ie", "ng", "oa", "oi", "oo", "or", "ou", "ph", "qu", "sh", "ss", "th", "ue"]

override func viewDidLoad() {
    super.viewDidLoad()
    MusicHelper.sharedHelper.playBackgroundMusic()
}


@IBAction func dismissCurrentView(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
}

//== Phonic audio playback =================
@IBAction func phonicPracticeButtonPressed(_ sender: AnyObject) {

    selectedSoundFileName = phonicSoundArray[sender.tag - 1]+".mp3"

    let path = Bundle.main.path(forResource: selectedSoundFileName, ofType:nil)!
    let url = URL(fileURLWithPath: path)

    do {
        audioPlayer = try AVAudioPlayer(contentsOf: url)
        audioPlayer?.play()
    } catch {
        print("Couldn't load audio")
    }
}
}
import Foundation
import AVFoundation

class MusicHelper {
static let sharedHelper = MusicHelper()
var audioPlayer: AVAudioPlayer?

func playBackgroundMusic() {
    let aSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: "BGM", ofType: "mp3")!)
    do {
        audioPlayer = try AVAudioPlayer(contentsOf:aSound as URL)
        audioPlayer!.numberOfLoops = -1
        audioPlayer!.prepareToPlay()
        audioPlayer!.play()
    }
    catch {
        print("Cannot play the file")
    }
}
}

假设你在两个你发布的相同的风投之间切换

无论何时你与一位风投合作
MusicHelper.sharedHelper.playBackgroundMusic()
在其视图didload中,您的音乐将重新启动

viewDidLoad在每个VC生命周期中发生一次。当你解雇VC时,你没有调用它的viewDidLoad,这就是音乐没有重新启动的原因

更多关于VC生命周期的信息


谢谢您的回答。什么位置更适合代码:MusicHelper.sharedHelper.playBackgroundMusic()@BillyBoampong取决于您希望背景音乐从何处开始。将它放在viewDidLoad中很好,但我不知道为什么您会选择同一个VC。有些VC没有附带的swift文件,只通过脚本界面进行管理,因为它们是简单的导航屏幕,不需要传递数据。如果我想继续播放背景音乐,我需要为每个VC提供一个swift文件,这是对的吗?我想了一下,把它从ViewController.swift中取出,放在AppDelegate.swift下:
func应用程序(\uApplication:
UIApplication,didFinishLaunchwithOptions:[UIApplication.LaunchOptionsKey:Any]?)->Bool
{MusicHelper.sharedHelper.playBackgroundMusic()
//应用程序启动后覆盖自定义点。返回true}
现在似乎工作正常,感谢您的帮助。