[SOLVED] Sound/Audio only plays when I click on the screen in HTML5 build but supposed to play automatically at start

DEFOLD EDITOR AND ENGINE BUGS

1.10.3

Link to the reported GitHub issue in your forum post to get visibility and discuss with the community and dev team.

Describe the bug (REQUIRED)
If I load it in debug mode it plays automatically but when I make a web build, it doesn’t start until I tap on the screen.

To Reproduce (REQUIRED)
Steps to reproduce the behavior:

--Audio manager script
function init(self)
	sound.play("/audio_tracks#bg_music")
	print("audio mang url ".. msg.url())
	-- self.url_audio = msg.url("urlstring") 
end

function on_message(self, message_id, message)
	if message_id ==hash("play_instance") then
		sound.play("/audio_tracks#instance")
	end
	if message_id ==hash("play_merge") then
		sound.play("/audio_tracks#merge")
		
	end
	if message_id ==hash("mute_music") then
		sound.stop("#bg_music")
		print("mute music")
	elseif message_id == hash("unmute_music") then
		sound.play("#bg_music")
		print("unmute music")
	end
	if message_id == hash("mute_sound") then
		sound.set_gain("/audio_tracks#instance", 0)
		sound.set_gain("/audio_tracks#merge", 0)
		print("mute sound")
	elseif message_id == hash("unmute_sound") then
		sound.set_gain("/audio_tracks#instance", 1)
		sound.set_gain("/audio_tracks#merge", 1)
		print("unmute sound")
	end
end
--Proxy loading
function init(self)
	msg.post("#main_proxy", "load")
	msg.post("#main_ui", "load")
	msg.post("#audio_collection", "load")
	
	msg.post(".", "acquire_input_focus")
	print("url proxy loading " .. msg.url())
end

function on_message(self, message_id, message, sender)
	if message_id == hash("proxy_loaded") then
		msg.post(sender, "enable")
	end

	if message_id == hash("pause_game") then
		print("pause")
		msg.post("/scene_loader#main_proxy", "set_time_step", {factor = 0, mode = 1})
		msg.post("UI_proxy:/GUI#main", "enable_game_over", message)
		
	end
	if message_id == hash("has_restarted") then

		msg.post("#main_proxy", "disable")
		msg.post("#main_proxy", "unload") 
	end

	if message_id == hash("proxy_unloaded") then
		msg.post("#main_proxy", "load")
		msg.post("/scene_loader#main_proxy", "set_time_step", {factor = 1, mode = 1})
		msg.post("UI_proxy:/GUI#main", "disable_lose_screen")
	end
	if message_id == hash("pause_menu") then
		print("pause")
		msg.post("/scene_loader#main_proxy", "set_time_step", {factor = 0, mode = 1})
	end
	if message_id == hash("unpause") then
		msg.post("/scene_loader#main_proxy", "set_time_step", {factor = 1, mode = 1})
		
	end
	
end
-- msg.post("/scene_loader#main_proxy", "set_time_step", {factor = 1, mode = 1})

Expected behaviour (REQUIRED)
background music play automatically.

Defold version (REQUIRED):
1.10.3

Platforms (REQUIRED):
HTML5

Screenshots (OPTIONAL):


This isn’t a bug, but how most browsers implement some features (e.g. sound playing)
I don’t have a specific documentation link for you, but google is your friend here.

1 Like

Thanks, SOLVED. “Browsers require a user gesture (like a click or key press) to initialize the audio context and allow playback. To handle this, implement a loading screen or initial interaction point that allows the user to initiate audio playback before the main content loads”

3 Likes