<commit_before>//
//  AudioPlayer.swift
//  AudioPlayer
//
//  Created by <NAME> on 26/04/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit
import MediaPlayer
import AVFoundation

private class ClosureContainer: NSObject {
    let closure: (sender: AnyObject) -> ()

    init(closure: (sender: AnyObject) -> ()) {
        self.closure = closure
    }

    @objc func callSelectorOnTarget(sender: AnyObject) {
        closure(sender: sender)
    }
}

// MARK: - AudioPlayerState

/**
`AudioPlayerState` defines 4 state an `AudioPlayer` instance can be in.

- `Buffering`:            Represents that the player is buffering data before playing them.
- `Playing`:              Represents that the player is playing.
- `Paused`:               Represents that the player is paused.
- `Stopped`:              Represents that the player is stopped.
- `WaitingForConnection`: Represents the state where the player is waiting for internet connection.
*/
public enum AudioPlayerState {
    case Buffering
    case Playing
    case Paused
    case Stopped
    case WaitingForConnection
    case Failed(NSError?)
}

extension AudioPlayerState: Equatable { }

public func ==(lhs: AudioPlayerState, rhs: AudioPlayerState) -> Bool {
    switch (lhs, rhs) {
    case (.Buffering, .Buffering):
        return true
    case (.Playing, .Playing):
        return true
    case (.Paused, .Paused):
        return true
    case (.Stopped, .Stopped):
        return true
    case (.WaitingForConnection, .WaitingForConnection):
        return true
    case (.Failed(let e1), .Failed(let e2)):
        return e1 == e2
    default:
        return false
    }
}


// MARK: - AudioPlayerMode

public struct AudioPlayerModeMask: OptionSetType {
    public let rawValue: UInt

    public init(rawValue: UInt) {
        self.rawValue = rawValue
    }

    public static var Shuffle: AudioPlayerModeMask {
        return self.init(rawValue: 0b001)
    }

    public static var Repeat: AudioPlayerModeMask {
        return self.init(rawValue: 0b010)
    }

    public static var RepeatAll: AudioPlayerModeMask {
        return self.init(rawValue: 0b100)
    }
}


// MARK: - AVPlayer+KVO

private extension AVPlayer {
    static var ap_KVOItems: [String] {
        return [
            "currentItem.playbackBufferEmpty",
            "currentItem.playbackLikelyToKeepUp",
            "currentItem.duration",
            "currentItem.status",
            "currentItem.loadedTimeRanges"
        ]
    }
}


// MARK: - NSObject+Observation

private extension NSObject {
    func observe(name: String, selector: Selector, object: AnyObject? = nil) {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: selector, name: name, object: object)
    }

    func unobserve(name: String, object: AnyObject? = nil) {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: name, object: object)
    }
}


// MARK: - Array+Shuffe

private extension Array {
    func shuffled() -> [Element] {
        return sort { e1, e2 in
            random() % 2 == 0
        }
    }
}


// MARK: - NSURL+iPodLibrary

private extension NSURL {
    var isOfflineURL: Bool {
        return fileURL || scheme == "ipod-library"
    }
}


// MARK: - AudioPlayerDelegate

public protocol AudioPlayerDelegate: NSObjectProtocol {
    func audioPlayer(audioPlayer: AudioPlayer, didChangeStateFrom from: AudioPlayerState, toState to: AudioPlayerState)
    func audioPlayer(audioPlayer: AudioPlayer, willStartPlayingItem item: AudioItem)
    func audioPlayer(audioPlayer: AudioPlayer, didUpdateProgressionToTime time: NSTimeInterval, percentageRead: Float)
    func audioPlayer(audioPlayer: AudioPlayer, didFindDuration duration: NSTimeInterval, forItem item: AudioItem)
    func audioPlayer(audioPlayer: AudioPlayer, didLoadRange: AudioPlayer.TimeRange, forItem item: AudioItem)
}


// MARK: - AudioPlayer

/**
An `AudioPlayer` instance is used to play `AudioPlayerItem`. It's an easy to use
AVPlayer with simple methods to handle the whole playing audio process.

You can get events (such as state change or time observation) by registering a delegate.
*/
public class AudioPlayer: NSObject {
    // MARK: Initialization

    public override init() {
        super.init()
<commit_msg>Renamed loadRange delegate method<commit_after>//
//  AudioPlayer.swift
//  AudioPlayer
//
//  Created by <NAME> on 26/04/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit
import MediaPlayer
import AVFoundation

private class ClosureContainer: NSObject {
    let closure: (sender: AnyObject) -> ()

    init(closure: (sender: AnyObject) -> ()) {
        self.closure = closure
    }

    @objc func callSelectorOnTarget(sender: AnyObject) {
        closure(sender: sender)
    }
}

// MARK: - AudioPlayerState

/**
`AudioPlayerState` defines 4 state an `AudioPlayer` instance can be in.

- `Buffering`:            Represents that the player is buffering data before playing them.
- `Playing`:              Represents that the player is playing.
- `Paused`:               Represents that the player is paused.
- `Stopped`:              Represents that the player is stopped.
- `WaitingForConnection`: Represents the state where the player is waiting for internet connection.
*/
public enum AudioPlayerState {
    case Buffering
    case Playing
    case Paused
    case Stopped
    case WaitingForConnection
    case Failed(NSError?)
}

extension AudioPlayerState: Equatable { }

public func ==(lhs: AudioPlayerState, rhs: AudioPlayerState) -> Bool {
    switch (lhs, rhs) {
    case (.Buffering, .Buffering):
        return true
    case (.Playing, .Playing):
        return true
    case (.Paused, .Paused):
        return true
    case (.Stopped, .Stopped):
        return true
    case (.WaitingForConnection, .WaitingForConnection):
        return true
    case (.Failed(let e1), .Failed(let e2)):
        return e1 == e2
    default:
        return false
    }
}


// MARK: - AudioPlayerMode

public struct AudioPlayerModeMask: OptionSetType {
    public let rawValue: UInt

    public init(rawValue: UInt) {
        self.rawValue = rawValue
    }

    public static var Shuffle: AudioPlayerModeMask {
        return self.init(rawValue: 0b001)
    }

    public static var Repeat: AudioPlayerModeMask {
        return self.init(rawValue: 0b010)
    }

    public static var RepeatAll: AudioPlayerModeMask {
        return self.init(rawValue: 0b100)
    }
}


// MARK: - AVPlayer+KVO

private extension AVPlayer {
    static var ap_KVOItems: [String] {
        return [
            "currentItem.playbackBufferEmpty",
            "currentItem.playbackLikelyToKeepUp",
            "currentItem.duration",
            "currentItem.status",
            "currentItem.loadedTimeRanges"
        ]
    }
}


// MARK: - NSObject+Observation

private extension NSObject {
    func observe(name: String, selector: Selector, object: AnyObject? = nil) {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: selector, name: name, object: object)
    }

    func unobserve(name: String, object: AnyObject? = nil) {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: name, object: object)
    }
}


// MARK: - Array+Shuffe

private extension Array {
    func shuffled() -> [Element] {
        return sort { e1, e2 in
            random() % 2 == 0
        }
    }
}


// MARK: - NSURL+iPodLibrary

private extension NSURL {
    var isOfflineURL: Bool {
        return fileURL || scheme == "ipod-library"
    }
}


// MARK: - AudioPlayerDelegate

public protocol AudioPlayerDelegate: NSObjectProtocol {
    func audioPlayer(audioPlayer: AudioPlayer, didChangeStateFrom from: AudioPlayerState, toState to: AudioPlayerState)
    func audioPlayer(audioPlayer: AudioPlayer, willStartPlayingItem item: AudioItem)
    func audioPlayer(audioPlayer: AudioPlayer, didUpdateProgressionToTime time: NSTimeInterval, percentageRead: Float)
    func audioPlayer(audioPlayer: AudioPlayer, didFindDuration duration: NSTimeInterval, forItem item: AudioItem)
    func audioPlayer(audioPlayer: AudioPlayer, didLoadRange range: AudioPlayer.TimeRange, forItem item: AudioItem)
}


// MARK: - AudioPlayer

/**
An `AudioPlayer` instance is used to play `AudioPlayerItem`. It's an easy to use
AVPlayer with simple methods to handle the whole playing audio process.

You can get events (such as state change or time observation) by registering a delegate.
*/
public class AudioPlayer: NSObject {
    // MARK: Initialization

    public override init() {
        super.init()

{'fraction_non_alphanumeric': 0.09283881502216002, 'fraction_numerical': 0.007231163984138092, 'mean_word_length': 2.960739030023095, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '919877', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2392, 'n_words': 744}