How to stop the music when screen is locked - corona

But why does it happen? 

main.lua

backgroundMusic = audio.loadStream("backgroundMusic.mp3") audio.setVolume( 1 )  audio.setVolume( 0.26, { channel = 0 } )  audio.setVolume( 0.1, { channel = 1 } )manu.lua

manu.lua

function startMusic( )     audio.play( backgroundMusic , { loops = -1, channel = 1 } )      music\_botton = display.newImage("musicIcon.png")     music\_botton.x=\_W\*0.065      music\_botton.y=\_H\*0.035      music\_botton.width= \_W\*0.11     music\_botton.height = \_W\*0.11     music\_botton:addEventListener( "tap", mute )     end     function mute(  )     music\_botton:removeSelf()     music\_botton = nil     audio.pause( backgroundMusic )     music\_botton = display.newImage("muteIcon.png")     music\_botton.x=\_W\*0.065      music\_botton.y=\_H\*0.035      music\_botton.width= \_W\*0.115     music\_botton.height = \_W\*0.115     music\_botton:addEventListener( "tap", resumMusic)     end     function resumMusic(  )     music\_botton:removeSelf()     music\_botton = nil     audio.resume( backgroundMusic )     music\_botton = display.newImage("musicIcon.png")     music\_botton.x=\_W\*0.065      music\_botton.y=\_H\*0.035      music\_botton.width= \_W\*0.11     music\_botton.height = \_W\*0.11     music\_botton:addEventListener( "tap", mute )     end     startMusic(  )

This is the code.

@idanahal3,

Hi.  I fielded a similar question some time back where we didn’t get to the bottom of the issue.  (I’d really like to this time.)

Thanks for the code, I’ll try to reproduce this error today.  Meanwhile…  Please bear with me here.  I have some questions and I want to ask you about your original post to be sure I’m on the right track with assistance.

Questions

  1. You’re running Android - Which version exactly?  Ex: I’m running Android 4.4.4 on my device (look under Settings -> About Phone/Tablet)

  2. Which specific device are you running into this problem on?

  3. Just for fun, which version of Corona are you using?  Ex: I’m currently using 2015.2687 (You can see this when you launch the simulator and from the ‘Help --> About Simulator…’ menu)

Re: Your Original Post

Your original post (paraphrased) said that:

  1. Your device locks the screen (based on whatever energy saver setting is currently being used).

  2. When the screen is locked, the music keeps playing.

This sounds to me very much like ‘screen lock’ is not suspending.  i.e. It is just turning off the screen.  

At this time, I do not believe we (Corona users) have a way to detect this event. i.e. ‘Screen locked’ event.  The system is still active and it has not suspended the app.  Thus, Corona has received no events.

@all - Please correct me if I’m misinterpreted this post or missed a point that is being asked.

@staff - I wonder if we don’t need another system event?  This seems like something new that is happening either:

a. With recent Android devices.

b. With newer Android (Lollipop maybe?)

More data.  This is the event I think we’re not getting:

http://stackoverflow.com/questions/3170563/android-detect-phone-lock-event

I thought that I had responded to this (perhaps its the other thread) where I tested this and sound cut off correctly. Maybe I used the wrong definition of locked…

Rob

When I press the lock botton the music stops. just when it locks himself it dose not stop.

1. I’m running Android 5.1.1 on my device

  1. Nexus 5

  2. 20142511 (2014.11.18)