Home Reference Source Repository

js/SceneKit/SCNActionPlaySound.js

  1. 'use strict'
  2.  
  3. import SCNAction from './SCNAction'
  4. import SCNActionTimingMode from './SCNActionTimingMode'
  5.  
  6. export default class SCNActionPlaySound extends SCNAction {
  7. static get _propTypes() {
  8. return {
  9. _actions: 'NSArray',
  10. _finished: 'boolean',
  11. _duration: 'float',
  12. _timingMode: 'integer',
  13. _beginTime: 'float',
  14. _isRunning: 'boolean',
  15. _pausedTime: 'float',
  16.  
  17. name: ['string', null]
  18. }
  19. }
  20.  
  21. /**
  22. * constructor
  23. * @access public
  24. * @constructor
  25. */
  26. constructor() {
  27. super()
  28.  
  29. this._actions = []
  30. this._finished = false
  31. this._duration = 0
  32. this._timingMode = SCNActionTimingMode.linear
  33. this._beginTime = 0
  34. this._isRunning = false
  35. this._pausedTime = 0
  36.  
  37. this._source = null
  38. this._wait = false
  39. }
  40.  
  41. /**
  42. * Creates an action that plays an audio source.
  43. * @access public
  44. * @param {SCNAudioSource} source - The audio source to play.
  45. * @param {boolean} wait - If true, the duration of this action is the same as the length of the audio playback. If false, the action is considered to have completed immediately.
  46. * @returns {SCNAction} -
  47. * @desc When the action executes, SceneKit plays the audio source on the target node—any positional audio effects are based on the node’s position. For more information about positional audio in SceneKit, see SCNAudioPlayer.This action is not reversible; the reverse of this action is the same action.
  48. * @see https://developer.apple.com/documentation/scenekit/scnaction/1523651-playaudio
  49. */
  50. static playAudioWaitForCompletion(source, wait) {
  51. const action = new SCNActionPlaySound()
  52. action._source = source
  53. action._wait = wait
  54. return action
  55. }
  56.  
  57. /**
  58. * @access public
  59. * @returns {SCNActionPlaySound} -
  60. */
  61. copy() {
  62. const action = super.copy()
  63.  
  64. action._source = this._source
  65. action._wait = this._wait
  66.  
  67. return action
  68. }
  69.  
  70. /**
  71. * apply action to the given node.
  72. * @access private
  73. * @param {Object} obj - target object to apply this action.
  74. * @param {number} time - active time
  75. * @param {boolean} [needTimeConversion = true] -
  76. * @returns {void}
  77. */
  78. _applyAction(obj, time, needTimeConversion = true) {
  79. if(!this._isRunning){
  80. this._source._play()
  81. this._isRunning = true
  82. }
  83. if(this._duration <= 0 || this._source._duration > 0){
  84. this._duration = this._source._duration
  85. }
  86. const t = this._getTime(time, needTimeConversion)
  87.  
  88. if(!this._wait){
  89. this._finished = true
  90. }else if(!this._source.loops && t >= 1){
  91. this._finished = true
  92. }else{
  93. this._finished = false
  94. }
  95. }
  96. }
  97.  
  98. SCNAction.playAudioWaitForCompletion = SCNActionPlaySound.playAudioWaitForCompletion