main.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python
  2. from panda3d.core import *
  3. # Tell Panda3D to use OpenAL, not FMOD
  4. loadPrcFileData("", "audio-library-name p3openal_audio")
  5. from direct.showbase.DirectObject import DirectObject
  6. from direct.gui.OnscreenText import OnscreenText
  7. from direct.showbase.ShowBase import ShowBase
  8. # Function to put instructions on the screen.
  9. def addInstructions(pos, msg):
  10. return OnscreenText(text=msg, style=1, fg=(0, 0, 0, 1), shadow=(1, 1, 1, 1),
  11. parent=base.a2dTopLeft, align=TextNode.ALeft,
  12. pos=(0.08, -pos - 0.04), scale=.06)
  13. # Function to put title on the screen.
  14. def addTitle(text):
  15. return OnscreenText(text=text, style=1, pos=(-0.1, 0.09), scale=.08,
  16. parent=base.a2dBottomRight, align=TextNode.ARight,
  17. fg=(1, 1, 1, 1), shadow=(0, 0, 0, 1))
  18. class MediaPlayer(ShowBase):
  19. def __init__(self, media_file):
  20. # Initialize the ShowBase class from which we inherit, which will
  21. # create a window and set up everything we need for rendering into it.
  22. ShowBase.__init__(self)
  23. self.title = addTitle("Panda3D: Tutorial - Media Player")
  24. self.inst1 = addInstructions(0.06, "P: Play/Pause")
  25. self.inst2 = addInstructions(0.12, "S: Stop and Rewind")
  26. self.inst3 = addInstructions(0.18,
  27. "M: Slow Motion / Normal Motion toggle")
  28. # Load the texture. We could use loader.loadTexture for this,
  29. # but we want to make sure we get a MovieTexture, since it
  30. # implements synchronizeTo.
  31. self.tex = MovieTexture("name")
  32. assert self.tex.read(media_file), "Failed to load video!"
  33. # Set up a fullscreen card to set the video texture on.
  34. cm = CardMaker("My Fullscreen Card")
  35. cm.setFrameFullscreenQuad()
  36. # Tell the CardMaker to create texture coordinates that take into
  37. # account the padding region of the texture.
  38. cm.setUvRange(self.tex)
  39. # Now place the card in the scene graph and apply the texture to it.
  40. card = NodePath(cm.generate())
  41. card.reparentTo(self.render2d)
  42. card.setTexture(self.tex)
  43. self.sound = loader.loadSfx(media_file)
  44. # Synchronize the video to the sound.
  45. self.tex.synchronizeTo(self.sound)
  46. self.accept('p', self.playpause)
  47. self.accept('P', self.playpause)
  48. self.accept('s', self.stopsound)
  49. self.accept('S', self.stopsound)
  50. self.accept('m', self.fastforward)
  51. self.accept('M', self.fastforward)
  52. def stopsound(self):
  53. self.sound.stop()
  54. self.sound.setPlayRate(1.0)
  55. def fastforward(self):
  56. if self.sound.status() == AudioSound.PLAYING:
  57. t = self.sound.getTime()
  58. self.sound.stop()
  59. if self.sound.getPlayRate() == 1.0:
  60. self.sound.setPlayRate(0.5)
  61. else:
  62. self.sound.setPlayRate(1.0)
  63. self.sound.setTime(t)
  64. self.sound.play()
  65. def playpause(self):
  66. if self.sound.status() == AudioSound.PLAYING:
  67. t = self.sound.getTime()
  68. self.sound.stop()
  69. self.sound.setTime(t)
  70. else:
  71. self.sound.play()
  72. player = MediaPlayer("PandaSneezes.ogv")
  73. player.run()