Source: ZegoExpressMediaPlayer.js

  1. const EventEmitter = require('events').EventEmitter;
  2. const WebGLRender = require('./ZegoExpressWebgl');
  3. const {ZegoViewMode} = require('./ZegoExpressDefines');
  4. /**
  5. * ZegoMediaPlayer
  6. */
  7. class ZegoMediaPlayer extends EventEmitter {
  8. /**
  9. * Start playing.
  10. *
  11. * You need to load resources before playing
  12. */
  13. start(){
  14. this.nativeMediaPlayer.start({});
  15. }
  16. /**
  17. * Stop playing.
  18. */
  19. stop(){
  20. this.nativeMediaPlayer.stop({});
  21. }
  22. /**
  23. * Pause playing.
  24. */
  25. pause(){
  26. this.nativeMediaPlayer.pause({});
  27. }
  28. /**
  29. * Resume playing.
  30. */
  31. resume(){
  32. this.nativeMediaPlayer.resume({});
  33. }
  34. /**
  35. * Load media resource.
  36. *
  37. * Yon can pass the absolute path of the local resource or the URL of the network resource
  38. * @param {string} path - the absolute path of the local resource or the URL of the network resource
  39. * @return {Promise<number>} - load resource result
  40. */
  41. loadResource(path){
  42. return this.nativeMediaPlayer.loadResource({path});
  43. }
  44. /**
  45. * Get the current playback status.
  46. *
  47. * @return {ZegoMediaPlayerState} - current state
  48. */
  49. getCurrentState(){
  50. return this.nativeMediaPlayer.getCurrentState({});
  51. }
  52. /**
  53. * Set the specified playback progress.
  54. *
  55. * Unit is millisecond
  56. * @param {number} millisecond - Point in time of specified playback progress
  57. * @return {Promise<number>} - seek result
  58. */
  59. seekTo(millisecond){
  60. return this.nativeMediaPlayer.seekTo({millisecond});
  61. }
  62. /**
  63. * Set mediaplayer volume. Both the local play volume and the publish volume are set.
  64. *
  65. * @param {number} volume - The range is 0 ~ 200. The default is 60.
  66. */
  67. setVolume(volume){
  68. this.nativeMediaPlayer.setVolume({volume});
  69. }
  70. /**
  71. * Set mediaplayer local playback volume.
  72. *
  73. * @param {number} volume - The range is 0 ~ 200. The default is 60.
  74. */
  75. setPlayVolume(volume){
  76. this.nativeMediaPlayer.setPlayVolume({volume});
  77. }
  78. /**
  79. * Set mediaplayer publish volume.
  80. *
  81. * @param {number} volume - The range is 0 ~ 200. The default is 60.
  82. */
  83. setPublishVolume(volume){
  84. this.nativeMediaPlayer.setPublishVolume({volume});
  85. }
  86. /**
  87. * Gets the current local playback volume of the mediaplayer, the range is 0 ~ 200, with the default value of 60.
  88. *
  89. * @return {number} - current volume
  90. */
  91. getPlayVolume(){
  92. return this.nativeMediaPlayer.getPlayVolume({});
  93. }
  94. /**
  95. * Gets the current publish volume of the mediaplayer, the range is 0 ~ 200, with the default value of 60.
  96. *
  97. * @return {number} - current volume
  98. */
  99. getPublishVolume(){
  100. return this.nativeMediaPlayer.getPublishVolume({});
  101. }
  102. /**
  103. * Get the total progress of your media resources.
  104. *
  105. * You should load resource before invoking this function, otherwise the return value is 0
  106. * @return {number} - Unit is millisecond
  107. */
  108. getTotalDuration(){
  109. return this.nativeMediaPlayer.getTotalDuration({});
  110. }
  111. /**
  112. * Get current playing progress.
  113. *
  114. * You should load resource before invoking this function, otherwise the return value is 0
  115. * @return {number} - current progress
  116. */
  117. getCurrentProgress(){
  118. return this.nativeMediaPlayer.getCurrentProgress({});
  119. }
  120. /**
  121. * Whether to play locally silently.
  122. *
  123. * If [enableAux] switch is turned on, there is still sound in the publishing stream. The default is false.
  124. * @param {boolean} mute - Mute local audio flag, The default is false.
  125. */
  126. muteLocal(mute){
  127. this.nativeMediaPlayer.muteLocal({mute});
  128. }
  129. /**
  130. * Set the view of the player playing video.
  131. *
  132. * @param {ZegoView} view - Video rendered view object
  133. */
  134. setPlayerView(view){
  135. if(view == null){
  136. this.localGLRender = null;
  137. }
  138. else{
  139. view = Object.assign({ viewMode: ZegoViewMode.AspectFit, backgroundColor: 0x000000, preserveDrawingBuffer: false}, view);
  140. this.localGLRender = new WebGLRender();
  141. this.localGLRender.setViewMode(view.viewMode);
  142. this.localGLRender.enablePreserveDrawingBuffer(view.preserveDrawingBuffer);
  143. this.localGLRender.initBkColor(view.backgroundColor);
  144. this.localGLRender.initGLfromCanvas(view.canvas);
  145. this.nativeMediaPlayer.setPlayerCanvas({});
  146. }
  147. }
  148. /**
  149. * Whether to mix the player's sound into the stream being published.
  150. *
  151. * @param {boolean} enable - Aux audio flag. The default is false.
  152. */
  153. enableAux(enable){
  154. this.nativeMediaPlayer.enableAux({enable});
  155. }
  156. /**
  157. * Whether to mix the player's video into the stream being published.
  158. *
  159. * @param {boolean} enable - enable publish video
  160. * @param {number} channel - channel
  161. */
  162. enablePublishVideo(enable, channel){
  163. this.nativeMediaPlayer.enablePublishVideo({enable, channel});
  164. }
  165. /**
  166. * Whether to repeat playback.
  167. *
  168. * @param {boolean} enable - repeat playback flag. The default is false.
  169. */
  170. enableRepeat(enable){
  171. this.nativeMediaPlayer.enableRepeat({enable});
  172. }
  173. /**
  174. * Set playback progress callback interval.
  175. *
  176. * This function can control the callback frequency of [onMediaPlayerPlayingProgress]. When the callback interval is set to 0, the callback is stopped. The default callback interval is 1s
  177. * This callback are not returned exactly at the set callback interval, but rather at the frequency at which the audio or video frames are processed to determine whether the callback is needed to call
  178. * @param {number} millisecond - Interval of playback progress callback in milliseconds
  179. */
  180. setProgressInterval(millisecond){
  181. this.nativeMediaPlayer.setProgressInterval({millisecond});
  182. }
  183. /**
  184. * Get the number of audio tracks of the playback file.
  185. *
  186. * @return {number} - Number of audio tracks
  187. */
  188. getAudioTrackCount(){
  189. return this.nativeMediaPlayer.getAudioTrackCount({});
  190. }
  191. /**
  192. * Set the audio track of the playback file.
  193. *
  194. * @param {number} index - Audio track index, the number of audio tracks can be obtained through the [getAudioTrackCount] function.
  195. */
  196. setAudioTrackIndex(index){
  197. this.nativeMediaPlayer.setAudioTrackIndex({index});
  198. }
  199. /**
  200. * Setting up the specific voice changer parameters.
  201. *
  202. * @param {ZegoMediaPlayerAudioChannel} audioChannel - The audio channel to be voice changed
  203. * @param {ZegoVoiceChangerParam} param - Voice changer parameters
  204. */
  205. setVoiceChangerParam(audioChannel, param){
  206. this.nativeMediaPlayer.setVoiceChangerParam({audioChannel, param});
  207. }
  208. /**
  209. * Take a screenshot of the current playing screen of the media player.
  210. *
  211. * Only in the case of calling `setPlayerCanvas` to set the display controls and the playback state, can the screenshot be taken normally
  212. * @return {Promise<number, string>} - snapshot error code(errorCode) and base64 string of the image in jpg format(image)
  213. */
  214. takeSnapshot(){
  215. return this.nativeMediaPlayer.takeSnapshot({});
  216. }
  217. getCustomVideoCapturePlugin(){
  218. return this.nativeMediaPlayer.getCustomVideoCapturePlugin();
  219. }
  220. /**
  221. * @event ZegoMediaPlayer#onMediaPlayerStateUpdate
  222. * @desc The callback triggered when the state of the media player changes.
  223. *
  224. * @property {object} result - param object
  225. * @property {ZegoMediaPlayerState} result.state - Media player status
  226. * @property {number} result.errorCode - Error code, please refer to the error codes document https://doc-en.zego.im/en/5548.html for details.
  227. */
  228. /**
  229. * @event ZegoMediaPlayer#onMediaPlayerNetworkEvent
  230. * @desc The callback triggered when the network status of the media player changes.
  231. *
  232. * @property {object} result - param object
  233. * @property {ZegoMediaPlayerNetworkEvent} result.networkEvent - Network status event
  234. */
  235. /**
  236. * @event ZegoMediaPlayer#onMediaPlayerPlayingProgress
  237. * @desc The callback to report the current playback progress of the media player.
  238. *
  239. * @property {object} result - param object
  240. * @property {number} result.millisecond - Progress in milliseconds
  241. */
  242. callEmit() {
  243. try {
  244. if (arguments[0] === "onVideoData") {
  245. if(this.localGLRender){
  246. let videoFrame = {
  247. "videoFrameParam": arguments[1]["videoFrameParam"],
  248. "videoFrameBuffer": Buffer.from(arguments[1]["videoFrameBuffer"])
  249. }
  250. this.localGLRender.drawVideoFrame(videoFrame);
  251. }
  252. }
  253. } catch (error) {
  254. // console.log("callEmit: ", error);
  255. }
  256. try {
  257. this.emit(arguments[0], arguments[1]);
  258. } catch (error) {
  259. console.log(`error catched in your callback ${arguments[0]} : ${error}`)
  260. }
  261. }
  262. }
  263. module.exports = ZegoMediaPlayer;