xcode 退出应用程序导致错误“来自调试器的消息:因信号9”而终止

oogrdqng  于 2023-04-22  发布在  其他
关注(0)|答案(4)|浏览(362)

我正在编写一个基本的音乐播放器应用程序,但在处理应用程序状态转换时遇到了一些问题。
我正在使用Swift 3和MPMusicPlayerController.systemMusicPlayer()
我们的目标是:
1)当用户点击Home按钮和应用程序进入bg(工作)时,保持音乐播放
2)停止播放器(myMP.stop()),如果用户退出应用程序(有时工作,其他时候抛出错误)
我根据可能的操作使用print语句跟踪流,得到了以下结果:

流程2是我所期望的,但流程1在应用程序关闭时抛出错误-我希望在这里“将终止”。
编辑:主要问题是,当使用Flow 1退出应用程序时,“will terminate”从未被调用-因此“myMP.stop()”从未被调用,并且播放器在应用程序退出后继续播放。
如果在应用处于活动状态时单击主视图一次(流程1)或双击主视图两次(流程2),则行为会有明显差异。
为什么我从应该是相同的动作中得到两个不同的响应?
编辑:最重要的是,如果流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();
    }
}

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()
    }
}

在此下载项目:www.NextCoInc.com/public/junkplayer.zip

a2mppw5e

a2mppw5e1#

“由于信号9而终止”消息只是意味着您的应用被SIGKILL信号终止。每当您的应用被非自愿终止时,操作系统都会发送该信号,无论是因为内存压力(或其他几个与此讨论无关的原因),还是用户通过双击Home按钮并将其清除而明确终止您的应用。
在您的示例中,用户明确地终止了您的应用程序,因此完全可以预期“Terminated due to signal 9”消息。(流程2)如果您的应用程序不是当前前台应用程序(流程1),如果应用程序处于挂起状态,则不会调用applicationWillTerminate方法。这是预期行为。还要注意“后台状态”和“suspended state”. They are not the same thing .
所以如果我理解正确的话,听起来问题是在用户终止应用程序后音频继续播放(流程1)。这意味着您在处理MPMusicPlayerController时做错了什么,因为它应该自动处理状态转换。
请确保为应用设置了正确的UIBackgroundMode。设置错误的后台模式可能会导致应用行为不当,因为操作系统仅允许在后台执行某些操作,具体取决于您设置的后台模式。设置错误的模式(或尝试执行您设置的模式中明确禁止的操作)将导致应用挂起或终止。
确保您已正确设置音频会话。
请确保您正确地响应了music player notifications-特别是,请确保您正确地调用了beginGeneratingPlaybackNotificationsendGeneratingPlaybackNotifications,并且正确地处理了这些通知。检查您的通知处理程序,以确保您没有在那里做任何愚蠢的事情。请确保您的控制器在您调用endGeneratingPlaybackNotifications之前没有超出范围或以其他方式被释放。
如果您已经正确地完成了其他所有操作,那么MPMusicPlayerController基本上可以自行管理,因此当您的应用程序转到后台时,您不需要做任何特殊的操作来使其正常工作(当然,除了设置正确的UIBackgroundMode之外)。作为最后的手段,开始注解代码,直到你的应用程序只是一个准系统的“打开音频文件并播放它”应用程序,然后查看它是否正确退出。如果是,您可以开始逐段取消注解代码,直到它失败-然后您就知道应用程序的哪个部分导致了问题,并且可以从那里缩小范围。

1cosmwyk

1cosmwyk2#

Settings应用中更改应用权限(如CameraPhoto使用情况)可能会导致崩溃(硬刷新)并发出此信号。
您可以在下面的链接中找到相关行为:

kgsdhlau

kgsdhlau3#

我有三个后台任务的应用程序。

<key>UIBackgroundModes</key>
<array>
    <string>bluetooth-central</string>
    <string>location</string>
    <string>remote-notification</string>
</array>

Message from debugger: Terminated due to signal 9当应用程序在后台运行时,会出现此消息,并且会消耗更多内存,超出iPhone操作系统为后台运行应用程序分配的内存。
在我的情况下,我不断地更新用户的位置和执行用户到服务器的位置API.它消耗了大量的内存.由于这个原因,操作系统杀死了应用程序.
由于操作系统的内存压力,我们得到了这个消息,并在后台杀死了应用程序。
我优化了代码,每当我们需要更新用户的位置时,只有我们向服务器发送位置API。我还enable \ disable标志allowsBackgroundLocationUpdates

if #available(iOS 9.0, *) {
   coreLocationManager.allowsBackgroundLocationUpdates = false
}

根据我们的需要。它工作得很好。

lsmepo6l

lsmepo6l4#

当设备低电量模式打开时,应用程序经常收到此消息。希望这对您有帮助

相关问题