<commit_before>//
//  RadioPlayer.swift
//  WXYC
//
//  Created by <NAME> on 12/1/17.
//  Copyright © 2017 wxyc.org. All rights reserved.
//

import Foundation
import AVFoundation

public final class RadioPlayer {
    private let player = AVPlayer(url: URL.WXYCStream)
    
    public init() {
        // Notification for AVAudioSession Interruption (e.g. Phone call)
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(RadioPlayer.applicationEnteredBackground),
            name: Notification.Name.UIApplicationDidEnterBackground,
            object: nil
        )
        
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(RadioPlayer.sessionInterrupted),
            name: Notification.Name.AVAudioSessionInterruption,
            object: AVAudioSession.sharedInstance()
        )
    }
    
    deinit {
        // Be a good citizen
        NotificationCenter.default.removeObserver(
            self,
            name: Notification.Name.AVAudioSessionInterruption,
            object: AVAudioSession.sharedInstance()
        )
    }
    
    public var isPlaying: Bool {
        return player.isPlaying
    }
    
    public func play() {
        try! AVAudioSession.sharedInstance().setActive(true)
        
        player.play()
        
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(playerItemFailedToPlayToEndTime(_:)),
            name: NSNotification.Name.AVPlayerItemPlaybackStalled,
            object: nil
        )
    }
    
    public func pause() {
        player.pause()
        self.resetStream()
        
        NotificationCenter.default.removeObserver(
            self,
            name: NSNotification.Name.AVPlayerItemPlaybackStalled,
            object: nil
        )
    }
    
    @objc private func playerItemFailedToPlayToEndTime(_ aNotification: Notification) {
        resetStream()
    }
    
    func resetStream() {
        let asset = AVAsset(url: URL.WXYCStream)
        let playerItem = AVPlayerItem(asset: asset)
        player.replaceCurrentItem(with: playerItem)
    }
    
    // MARK: AVAudioSesssion interruptions
    
    @objc func sessionInterrupted(notification: NSNotification) {
        if let typeValue = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? NSNumber{
            if let type = AVAudioSessionInterruptionType(rawValue: typeValue.uintValue) {
                if type == .began {
                    print("interruption: began")
                } else{
                    print("interruption: ended")
                    play()
                }
            }
        }
    }
    
    // MARK: Application lifecycle
    
    @objc func applicationEnteredBackground(notification: NSNotification) {
        if !self.player.isPlaying {
            try! AVAudioSession.sharedInstance().setActive(false)
        }
    }
}


private extension AVPlayer {
    var isPlaying: Bool {
        return rate > 0.0
    }
}

<commit_msg>For the love of whitespace<commit_after>//
//  RadioPlayer.swift
//  WXYC
//
//  Created by <NAME> on 12/1/17.
//  Copyright © 2017 wxyc.org. All rights reserved.
//

import Foundation
import AVFoundation

public final class RadioPlayer {
    private let player = AVPlayer(url: URL.WXYCStream)
    
    public init() {
        // Notification for AVAudioSession Interruption (e.g. Phone call)
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(RadioPlayer.applicationEnteredBackground),
            name: Notification.Name.UIApplicationDidEnterBackground,
            object: nil
        )
        
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(RadioPlayer.sessionInterrupted),
            name: Notification.Name.AVAudioSessionInterruption,
            object: AVAudioSession.sharedInstance()
        )
    }
    
    deinit {
        // Be a good citizen
        NotificationCenter.default.removeObserver(
            self,
            name: Notification.Name.AVAudioSessionInterruption,
            object: AVAudioSession.sharedInstance()
        )
    }
    
    public var isPlaying: Bool {
        return player.isPlaying
    }
    
    public func play() {
        try! AVAudioSession.sharedInstance().setActive(true)
        
        player.play()
        
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(playerItemFailedToPlayToEndTime(_:)),
            name: NSNotification.Name.AVPlayerItemPlaybackStalled,
            object: nil
        )
    }
    
    public func pause() {
        player.pause()
        self.resetStream()
        
        NotificationCenter.default.removeObserver(
            self,
            name: NSNotification.Name.AVPlayerItemPlaybackStalled,
            object: nil
        )
    }
    
    @objc private func playerItemFailedToPlayToEndTime(_ aNotification: Notification) {
        resetStream()
    }
    
    func resetStream() {
        let asset = AVAsset(url: URL.WXYCStream)
        let playerItem = AVPlayerItem(asset: asset)
        player.replaceCurrentItem(with: playerItem)
    }
    
    // MARK: AVAudioSesssion interruptions
    
    @objc func sessionInterrupted(notification: NSNotification) {
        if let typeValue = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? NSNumber{
            if let type = AVAudioSessionInterruptionType(rawValue: typeValue.uintValue) {
                if type == .began {
                    print("interruption: began")
                } else{
                    print("interruption: ended")
                    play()
                }
            }
        }
    }
    
    // MARK: Application lifecycle
    
    @objc func applicationEnteredBackground(notification: NSNotification) {
        if !self.player.isPlaying {
            try! AVAudioSession.sharedInstance().setActive(false)
        }
    }
}

private extension AVPlayer {
    var isPlaying: Bool {
        return rate > 0.0
    }
}

{'fraction_non_alphanumeric': 0.08132480734546647, 'fraction_numerical': 0.003607148712903755, 'mean_word_length': 1.9089175011921793, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6174431', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1572, 'n_words': 326}