PageRenderTime 62ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/IronPython/Samples/IronTunes/Player.py

https://github.com/thomo13/ironruby
Python | 188 lines | 164 code | 8 blank | 16 comment | 0 complexity | 5f59dd896a211fd82b46750ba0c97f4e MD5 | raw file
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. import clr
  16. clr.AddReferenceToFile("QuartzTypeLib.dll")
  17. import QuartzTypeLib
  18. import System.Math
  19. from System.ComponentModel import INotifyPropertyChanged
  20. from System.ComponentModel import PropertyChangedEventArgs
  21. #define for window message to tell us there are filter graph events
  22. WM_GRAPHNOTIFY = 0x8001
  23. #event code that indicates the file finished playing
  24. EC_COMPLETE = 1
  25. class Player(INotifyPropertyChanged):
  26. "class that handles the playing operations"
  27. def __init__(self,app,dataModel):
  28. self.graphManager = QuartzTypeLib.FilgraphManagerClass()
  29. self.__filename = ""
  30. self.__app = app
  31. self.__selectedItem = None
  32. self.__playingItem = None
  33. self.__dataModel = dataModel
  34. self.__volume = 100
  35. self._events = []
  36. def add_PropertyChanged(self, value):
  37. self._events.append(value)
  38. def remove_PropertyChanged(self, value):
  39. self._events.remove(value)
  40. def NotifyPropertyChanged(self, info):
  41. for x in self._events:
  42. x(self,PropertyChangedEventArgs(info))
  43. def setupEvents(self):
  44. if (hasattr(self.__dataModel,"windowHandle")):
  45. try:
  46. QIHelper.SetNotifyWindowForMe(self.graphManager,self.__dataModel.windowHandle.ToInt32(),WM_GRAPHNOTIFY,0)
  47. except:
  48. pass
  49. def LoadFile(self, filename):
  50. self.graphManager = QuartzTypeLib.FilgraphManagerClass()
  51. self.graphManager.RenderFile(filename)
  52. self.setVolume(self.__volume) #we need to reset the volume in to the new graph manager
  53. self.setupEvents()
  54. self.filename = filename
  55. def Play(self):
  56. self.graphManager.Run()
  57. def Pause(self):
  58. self.graphManager.Pause()
  59. def Stop(self):
  60. self.graphManager.Stop()
  61. def getPosition(self):
  62. return self.graphManager.CurrentPosition
  63. def setPosition(self, newPosition):
  64. self.graphManager.CurrentPosition = newPosition
  65. def PlayPause(self):
  66. state = self.graphManager.GetState(10)
  67. if (state == 2): # playing
  68. self.Pause()
  69. elif (state == 1): # paused
  70. self.Play()
  71. elif (state == 0): # stopped
  72. if ((self.SelectedItem != None) and (self.filename != self.SelectedItem.Path)):
  73. self.Stop()
  74. self.PlayingItem = self.SelectedItem
  75. self.LoadFile(self.SelectedItem.Path)
  76. self.Play()
  77. else:
  78. self.Play()
  79. else:
  80. pass #for now just do nothing
  81. self.NotifyPropertyChanged("IsPlaying")
  82. self.NotifyPropertyChanged("Duration")
  83. def ProcessEvents(self):
  84. while (True):
  85. eventCode,param1,param2 = self.graphManager.GetEvent(10) #raises exception when no events are left
  86. if (eventCode == EC_COMPLETE):
  87. self.PlayingIndex += 1
  88. def Tick(self):
  89. self.NotifyPropertyChanged("Position")
  90. def LibraryMouseDown(self,eventargs):
  91. if (eventargs.ClickCount == 2):
  92. if (self.selectedItem != None):
  93. self.Stop()
  94. self.RenderFile(self.selectedItem.Path)
  95. self.Play()
  96. def getFilename(self):
  97. return self.__filename
  98. def setFilename(self, value):
  99. self.__filename = value
  100. self.NotifyPropertyChanged("filename")
  101. def getVolume(self):
  102. return self.__volume
  103. def setVolume(self,value):
  104. self.__volume = value
  105. newVal = int((value - 100.0)*25) #clipping the bottom 3/4 of the range since it's basically unhearable
  106. if (newVal < -2499):
  107. newVal = -10000
  108. self.graphManager.Volume = newVal
  109. self.NotifyPropertyChanged("Volume")
  110. def getSelectedItem(self):
  111. return self.__selectedItem
  112. def setSelectedItem(self,value):
  113. self.__selectedItem = value
  114. self.NotifyPropertyChanged("SelectedItem")
  115. def getSelectedIndex(self):
  116. return self.__dataModel.library.ViewIndexOf(self.SelectedItem)
  117. def setSelectedIndex(self,value):
  118. self.SelectedItem = self.__dataModel.library.ViewGetValue(value)
  119. self.NotifyPropertyChanged("SelectedIndex")
  120. def setPlayingItem(self,value):
  121. if value:
  122. self.Stop()
  123. self.__playingItem = value
  124. self.LoadFile(value.Path)
  125. self.Play()
  126. self.SelectedItem = self.PlayingItem
  127. self.NotifyPropertyChanged("PlayingItem")
  128. def getPlayingItem(self):
  129. return self.__playingItem
  130. def setPlayingIndex(self,value):
  131. self.PlayingItem = self.__dataModel.library.ViewGetValue(value)
  132. self.NotifyPropertyChanged("PlayingIndex")
  133. def getPlayingIndex(self):
  134. return self.__dataModel.library.ViewIndexOf(self.PlayingItem)
  135. def isPlaying(self):
  136. return (self.graphManager.GetState(10) == 2)
  137. def getDuration(self):
  138. try:
  139. return self.graphManager.StopTime
  140. except:
  141. return 0
  142. Player.filename = property(Player.getFilename, Player.setFilename)
  143. Player.volume = property(Player.getVolume, Player.setVolume)
  144. Player.SelectedItem = property(Player.getSelectedItem, Player.setSelectedItem)
  145. Player.SelectedIndex = property(Player.getSelectedIndex, Player.setSelectedIndex)
  146. Player.PlayingItem = property(Player.getPlayingItem, Player.setPlayingItem)
  147. Player.PlayingIndex = property(Player.getPlayingIndex, Player.setPlayingIndex)
  148. Player.IsPlaying = property(Player.isPlaying)
  149. Player.Duration = property(Player.getDuration)
  150. Player.Position = property(Player.getPosition,Player.setPosition)