Browse Source

Music playback backend now present

Sebastian Lohff 6 years ago
parent
commit
645745ed7c
3 changed files with 56 additions and 6 deletions
  1. 35
    0
      music.py
  2. 4
    0
      seopardy.py
  3. 17
    6
      windows.py

+ 35
- 0
music.py View File

@@ -0,0 +1,35 @@
1
+from __future__ import print_function
2
+
3
+import sys
4
+from PySide import phonon
5
+
6
+class MusicBox(object):
7
+	# default media
8
+	media = {}
9
+
10
+	# state
11
+	_media_obj = None
12
+
13
+	@classmethod
14
+	def init(clazz):
15
+		clazz._output = phonon.Phonon.AudioOutput(phonon.Phonon.MusicCategory)
16
+		clazz._media_obj = phonon.Phonon.MediaObject()
17
+		phonon.Phonon.createPath(clazz._media_obj, clazz._output)
18
+
19
+	@classmethod
20
+	def add_music(clazz, name, media_path):
21
+		clazz.media[name] = phonon.Phonon.MediaSource(media_path)
22
+
23
+	@classmethod
24
+	def play_music(clazz, name):
25
+		try:
26
+			clazz._media_obj.setCurrentSource(clazz.media[name])
27
+			clazz._media_obj.play()
28
+		except KeyError:
29
+			print("Warning: Song '%s' is missing, not found in media base" % (name,), file=sys.stderr)
30
+
31
+	@classmethod
32
+	def stop_music(clazz):
33
+		# use pause so we don't stop having the device open
34
+		clazz._media_obj.pause()
35
+

+ 4
- 0
seopardy.py View File

@@ -7,6 +7,7 @@ import sys
7 7
 #from PySide import QtCore, QtGui
8 8
 from PySide import QtGui
9 9
 
10
+from music import MusicBox
10 11
 from question import Questions
11 12
 from gamestate import GameState
12 13
 from game import SeopardyGame
@@ -51,6 +52,9 @@ if __name__ == '__main__':
51 52
 	else:
52 53
 		gamestate = GameState(args.savedir)
53 54
 
55
+	# init music box
56
+	MusicBox.init()
57
+
54 58
 	# create board
55 59
 	board = SeopardyGame(questions, gamestate)
56 60
 	board.show()

+ 17
- 6
windows.py View File

@@ -3,6 +3,7 @@ from PySide import QtGui, QtCore
3 3
 import copy
4 4
 
5 5
 from gamestate import QuestionAnswers
6
+from music import MusicBox
6 7
 from player import Player, ButtonEvent, nobodyColor
7 8
 
8 9
 class QuestionWindow(QtGui.QDialog):
@@ -13,16 +14,18 @@ class QuestionWindow(QtGui.QDialog):
13 14
 		self.section = section
14 15
 		self.qnumber = qnumber
15 16
 		self.question = question
17
+		self.dj = dj
16 18
 		if answers is not None:
17 19
 			self.answers = answers
18 20
 		else:
19 21
 			self.answers = QuestionAnswers(self.section, self.qnumber)
20 22
 
21
-		self.dj = dj
22
-		if self.dj and not self.answers.is_answered():
23
-			self.answers.set_dj_points(self.dj[1])
24
-			playerNo = self.players.index(self.dj[0])+1
25
-			QtCore.QCoreApplication.postEvent(self, ButtonEvent(playerNo))
23
+		if not self.answers.is_answered():
24
+			MusicBox.play_music("questionSong")
25
+			if self.dj:
26
+				self.answers.set_dj_points(self.dj[1])
27
+				playerNo = self.players.index(self.dj[0])+1
28
+				QtCore.QCoreApplication.postEvent(self, ButtonEvent(playerNo))
26 29
 
27 30
 		self._setupGui()
28 31
 		self.setWindowTitle("Seopardy - %s - %d" % (section, qnumber*100))
@@ -31,6 +34,13 @@ class QuestionWindow(QtGui.QDialog):
31 34
 	def get_answers(self):
32 35
 		return self.answers
33 36
 
37
+	def closeEvent(self, event):
38
+		if not self._inWindow:
39
+			MusicBox.stop_music()
40
+			event.accept()
41
+		else:
42
+			event.ignore()
43
+
34 44
 	def _mkQuestionLabel(self, text):
35 45
 		question = QtGui.QLabel(text, alignment=QtCore.Qt.AlignCenter)
36 46
 		question.setWordWrap(True)
@@ -73,7 +83,8 @@ class QuestionWindow(QtGui.QDialog):
73 83
 				res = qawin.exec_()
74 84
 				if res == QuestionAnswerWindow.CORRECT:
75 85
 					self.answers.add_try(player, correct=True)
76
-					self.accept()
86
+					self._inWindow = False
87
+					self.close()
77 88
 				elif res == QuestionAnswerWindow.WRONG:
78 89
 					self.answers.add_try(player, correct=False)
79 90
 				self._inWindow = False

Loading…
Cancel
Save