【问题标题】:How do I stop Music from playing with my stop button?如何使用停止按钮停止音乐播放?
【发布时间】:2020-05-12 12:29:19
【问题描述】:

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


    【解决方案1】:

    您的 buttonHit 功能有误。

    您传入参数 action,但是,由于您使用的是小部件库,因此传入函数的唯一内容是 event。此外,您为按钮指定了 id,而不是操作。然后这个 id 就属于事件目标,即被按下的按钮。

    你想要的是类似的东西:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多