Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift命令中心播放控制引发异常_Ios_Swift - Fatal编程技术网

Ios Swift命令中心播放控制引发异常

Ios Swift命令中心播放控制引发异常,ios,swift,Ios,Swift,我正试图从指挥中心控制音频播放。音频是从一个视图中播放的,该视图在选择播放文件时,每次从文件列表触发segue时都会创建一个新实例 配置命令中心控件的代码出现在播放器视图的viewDidLoad功能中: let audioSession = AVAudioSession.sharedInstance(); do { try audioSession.setCategory(AVAudioSessionCategoryPlayback);

我正试图从指挥中心控制音频播放。音频是从一个视图中播放的,该视图在选择播放文件时,每次从文件列表触发segue时都会创建一个新实例

配置命令中心控件的代码出现在播放器视图的
viewDidLoad
功能中:

        let audioSession = AVAudioSession.sharedInstance();
        do {
            try audioSession.setCategory(AVAudioSessionCategoryPlayback);
            try AVAudioSession.sharedInstance().setActive(true);
        } catch {}

        UIApplication.shared.beginReceivingRemoteControlEvents();

        TableViewController.commandCenter = MPRemoteCommandCenter.shared();

        TableViewController.commandCenter!.togglePlayPauseCommand.isEnabled = true;
        TableViewController.commandCenter!.togglePlayPauseCommand.addTarget(self, action: #selector(TableViewController.playbacker!.TogglePlayPause))
我第一次进入播放器视图时,控制中心的命令工作得非常好。当我选择另一个文件进行播放并重新创建视图时,会出现问题。然后,我看到以下例外情况:

由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:“-[\u MPWEAGENTEXCEPTION” TogglePlayPause]:发送到实例0x170032340'的无法识别的选择器

我已经尝试将
MPRemoteCommandCenter
变量设置为静态,将播放器视图引用设置为静态,但结果是相同的


有谁能告诉我为什么这不起作用吗?

我也有同样的问题。您解决了这个问题吗?我从一月份以来就没有接触过该代码,但是如果我正确阅读了提交,我会将特定于命令中心的定义移动到
AppDelegate.swift的
应用程序
函数中。我将函数的第一行设置为
UIApplication.shared.beginReceivingRemoteControlEvents()
commandCenter=MPRemoteCommandCenter.shared()
然后继续启用
commandCenter
变量的按钮。让我知道它是否适合您。我添加了第一行UIApplication.shared.beginReceivingRemoteControlEvents();在AppDelegate.swift中,仍然给出相同的错误。请检查这个“我也有同样的问题。您解决了这个问题吗?我从一月份以来就没有接触过该代码,但是如果我正确阅读了提交,我会将特定于命令中心的定义移动到
AppDelegate.swift的
应用程序
函数中。我将函数的第一行设置为
UIApplication.shared.beginReceivingRemoteControlEvents()
commandCenter=MPRemoteCommandCenter.shared()
然后继续启用
commandCenter
变量的按钮。让我知道它是否适合您。我添加了第一行UIApplication.shared.beginReceivingRemoteControlEvents();在AppDelegate.swift中,仍然给出相同的错误。请检查这个“