如何使用停止按钮停止播放音乐?

问题描述 投票:0回答:1

我正在使用Corona SDK创建两个按钮,一个用于播放音乐,另一个用于停止。该程序工作正常,直到我创建了停止按钮,但没有任何效果。没有音频。有人可以帮我解决这个问题吗?

          local widget = require("widget")
          display.setStatusBar(display.HiddenStatusBar)

            centerX = display.contentWidth * .5
            centerY = display.contentHeight * .5

   -- Background
   local bg = display.newImageRect("bg_iPhone.png", 1100, 3200)
   bg.x = centerX
   bg.y = centerY

   local isAudioOn = true

   local soundJump = audio.loadSound("boing.mp3") --[[loadSound is for animations]]--
   local soundMusic = audio.loadStream("HappyPants.wav") --[[loadStream is for background music]]--

 --Sound function to play music
   local function playSound()
     if isAudioOn then
       audio.play(soundMusic)
       print("Boing!")
     end
   end

 -- Button Function that controls what happens after button is pressed
   local function buttonHit(action)
     if action == "play" then
       playSound()
     elseif action == "stop" then
       audio.stop(playSound)
     end
   end

 -- Play Button
   local playButton = widget.newButton{
   label = "Play",
   id = "play",
   x = 330,
   y = 500,
   labelColor = { default={ 0, 19, 189 }, over={ 0, 19, 189, 1 } },
   onPress = buttonHit
   }

  -- Stop Button
   local stopButton = widget.newButton{
   label = "Stop",
   id = "stop",
   x = 330,
   y= 550,
   labelColor = { default={ 0, 19, 189 }, over={ 0, 19, 189, 1 } },
   onPress = buttonHit
   }
lua corona
1个回答
0
投票

您的buttonHit功能错误。

您传入参数action,但是,由于您使用的是小部件库,因此传递给该函数的唯一内容是event。另外,还给按钮提供了一个[[id,而不是一个动作。然后,此ID属于事件目标,即所按下的按钮。

您想要的是类似的东西:

local function buttonHit( event ) if event.target.id == "play" then playSound() else audio.stop(playSound) end end

© www.soinside.com 2019 - 2024. All rights reserved.