在swift中以静音模式播放闹钟

时间:2017-11-06 06:14:20

标签: ios swift alarm

如果我使用的是背景音频,那么developer.apple.com会发送给我邮件

您的应用声明在Info.plist中的UIBackgroundModes键中声明支持音频,但是当应用在后台运行时,我们无法播放任何可听内容。

后续步骤

音频键适用于在后台为用户提供可听内容的应用,例如音乐播放器或流媒体音频应用。请修改您的应用,以便在应用处于后台时向用户提供可听内容,或从UIBackgroundModes键中删除“音频”设置。

代码: -

let appDelegate = UIApplication.shared.delegate as! AppDelegate
if soundPath == "" {
   appDelegate.playSound()
}else {
   appDelegate.playSoundWithPath(notificationSoundPath: soundPath)
}

播放声音(): -

func playSound() {
  let url = Bundle.main.url(forResource: "loud_alarm", withExtension: "caf")!
  do{
      player = try AVAudioPlayer(contentsOf: url)
      guard let player = player else {return}
      player.prepareToPlay()
      player.play()
    } catch _ as NSError {

    }
}

如何解决这个问题

3 个答案:

答案 0 :(得分:1)

播放音频前添加以下代码:

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
 }
 catch {
    // catch error
 }

答案 1 :(得分:0)

我已发送邮件developer.apple.com邮件,因为背景音频失败

此: -

准则2.5.4 - 性能 - 软件要求 您的应用声明在Info.plist中的UIBackgroundModes键中声明对音频的支持,但不包括需要持久音频的功能。

后续步骤 音频密钥旨在供在后台为用户提供可听内容的应用程序使用, 例如音乐播放器或流媒体音频应用。 请修改您的应用,以便在应用在后台时向用户提供可听内容,或者删除音频"从UIBackgroundModes键设置。

如何解决此问题

我的代码是

 public Map<String,String> loadObject(ArrayList<Integer> tradigAccountList){

        com.datastax.driver.core.Session session;
        Map<String,String> orderListMap = new HashMap<>();
        List<ResultSetFuture> futures = new ArrayList<>();
        List<ListenableFuture<ResultSet>> Future;

        try {
            session =jdbcUtils.getCassandraSession();
            PreparedStatement statement = session.prepare(
                    "SELECT * FROM omsks_v1.ordersStringV1 WHERE tradacntid = ?");

            for (Integer tradingAccount:tradigAccountList){
                futures.add(session.executeAsync(statement.bind(tradingAccount).setFetchSize(50000)));
            }
            Future = Futures.inCompletionOrder(futures);
            for (ListenableFuture<ResultSet> future : Future){
                for ( Row row : future.get()){
                    orderListMap.put(row.getString("cliordid"),row.getString("ordermsg"));
                }
            }

        }catch (Exception e){
        }finally {
        }
        return orderListMap;
    }

和playsound()函数代码是

 trunOffAlarm.isHidden = false
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        if soundPath == "" {

            appDelegate.playSound()
        }else {
            appDelegate.playSoundWithPath(notificationSoundPath: soundPath)
        }

答案 2 :(得分:-1)

/**
 setup and play the sound of the local mp3 file
 */
@objc func setupAudioPlayer() {
    // TODO: load the audio file asynchronously and observe player status
    if (userSelectedRingTone.isEmpty) {
       ringToneName = "Default_Siren"
    } else {
        guard (isFromHamburgerMenuDefaultTone) else {
            ringToneName = self.userSelectedRingTone
            isFromHamburgerMenuDefaultTone = false
            self.playSound(soundName: ringToneName)
            return
        }
        ringToneName = "Default_Siren"
    }
       self.playSound(soundName: ringToneName)

}

func playSound( soundName : String) {
    guard let url = Bundle.main.url(forResource: soundName, withExtension: "mp3") else { return }

    do {
        try 
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)
        /* The following line is required for the player to work on iOS 11. Change the file type accordingly*/
        audioPlayer = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)
        audioPlayer?.delegate = self


        guard let player = audioPlayer else { return }
//            player.play()
    } catch let error {
        print(error.localizedDescription)
    }
}


// if you continue to play the audio then add this delegate method
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    if (playAudioRepeatedly) {
        audioPlayer?.play()
    }
}