我正在编写一个基本的音乐播放器应用程序,但在处理应用程序状态转换时遇到了一些问题。
我正在使用 Swift 3 和 MPMusicPlayerController.systemMusicPlayer()
目标是这样的:
1)当用户点击主页按钮并且应用程序进入bg时保持音乐播放(有效)
2)如果用户退出应用程序,则停止播放器( myMP.stop() )(有时有效,其他时候抛出错误)
我根据可能的操作使用打印语句跟踪流程并得到以下信息:
流程 2 是我所期望的,但流程 1 在应用程序关闭时会引发错误 - 我希望在这里“将终止”。
编辑:主要问题是当使用 Flow 1 退出应用程序时,永远不会调用“将终止” - 因此永远不会调用“myMP.stop()”,并且播放器在应用程序退出后继续播放。
如果您在应用程序处于活动状态时单击主页一次(流程 1)或两次(流程 2),行为会有明显差异。
为什么我会从应该是相同的操作中得到两个不同的响应?
编辑:最重要的是,如果 Flow 1 的 MediaPlayer 永远不会“将终止”,我该如何停止它?
编辑:
这是一些复制问题的示例代码:
AppDelegate.swift
//
// AppDelegate.swift
// Jumbo Player
//
import UIKit
//import MediaPlayer
//doesn't matter where this is declared - here or in ViewController - same results
//let myMP:MPMusicPlayerController = MPMusicPlayerController.systemMusicPlayer()
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
print("applicationWillResignActive")
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
print("applicationDidEnterBackground")
}
func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
print("applicationDidReceiveMemoryWarning")
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
print("applicationWillEnterForeground")
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
print("applicationDidBecomeActive")
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
print("applicationWillTerminate")
myMP.stop();
}
}
ViewController.swift
//
// ViewController.swift
// junkplayer
//
import UIKit
import MediaPlayer
let myMP:MPMusicPlayerController = MPMusicPlayerController.systemMusicPlayer()
class ViewController: UIViewController {
@IBOutlet weak var xxx: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let qrySongs = MPMediaQuery.songs()
myMP.setQueue(with: qrySongs)
}
@IBAction func playbut(_ sender: UIButton) {
myMP.play()
}
}