way*_*neh 24 xcode ios mpmediaplayercontroller swift3
我正在编写一个基本的音乐播放器应用程序但在处理应用程序状态转换时遇到一些问题.
我正在使用Swift 3和MPMusicPlayerController.systemMusicPlayer()
目标是这样的:
1)当用户点击主页按钮并且应用程序进入bg(工作)时保持音乐播放
2)如果用户退出应用程序,则停止播放器(myMP.stop())(有时工作,其他时间抛出错误)
我根据可能的操作使用print语句跟踪了流程并得到了:
流程2是我所期望的,但是当应用程序关闭时,Flow 1会抛出一个错误 - 我希望"会终止".
编辑:主要问题是当使用流程1退出应用程序时,永远不会调用"将终止" - 因此永远不会调用"myMP.stop()"并且应用程序退出后播放器继续播放.
如果您在应用程序处于活动状态时单击Home一次(Flow 1)或double(Flow 2),则行为会有明显的不同.
为什么会采用哪两个不同的反应应该是相同的动作?
编辑:最重要的是,如果它永远不会"终止",如何停止MediaPlayer for Flow 1?
编辑:
以下是一些复制问题的示例代码:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
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()
}
}
Run Code Online (Sandbox Code Playgroud)
Jef*_*lin 33
"由于信号9终止"消息仅表示您的应用程序被SIGKILL信号终止.每当您的应用程序被非自愿终止时,操作系统都会发送该信号,无论是因为内存压力(或与此讨论无关的其他几个原因),还是用户通过双击主页按钮并将其轻扫而明确查杀您的应用程序.
在您的情况下,用户明确地终止您的应用程序,因此完全期望"由于信号9终止"消息.如果您的应用程序是当前的前台应用程序,则会调用applicationWillTerminate方法,如上面的逻辑流程大纲(流程2)所示.如果您的应用程序不是当前的前台应用程序(流程1),applicationWillTerminate那么如果您的应用程序处于挂起状态,则不会调用您的方法.这是预期的行为.还要注意"背景状态"和"暂停状态"之间的区别. 他们不是一回事.
因此,如果我正确地理解你,听起来问题是在用户终止你的应用程序之后音频继续播放(流程1).这意味着你在处理时遇到了错误MPMusicPlayerController,因为它应该自动处理状态转换.
确保为您的应用设置了正确的UIBackgroundMode.设置错误的后台模式可能会导致应用程序出现异常,因为操作系统仅在后台运行时允许某些操作,具体取决于您设置的背景模式.设置错误的模式(或尝试执行您设置的模式中明确禁止的操作)将导致您的应用被暂停或终止.
确保您已正确设置音频会话.
请确保您正确地响应音乐播放器的通知 -特别是,确保你调用beginGeneratingPlaybackNotifications和endGeneratingPlaybackNotifications适当的,而且你是正确处理这些通知.检查您的通知处理程序,以确保您没有在那里做任何愚蠢的事情.确保您的控制器在调用之前不会超出范围或以其他方式释放endGeneratingPlaybackNotifications.
如果你已经正确地完成了所有其他工作MPMusicPlayerController,那么它本身就可以管理,所以当你的应用程序进入后台时,你不应该做任何特别的事情来使它工作(UIBackgroundMode当然,除了设置正确之外).作为最后的手段,开始评论代码,直到您的应用程序只是一个准确的"打开音频文件并播放它"应用程序,然后查看它是否正常退出.如果是这样,您可以开始逐个取消注释代码,直到它失败 - 然后您知道应用程序的哪个部分导致了问题,您可以从那里缩小范围.
在Settings应用程序中更改应用程序权限(例如Camera或Photo使用)可能会导致此信号崩溃(硬刷新)。
您可以在以下链接中找到相关行为:
| 归档时间: |
|
| 查看次数: |
29553 次 |
| 最近记录: |