当手机处于静音模式时,在我的后台应用程序上播放声音 [英] Play a sound on my backgrounded app when the phone is in silent mode

查看:339
本文介绍了当手机处于静音模式时,在我的后台应用程序上播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个计时器应用程序,但我遇到的一个问题是它在后台运行时,如果用户音量降低,则无法响铃.关闭音量或声音也会使通知静音,这是我在后台将计时器响铃的方法.

I've built a timer app and one problem I have is when it's backgrounded, I'm unable to ring the timer if the user has volume off. Turning the volume or sound off also mutes notifications, which is the method I was using for ringing the timer in the background.

我刚买了一块瓷砖,发现了它

I just bought a tile and discovered it can ring your phone even on silent. I have tested this and it works in iOS 9, but I'm not sure how to duplicate this behavior.

如何在静音模式下振铃iPhone?后台刷新?动作和活动?还有吗?

How is ringing the iPhone in silent mode accomplished? Background refresh? Motion and activity? Something else?

现有技术:

  • 此答案以静默模式播放,但在应用程序关闭时无法解决.相同的此处
  • this answer plays in silent mode, but does not address when the app is closed. same here

推荐答案

首先,为了在后台播放声音,您的应用程序必须能够在后台启动响应回调事件的相关代码.如此处.例如,"Tile"应用程序可以在后台播放声音,以响应与BLE相关的回调事件,该事件在应用程序处于后台时触发(例如BLE外围设备连接和断开连接).相反,当应用程序进入后台时,一个简单的计时器将被挂起,因此不会触发计时器到期回调,并且在这种情况下无法启动任何代码来播放声音. (您可以在此处阅读,以了解一些详细信息和可能的操作iOS中构建闹钟的方法)

First of all, in order to play a sound in background your application must be able to launch the related code responding to a callback event when in the background. Only specific app types are allowed to respond to callbacks events in the background as indicated here. For example, the "Tile" application can play sound in background responding to BLE-related callbacks events which are fired when the application is in background (e.g., BLE peripheral connections and disconnections) . Conversely, a simple timer is suspended when application goes in background thus the timer-expiration callback won't be triggered and there is no way to launch any code to play sound in this case. (You can read here for some details and a possibile approach to build alarm clock in iOS)

如果您的应用程序类型在特殊的后台模式应用程序集中,则即使电话处于静音模式,也可以使用AVAudioPlayer播放声音以响应后台事件.在您的视图控制器中,导入AVFoundation框架:

If your application type is within the set of special background-mode applications you can play a sound responding to events in background even when the phone is in silent mode by using AVAudioPlayer. In your view controller import the AVFoundation framework:

import AVFoundation

比声明AVAudioPlayer变量

than declare an AVAudioPlayer variable

var player: AVAudioPlayer?;

为了播放放置在应用程序捆绑包中的mp3文件:

in order to play an mp3 file placed in the app bundle:

    func playSound() {

        guard let url = Bundle.main.url(forResource: "MY_MP3_FILE", withExtension: "mp3") else {
            print("error");
            return;
        }
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.mixWithOthers);
            player = try AVAudioPlayer(contentsOf: url);
            guard let player = player else {
                print("error");
                return;
            }
            player.play();
        } catch let error {
            print(error.localizedDescription);
        }
    }

重要:在Xcode的"功能"标签中,您还必须启用"音频,AirPlay和画中画"功能在"背景模式"部分中.

Important: in Xcode in the "Capabilities" tab you also have to enable the "Audio, AirPlay and Picture in Picture" feature in the "Background Modes" section.

(在iOS 11和12中使用不同的iPhone型号测试过的Swift 4解决方案).

(Swift 4 solution tested in iOS 11 and 12 with different iPhone models).

警告:按照这种方法,声音将以手机中设置的当前音量播放,因此,如果音量等于零,则不会播放声音.您可以通过编程方式通过这种方式将音量强制为给定值:

Warning: Following this approach the sound will play at the current volume set in the phone, thus if the sound volume is equal to zero the sound will not be played. You can force the volume sound to a given value programmatically in this way:

    let volumeView = MPVolumeView();
    if let view = volumeView.subviews.first as? UISlider{

        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.01) {
            view.value = 0.5; //Set the volume level between 0 and 1
        }
    }

但是,请考虑到这种方法会显示系统音量条,并且可能导致不良的用户体验.

however, take into account that this approach shows the system sound volume bar and it could lead to a bad user experience.

这篇关于当手机处于静音模式时,在我的后台应用程序上播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆