<commit_before>//
//  AudioSession.swift
//  AudioPlayer
//
//  Created by <NAME> on 9/23/17.
//  Copyright © 2017 ShortSands. All rights reserved.
//
// Documentation: https://developer.apple.com/documentation/avfoundation/avaudiosession
// https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Introduction/Introduction.html
// http://www.densci.com/files/files/Denville/ProductDocs/AudioSessionProgrammingGuide.pdf
// http://devstreaming.apple.com/videos/wwdc/2016/507n0zrhzxdzmg20zcl/507/507_delivering_an_exceptional_audio_experience.pdf
//
// Category AVAudioSessionCategoryPlayback means that audio will play when the lock screen is on.
// to have it continue in background when the screen locks, one must also set
// UIBackgroundModes -> audio
//

import AVFoundation

class AudioSession: NSObject {
    
    let session: AVAudioSession = AVAudioSession.sharedInstance()
    let audioBibleView: AudioBibleView
    
    init(audioBibleView: AudioBibleView) {
        self.audioBibleView = audioBibleView
        super.init()
        do {
            try self.session.setCategory(AVAudioSessionCategoryPlayback,
                                         mode: AVAudioSessionModeSpokenAudio,
                                         options: [])
            try self.session.setActive(true)
        } catch let err {
            print("Could not initialize AVAudioSession \(err)")
        }
        let center = NotificationCenter.default
        center.addObserver(self,
                           selector: #selector(audioSessionInterruption(note:)),
                           name: .AVAudioSessionInterruption,
                           object: self.session)
        center.addObserver(self,
                           selector: #selector(audioSessionRouteChange(note:)),
                           name: .AVAudioSessionRouteChange,
                           object: self.session)
        center.addObserver(self,
                           selector: #selector(audioSessionSilenceSecondaryAudioHint(note:)),
                           name: .AVAudioSessionSilenceSecondaryAudioHint,
                           object: self.session)
        center.addObserver(self,
                           selector: #selector(audioSessionMediaServicesWereReset(note:)),
                           name: .AVAudioSessionMediaServicesWereReset,
                           object: self.session)
    }
    
    deinit {
        print("***** Deinit AudioSessionDelegate *****")
        // session does not need to be deactivated
    }
    
    @objc func audioSessionInterruption(note: NSNotification) {
        if let value = note.userInfo?[AVAudioSessionInterruptionTypeKey] as? UInt {
            if let interruptionType =  AVAudioSessionInterruptionType(rawValue: value) {
                if interruptionType == .began {
                    print("\n****** Interruption Began, Pause in UI")
                    self.audioBibleView.pause()
                } else if interruptionType == .ended {
                    print("\n****** Interruption Ended, Play in UI, try to resume")<commit_msg>Fix audio bug that prevented play pause from sometime being in correct state.<commit_after>//
//  AudioSession.swift
//  AudioPlayer
//
//  Created by <NAME> on 9/23/17.
//  Copyright © 2017 ShortSands. All rights reserved.
//
// Documentation: https://developer.apple.com/documentation/avfoundation/avaudiosession
// https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Introduction/Introduction.html
// http://www.densci.com/files/files/Denville/ProductDocs/AudioSessionProgrammingGuide.pdf
// http://devstreaming.apple.com/videos/wwdc/2016/507n0zrhzxdzmg20zcl/507/507_delivering_an_exceptional_audio_experience.pdf
//
// Category AVAudioSessionCategoryPlayback means that audio will play when the lock screen is on.
// to have it continue in background when the screen locks, one must also set
// UIBackgroundModes -> audio
//

import AVFoundation

class AudioSession: NSObject {
    
    let session: AVAudioSession = AVAudioSession.sharedInstance()
    let audioBibleView: AudioBibleView
    
    init(audioBibleView: AudioBibleView) {
        self.audioBibleView = audioBibleView
        super.init()
        do {
            try self.session.setCategory(AVAudioSessionCategoryPlayback,
                                         mode: AVAudioSessionModeSpokenAudio,
                                         options: [])
            try self.session.setActive(true)
        } catch let err {
            print("Could not initialize AVAudioSession \(err)")
        }
        let center = NotificationCenter.default
        center.addObserver(self,
                           selector: #selector(audioSessionInterruption(note:)),
                           name: .AVAudioSessionInterruption,
                           object: self.session)
        center.addObserver(self,
                           selector: #selector(audioSessionRouteChange(note:)),
                           name: .AVAudioSessionRouteChange,
                           object: self.session)
        center.addObserver(self,
                           selector: #selector(audioSessionSilenceSecondaryAudioHint(note:)),
                           name: .AVAudioSessionSilenceSecondaryAudioHint,
                           object: self.session)
        center.addObserver(self,
                           selector: #selector(audioSessionMediaServicesWereReset(note:)),
                           name: .AVAudioSessionMediaServicesWereReset,
                           object: self.session)
    }
    
    deinit {
        print("***** Deinit AudioSessionDelegate *****")
        // session does not need to be deactivated
    }
    
    @objc func audioSessionInterruption(note: NSNotification) {
        if let value = note.userInfo?[AVAudioSessionInterruptionTypeKey] as? UInt {
            if let interruptionType =  AVAudioSessionInterruptionType(rawValue: value) {
                if interruptionType == .began {
                    print("\n****** Interruption Began")//, Pause in UI")
                    //self.audioBibleView.pause()
                } else if interruptionType == .ended {
                    print("\n****** Interruption Ended, Play in UI, try to resume")
{'fraction_non_alphanumeric': 0.08948863636363637, 'fraction_numerical': 0.007891414141414142, 'mean_word_length': 2.0851996105160664, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2770547', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1545, 'n_words': 335}