summaryrefslogtreecommitdiffhomepage
path: root/not/MusicPlayer.lua
blob: 17beda4ac4ddea8e5fac2c8d6e093af31cd6c69f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
--- Simple music player object that stores, playes and loops tracks..
MusicPlayer = require "not.Object":extends()

function MusicPlayer:new (trackName)
	self.tracks = {}
	if trackName then
		self:setTrack(trackName)
		self:play()
	end
end

function MusicPlayer:delete ()
	self:stop()
	self.tracks = nil
	self.source = nil
end

function MusicPlayer:setTrack (trackName)
	if self.source then
		self.source:stop()
	end
	if self.tracks[trackName] then
		self.source = self.tracks[trackName]
	else
		local source = love.audio.newSource("assets/music/" .. trackName)
		source:setLooping(true)
		source:setVolume(.7)
		self.source = source
		self.tracks[trackName] = source
	end
end

function MusicPlayer:getCurrentTrack ()
	for key,track in pairs(self.tracks) do
		if self.tracks[key] == self.source then
			return key
		end
	end
end

function MusicPlayer:play (trackName)
	if trackName then
		self:setTrack(trackName)
	end
	self.source:play()
end

function MusicPlayer:stop ()
	self.source:stop()
end

return MusicPlayer