![](/img/trans.png)
[英]How to mute AVAudioPlayer if music from another app is playing in spritekit
[英]Stopping or Playing AVAudioPlayer From Another Class - App Crashes - Swift
我在AppDelegate中设置了一个AVAudioPlayer,可以在启动时播放。 它继续在整个应用程序中播放,但是当我尝试停止或在不同的类中播放播放器时,应用程序在该行上与EXC_BAD_ACCESS崩溃。
AppDelegate.swift
import AVFoundation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var path = NSBundle.mainBundle().pathForResource("backgroundMusic", ofType: "wav")
var soundTrack = AVAudioPlayer()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
soundTrack = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path), error: nil)
soundTrack.numberOfLoops = -1
soundTrack.volume = 0.35
soundTrack.play()
return true
}
OtherClass.swift
func aFunction() {
let appDelegate = AppDelegate()
appDelegate.soundTrack.stop() //Here is where the app crashes. Same with .play()
}
谢谢你的帮助!
通过执行AppDelegate()
您将创建一个新的app delegate实例; 变量尚未启动的变量。 您想获得UiApplications共享委托:
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.