main.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. success = self.tex.read(media_file)
  33. assert success, "Failed to load video!"
  34. # Set up a fullscreen card to set the video texture on.
  35. cm = CardMaker("My Fullscreen Card")
  36. cm.setFrameFullscreenQuad()
  37. # Tell the CardMaker to create texture coordinates that take into
  38. # account the padding region of the texture.
  39. cm.setUvRange(self.tex)
  40. # Now place the card in the scene graph and apply the texture to it.
  41. card = NodePath(cm.generate())
  42. card.reparentTo(self.render2d)
  43. card.setTexture(self.tex)
  44. self.sound = loader.loadSfx(media_file)
  45. # Synchronize the video to the sound.
  46. self.tex.synchronizeTo(self.sound)
  47. self.accept('p', self.playpause)
  48. self.accept('P', self.playpause)
  49. self.accept('s', self.stopsound)
  50. self.accept('S', self.stopsound)
  51. self.accept('m', self.fastforward)
  52. self.accept('M', self.fastforward)
  53. def stopsound(self):
  54. self.sound.stop()
  55. self.sound.setPlayRate(1.0)
  56. def fastforward(self):
  57. if self.sound.status() == AudioSound.PLAYING:
  58. t = self.sound.getTime()
  59. self.sound.stop()
  60. if self.sound.getPlayRate() == 1.0:
  61. self.sound.setPlayRate(0.5)
  62. else:
  63. self.sound.setPlayRate(1.0)
  64. self.sound.setTime(t)
  65. self.sound.play()
  66. def playpause(self):
  67. if self.sound.status() == AudioSound.PLAYING:
  68. t = self.sound.getTime()
  69. self.sound.stop()
  70. self.sound.setTime(t)
  71. else:
  72. self.sound.play()
  73. player = MediaPlayer("PandaSneezes.ogv")
  74. player.run()