Я думаю, это то, что вы ищете
struct Manager
{
//Required Objects - AVFoundation
///AVAudio Session
static var recordingSession: AVAudioSession!
///AVAudio Recorder
static var recorder: AVAudioRecorder?
///AVAudio Player
static var player: AVAudioPlayer?
}
Воспроизведение музыки
//Set player with audio File
do
{
try Manager.player = AVAudioPlayer.init(contentsOf: returnPathAtSelectedIndex(fileName: fileName))
//Set required delegates and Values
Manager.player?.delegate = self
Manager.player?.volume = 1.0
Manager.player?.prepareToPlay()
Manager.player?.play()
}
catch
{
print("Error while playing music: \(error.localizedDescription)")
}
Делегаты аудиоплеера
//MARK:- Audio Player Delegates
extension RecordingManager: AVAudioPlayerDelegate
{
//MARK: Audio Player Finishes Playing audio
/**
Called when a sound has finished playing.
- parameter player: player instance
- parameter flag: Bool player is running or not successfully
*/
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool)
{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "StoppedPlaying"), object: nil)
player.stop()
Manager.player?.stop()
Manager.recordingalreadyPlayedStatus = false
print("Finish Playing")
}
//MARK: Audio Player error occur while Playing
/**
Called when an audio player encounters a decoding error during playback.
- parameter player: player instance
- parameter error: Error if occurs
*/
func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer,error: Error?)
{
print("Encoding Error: \(String(describing: error?.localizedDescription))")
}
}