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

How do I stop Music from playing with my stop button?

我正在使用 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
   }

您的 buttonHit 函数有误。

您传入参数 action,但是,由于您使用的是小部件库,因此唯一传入函数的是 event.此外,您已经为按钮提供了 id,而不是操作。此 id 属于事件目标,即被按下的按钮。

您想要的是:

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