AudioPlayer - Kelmatou/iOSTools GitHub Wiki
AudioPlayer
Delegates
protocol AudioPlayerDelegate: class {
@objc optional func queueUpdated(_ player: AudioPlayer, queue: [String])
@objc optional func songLoaded(_ player: AudioPlayer, song: String)
@objc optional func willStartPlaying(_ player: AudioPlayer, song: String)
@objc optional func didFinishPlaying(_ player: AudioPlayer, song: String)
@objc optional func didReachEndOfQueue(_ player: AudioPlayer)
@objc optional func didChangeVolume(_ player: AudioPlayer, newVolume: Float)
@objc optional func didPause(_ player: AudioPlayer, song: String)
@objc optional func didStop(_ player: AudioPlayer)
@objc optional func didReplay(_ player: AudioPlayer, song: String)
@objc optional func didMoveForward(_ player: AudioPlayer, song: String?)
@objc optional func didMoveRewind(_ player: AudioPlayer, song: String)
}
Nested Types
enum PlayerState {
case Playing // Audio is being played
case Paused // Audio is stoped but player is ready to play()
case Stoped // Audio is stoped, player may be ready or not. Must call prepareCurrentSong()
}
enum AudioUsage {
case Foreground // Audio will be heard even in silent mode
case Background // Audio will be heard only if silent mode is disabled. When possible, use it!
}
Properties
weak var delegate: AudioPlayerDelegate?
var songQueue: AudioQueue
var autoPlay: Bool
var audioUsage: AudioUsage
private(set) var status: PlayerState
Inits
init(withSongs: [String] = [], autoPlay: Bool = true, audioUsage: AudioUsage = .Foreground)
Methods
func play()
func replay()
func stop()
func pause()
func fastRewind()
func fastFoward()
func setVolume(_ volume: Float)
func setCurrentTime(_ time: TimeInterval)
Definitions
-
weak var delegate: AudioPlayerDelegate?
: AudioPlayer delegate, assign it to your class to receive notifications. -
var songQueue: AudioQueue
: a queue that manages songs operations (add, insert, remove...). -
var autoPlay: Bool
: Indicates whether AudioPlayer should play next song when current one is finished. Default value is true. -
var audioUsage: AudioUsage
: Indicates how song should be played. If set to AudioUsage.Foreground, songs will be heard even in silent mode. If set to AudioUsage.Background, songs will be heard only if silent mode is disabled. When possible, use it. -
private(set) var status: PlayerState
: The current status of player. Can indicates if player is playing songs or not. -
init(withSongs: [String] = [], autoPlay: Bool = true, audioUsage: AudioUsage = .Foreground)
: Create a AudioPlayer object with a list of songs and options such as autoPlay or audioUsage previously described. -
func play()
: Play currently selected track. If no songs are available in queue, does nothing. If player cannot be created, didStop() will be called. Will set current status to Playing if successful or Stop if something failed. -
func replay()
: Replay currently selected track. If currently played song was removed, does nothing. If successful, will set status to Playing or Stop if something failed. -
func stop()
: Will stop audio and set current track to 0. Will also set status to Stop. -
func pause()
: Will pause audio and set status to Paused. -
func fastRewind()
: Will set previous track as currently selected. If first song and songQueue.canLoop is false does nothing. Note that it does not change status so if song was being played, newly selected song will be automatically played. -
func fastFoward()
: Will set next track as currently selected. If last song and songQueue.canLoop is false does nothing. Note that it does not change status so if song was being played, newly selected song will be automatically played. -
func setVolume(_ volume: Float)
: Set player's volume. volume can go from 0 to 1 (if argument is greater than 1, will be treated as 1). It represents the coefficient of user's device volume. -
func setCurrentTime(_ time: TimeInterval)
: set current time in song.
AudioQueue
Delegates
protocol AudioPlayerQueueDelegate: class {
func queueUpdate(_ audioQueue: AudioQueue, queue: [String])
func currentSongRemoved(_ audioQueue: AudioQueue, song: String)
}
Nested Types
enum IndexMove {
case Prev
case Next
}
Properties
private(set) var songs: [AudioSong]
private(set) var currentSong: Int
var canLoop: Bool
Inits
init(songs: [String] = [])
init(audioSongs: [AudioSong])
Methods
func getCurrentSongName() -> String?
func getCurrentSongFile() -> String?
func getCurrentSongURL() -> URL?
@discardableResult func setCurrentSong(_ move: IndexMove, allowLoop: Bool? = nil) -> Bool
func setCurrentSong(at index: Int)
func append(_ song: String)
func append(_ song: AudioSong)
func append(_ songs: [String])
func append(_ songs: [AudioSong])
func insert(_ song: String, at index: Int)
func insert(_ song: AudioSong, at index: Int)
func removeAll()
func remove(at index: Int)
func remove(named: String, firstOnly: Bool = false)
func moveSong(at src: Int, to dst: Int)
func shuffle()
func currentIsFirstSong() -> Bool
func currentIsLastSong() -> Bool
func isEmpty() -> Bool
Definitions
-
private(set) var songs: [AudioSong]
: the list of songs currently present in queue. -
private(set) var currentSong: Int
: the index of current song in queue. -
var canLoop: Bool
: a value indicating if queue should be treated as circular. Default value is false. -
init(songs: [String] = [])
: Initialize queue with songs file's name. -
`init(audioSongs: [AudioSong]): Initialize queue with songs.
-
func getCurrentSongName() -> String?
: get the name of currently selected song in queue. -
func getCurrentSongFile() -> String?
: get file's name of currently selected song in queue. -
func getCurrentSongURL() -> URL?
: get the path of currently selected song in queue. If current song is removed or file cannot be found jump to next song (Updating currentSong). -
@discardableResult func setCurrentSong(_ move: IndexMove, allowLoop: Bool? = nil) -> Bool
: attempts to move currentSong. If move is impossible, does nothing. return true if currentSong's value changed. -
func setCurrentSong(at index: Int)
: set song at index as the currentSong. If index is out of range does nothing. -
func append(_ song: String)
: add a song at the end of the queue. song represents the file name. -
func append(_ song: AudioSong)
: add a song at the end of the queue. -
func append(_ songs: [String])
: add songs at the end of the queue. songs represents a list of files. -
func append(_ songs: [AudioSong])
: add songs at the end of the queue. -
func insert(_ song: String, at index: Int)
: insert a song at index. song represents a file name. If index is lower than 0, insert song at the first place. if index is greater than the last position, has the same behavior as append(song:). -
func insert(_ song: AudioSong, at index: Int)
: insert a song at index. If index is lower than 0, insert song at the first place. if index is greater than the last position, has the same behavior as append(song:). -
func removeAll()
: remove all songs in queue. -
func remove(at index: Int)
: remove song at a specified index. If index is out of range, does nothing. -
func remove(named: String, firstOnly: Bool = false)
: remove songs that have the same name as named. Parameter firstOnly specify if function should only remove the first match or continue search. Its default value is false. -
func moveSong(at src: Int, to dst: Int)
: move song at src index to dst index. Also update currentSong's value if song at src index is currently selected so that it remains selected. -
func shuffle()
: randomly re-order songs in queue. Update currentSong's value so that currently selected song remains selected. -
func currentIsFirstSong() -> Bool
: Determines if currently selected song is first in songQueue. -
func currentIsLastSong() -> Bool
: Determines if currently selected song is last in songQueue. -
func isEmpty() -> Bool
: Determines if queue is empty or not.
AudioSong
Properties
var name: String
var filename: String
Inits
init(_ filename: String, named: String? = nil)
Definitions
-
var name: String
: The name of the song. -
var filename: String
: The file containing audio song. -
init(_ filename: String, named: String? = nil)
: Create a new AudioSong object from a filename. Parameter named will specified song's name. If not specified, iOSTools will try to get last component from file's path. If it failed, song's name will be the same as filename.
AudioTrack
Properties
private(set) var name: String
private(set) var filename: String
Definitions
-
private(set) var name: String
: The name of the track. -
private(set) var filename: String
: The file containing audio track.
Note
AudioTrack objects internally contain a 'removed' field. If this boolean is set to 'true', it'll never be played by an AudioPlayer.