草庐IT

ios - 退出应用程序导致错误 "Message from debugger: Terminated due to signal 9"

coder 2023-07-25 原文

我正在编写一个基本的音乐播放器应用程序,但在处理应用程序状态转换时遇到了一些问题。

我正在使用 Swift 3 和 MPMusicPlayerController.systemMusicPlayer()

目标是这样的:

1) 当用户点击主页按钮并且应用输入 bg 时继续播放音乐(有效)

2) 如果用户退出应用程序(有时工作,其他时候抛出错误),则停止播放器 ( myMP.stop() )

我根据可能的操作使用 print 语句跟踪流程并得到:

流程 2 是我所期望的,但流程 1 会引发错误 当应用程序关闭时 - 我希望“将在此处终止”。

编辑:主要问题是当使用流程 1 退出应用程序时,永远不会调用“will terminate”——因此永远不会调用“myMP.stop()”,播放器会在应用程序退出后继续播放。

如果您在应用程序处于事件状态时单击“主页”一次(流程 1)或双击(流程 2),行为会有明显差异。

为什么我从应该相同的操作中得到两种不同的响应?

编辑: 最重要的是,如果流 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()
    }
}

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

最佳答案

“因信号 9 而终止”消息仅表示您的应用已被 SIGKILL 信号终止。每当您的应用程序非自愿终止时,操作系统都会发送该信号,无论是由于内存压力(或与此讨论无关的其他几个原因),还是用户通过双击主页按钮并将其滑开来明确终止您的应用程序。

在您的情况下,用户明确终止了您的应用程序,因此“因信号 9 而终止”消息是完全可以预期的。如果您的应用程序是当前的前台应用程序,您的 applicationWillTerminate方法将被调用,如上面的逻辑流程大纲(流程 2)所示。如果您的应用程序不是当前的前台应用程序(流程 1),则当您的应用程序处于挂起状态时,您的 applicationWillTerminate 方法将不会被调用。这是预期的行为。还要注意“背景状态”和“暂停状态”之间的区别。 They are not the same thing .

因此,如果我对您的理解是正确的,听起来问题是在您的应用程序被用户终止后音频继续播放(流程 1)。这意味着您在处理 MPMusicPlayerController 时做错了,因为它应该自动处理状态转换。

确保您设置了正确的 UIBackgroundMode为您的应用程序。设置错误的后台模式可能会导致您的应用程序行为异常,因为操作系统仅允许在后台进行某些操作,具体取决于您设置的后台模式。设置错误的模式(或尝试执行您设置的模式中明确不允许的事情)将导致您的应用暂停或终止。

确保您已正确设置 Audio Session 。

确保您正确响应 music player notifications - 特别是,确保您正确地调用了 beginGeneratingPlaybackNotificationsendGeneratingPlaybackNotifications,并且您正在正确处理这些通知。检查您的通知处理程序以确保您没有在其中做任何愚蠢的事情。在调用 endGeneratingPlaybackNotifications 之前,请确保您的 Controller 不会超出范围或以其他方式被释放。

如果您已正确完成其他所有操作,MPMusicPlayerController 几乎可以自行管理,因此当您的应用程序进入后台时,您不必做任何特殊的事情来使其正常工作(除了当然,设置正确的 UIBackgroundMode)。作为最后的手段,开始注释掉代码,直到您的应用程序只是一个准系统的“打开音频文件并播放它”应用程序,然后查看它是否正确退出。如果是这样,您可以开始一段一段地取消注释代码,直到它失败 - 然后您就会知道您的应用程序的哪个部分导致了问题,您可以从那里缩小范围。

关于ios - 退出应用程序导致错误 "Message from debugger: Terminated due to signal 9",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42331852/

有关ios - 退出应用程序导致错误 "Message from debugger: Terminated due to signal 9"的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  5. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  6. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  7. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  8. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  9. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  10. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

随机推荐