I play background music. When the screen locked (goes black) the music doesnot stops, it keeps playing. why? And how can I change it.
Are you building for iOS or Android?
Android
That’s a surprise. We don’t support backgrounded actions in particular on Android. You can listen for system events and when you get a Suspend event, you can stop your music and in the resume event you can resume it.
Rob
Wow that’s weird actually… you mind posting some code of it?
** UPDATE: This is me (RG) misreading Sonic’s question. He was asking to see idanahal3’s code ** :rolleyes:
Sonic,
The code is out there, just search the docs or google it:
This google search: corona sdk suspend event
Turned up this thread:
http://forums.coronalabs.com/topic/43082-discussion-on-application-suspendresume-exit-system-events/
With this code:
local function onSystemEvent( event ) if (event.type == "applicationStart") then print("Application started") elseif (event.type == "applicationExit") then print("Application exited") elseif ( event.type == "applicationSuspend" ) then print("Application suspended") elseif event.type == "applicationResume" then print("Application resumed from suspension") end end --setup the system listener to catch applicationExit etc Runtime:addEventListener( "system", onSystemEvent )
You can also find all you need to know in the docs:
While I’m glad to help, asking for someone to write code which is already spelled out explicitly in the docs is kinda off-putting. I’m sure you’re heads down making your game, but we’re all working hard on stuff.
Please save questions for stuff that isn’t clearly documented, or if you can’t find it, we’ll happily point the way.
Oh sorry just i havent really searched much on this topic but it sounds interesting … i might try it later on. But thanks!
Oh, crud. Did I misread you again? You were asking to see the original poster’s code weren’t you?
Sigh… <Forehead smack!>
Hi. My (rather grumpy) response to Sonic has the code and links you’ll need to piece together to stop your music/sounds on the suspend event.
If you need to pause, use the audio.pause() function instead.
Now on to trying to actually help someone…
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.
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
-
You’re running Android - Which version exactly? Ex: I’m running Android 4.4.4 on my device (look under Settings -> About Phone/Tablet)
-
Which specific device are you running into this problem on?
-
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:
-
Your device locks the screen (based on whatever energy saver setting is currently being used).
-
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
-
Nexus 5
-
20142511 (2014.11.18)
Are you building for iOS or Android?
Android
That’s a surprise. We don’t support backgrounded actions in particular on Android. You can listen for system events and when you get a Suspend event, you can stop your music and in the resume event you can resume it.
Rob
Wow that’s weird actually… you mind posting some code of it?
** UPDATE: This is me (RG) misreading Sonic’s question. He was asking to see idanahal3’s code ** :rolleyes:
Sonic,
The code is out there, just search the docs or google it:
This google search: corona sdk suspend event
Turned up this thread:
http://forums.coronalabs.com/topic/43082-discussion-on-application-suspendresume-exit-system-events/
With this code:
local function onSystemEvent( event ) if (event.type == "applicationStart") then print("Application started") elseif (event.type == "applicationExit") then print("Application exited") elseif ( event.type == "applicationSuspend" ) then print("Application suspended") elseif event.type == "applicationResume" then print("Application resumed from suspension") end end --setup the system listener to catch applicationExit etc Runtime:addEventListener( "system", onSystemEvent )
You can also find all you need to know in the docs:
While I’m glad to help, asking for someone to write code which is already spelled out explicitly in the docs is kinda off-putting. I’m sure you’re heads down making your game, but we’re all working hard on stuff.
Please save questions for stuff that isn’t clearly documented, or if you can’t find it, we’ll happily point the way.
Oh sorry just i havent really searched much on this topic but it sounds interesting … i might try it later on. But thanks!
Oh, crud. Did I misread you again? You were asking to see the original poster’s code weren’t you?
Sigh… <Forehead smack!>
Hi. My (rather grumpy) response to Sonic has the code and links you’ll need to piece together to stop your music/sounds on the suspend event.
If you need to pause, use the audio.pause() function instead.
Now on to trying to actually help someone…