-- Function to play an audio asset

AudioPlayer.playAudio = function(asset)
	if not asset then
		warn("Could not find audio asset: " .. asset)
		return
	end
	if not asset.IsLoaded then
		asset.Loaded:wait()
	end
	asset:Play()
end

AudioPlayer.pauseAudio = function(asset) 
	if asset.IsPlaying then
		AudioPlayer.reduceVolume(asset, 0)
		asset:Pause()
	end
end

AudioPlayer.resumeAudio = function(asset) 
	if asset.IsPaused then
		asset:Resume()
		AudioPlayer.increaseVolume(asset, 0.5)
	end
end

AudioPlayer.reduceVolume = function(asset, goalVolume) 
	for i = asset.Volume, goalVolume, -0.02 do
		asset.Volume = i
		wait()
	end 
end

AudioPlayer.increaseVolume = function(asset, goalVolume) 
	for i = 0, goalVolume, 0.02 do
		asset.Volume = i
		wait()
	end 
end

AudioPlayer.getCurrentlyPlaying = function(assets)
	for _, asset in ipairs(assets) do
		if asset.IsPlaying then
			return asset
		end
	end
end

AudioPlayer.getCurrentlyPaused = function(assets)
	for _, asset in ipairs(assets) do
		if asset.IsPaused and asset.TimePosition > 0 then
			return asset
		end
	end
end

return AudioPlayer
