MultiSoundEffect.qml 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (C) 2016 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
  3. import QtQuick
  4. import QtMultimedia
  5. Item {
  6. id: multiSoundEffect
  7. property url source
  8. property int maxInstances: 2
  9. property var __cachedInstances
  10. property int __currentIndex: 0
  11. property real soundVolume: 1.0
  12. signal playingChanged(url source, bool playing)
  13. Component {
  14. id: soundEffectComp
  15. SoundEffect {
  16. source: multiSoundEffect.source
  17. onPlayingChanged: multiSoundEffect.playingChanged(source, playing)
  18. }
  19. }
  20. onSourceChanged: {
  21. __cachedInstances = []
  22. __currentIndex = 0
  23. if (source != Qt.resolvedUrl("")) {
  24. var i
  25. for (i = 0; i < maxInstances; i++) {
  26. var soundEffect = soundEffectComp.createObject(multiSoundEffect)
  27. if (soundEffect === null)
  28. return
  29. __cachedInstances.push(soundEffect)
  30. }
  31. }
  32. }
  33. function play() {
  34. if (__cachedInstances === undefined || __cachedInstances.length === 0)
  35. return
  36. if (__cachedInstances[__currentIndex].playing) {
  37. __cachedInstances[__currentIndex].stop()
  38. __currentIndex = (__currentIndex + 1) % __cachedInstances.length
  39. }
  40. __cachedInstances[__currentIndex].volume = soundVolume
  41. __cachedInstances[__currentIndex].play()
  42. }
  43. }