set-inner-audio-option.uvue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex:1">
  4. <!-- #endif -->
  5. <page-head title="setInnerAudioOption"></page-head>
  6. <text style="margin-left: 8px">是否允许与其他音频同时播放</text>
  7. <radio-group class="uni-flex" style="margin:8px" @change="onMixChanged">
  8. <radio value="1" :checked="true">是</radio>
  9. <radio value="0" style="margin-left: 16px">否</radio>
  10. </radio-group>
  11. <view style="padding: 8px 8px;">
  12. <button @click="playBackgroundMusic" type="primary">播放背景音频</button>
  13. <button @click="playInnerMusic" style="margin-top: 8px">播放音频</button>
  14. </view>
  15. <view style="padding: 16px 8px;">
  16. <view class="uni-row">
  17. <view>1. </view>uni.setInnerAudioOption需要与uni.createInnerAudioContext搭配才会生效
  18. </view>
  19. <view class="uni-row">
  20. <view>2. </view>设置mixWithOther为true时,会暂停其他App的音频和背景音频
  21. </view>
  22. </view>
  23. <!-- #ifdef APP -->
  24. </scroll-view>
  25. <!-- #endif -->
  26. </template>
  27. <script>
  28. export default {
  29. data() {
  30. return {
  31. backgroundManager: null as BackgroundAudioManager | null,
  32. innerAudio: null as InnerAudioContext | null,
  33. isBackgroundAudioPaused: false
  34. }
  35. },
  36. onUnload() {
  37. this.backgroundManager?.stop()
  38. this.innerAudio?.stop()
  39. },
  40. methods: {
  41. playBackgroundMusic() {
  42. if (this.backgroundManager == null) {
  43. this.backgroundManager = uni.getBackgroundAudioManager()
  44. this.backgroundManager.onPause(() => {
  45. this.isBackgroundAudioPaused = true
  46. })
  47. } else {
  48. this.backgroundManager!.stop()
  49. }
  50. this.backgroundManager!.src = 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3'
  51. this.backgroundManager!.coverImgUrl = 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/music-a.png';
  52. this.backgroundManager!.play()
  53. },
  54. playInnerMusic() {
  55. if (this.innerAudio == null)
  56. this.innerAudio = uni.createInnerAudioContext()
  57. else {
  58. this.innerAudio!.stop()
  59. }
  60. this.innerAudio!.src = 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3'
  61. this.innerAudio!.play()
  62. },
  63. onMixChanged(event : UniRadioGroupChangeEvent) {
  64. uni.setInnerAudioOption({
  65. mixWithOther: event.detail.value == "1"
  66. })
  67. },
  68. testInnerAudioOption() {
  69. uni.setInnerAudioOption({
  70. mixWithOther: false
  71. })
  72. }
  73. }
  74. }
  75. </script>
  76. <style>
  77. </style>