Scripting Audio in the Background

0 favourites
  • 5 posts
From the Asset Store
Here is the wonderful & amazing script for your website or fun with maths.
  • I have a line of script code which plays an audio stream

    let audio = new Audio(runtime.globalVars.AudioURL);

    runtime.globalVars.Player = audio;

    runtime.globalVars.Player.play();

    Now understandably this is not idea as it plays the audio without Construct objects being able to access it.

    If this code is ran on a mobile phone there is issues with background audio as the object hasnt been defined and I can therefore not tick the Play in background check box.

    Does anyone know if by adding the audio object, this will allow the entire app to stay alive in the background and all the scripted audio to continue to play?

    I cant reference the audio stream in the audio object as it is an mpeg file stream

    Ashley if you have another way to play an mpeg stream please let me know?

  • runtime.globalVars.Player = audio;

    This code is wrong - Construct's global variables can only be strings, numbers or booleans. You can't assign a complex JavaScript object like Audio to a Construct event sheet global variable (how would that work in the event sheet?). You will need to assign it somewhere else.

    Does anyone know if by adding the audio object, this will allow the entire app to stay alive in the background and all the scripted audio to continue to play?

    Adding the Audio object doesn't change the way browser permissions for audio playback work. If it's not working, it is most likely due to the mistake above.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hi Ashley, surprisingly it does allow me to add the audio to the instance and it then works perfectly all through the project and can be called without any problem.

    I realise this is not the ideal or correct way to do it.

    However the current audio object does not play mpeg audio streams as Is required for a radio station stream.

    Is there any way to get the audio object to stream mpeg audio rather than loading it?

    If there is then I can just click the check box to play audio in the background.

    Otherwise does the play audio in the background check box only work for the specific audio file or for the entire project?

  • The code you showed is definitely wrong, so if something doesn't work, the first thing you should do is fix that code. I can't help any further if your code is obviously wrong, as my only assumption will be that wrong code is going to break things.

    The 'Play in background' setting in Construct's Audio object only affects audio played with the Audio object, and it only means "do not deliberately stop all audio playback when the page goes in to the background". So the default is to continue playing audio in the background, providing the browser allows it.

  • Hi Ashley

    Thank you for your reply.

    Ok for arguments sake I have created a new project and on the event sheet I've added one script as follows

    System > On start of layout >

    let audio = new Audio("https://streaming.galaxywebsolutions.com/stream/moorland");

    audio.play();

    When you run the project it starts and plays the audio from the script.

    However as it is ran direct from the script I can not tick the play in background on the audio object as the audio object as not been added to the project, therefore all works perfectly apart from the background issue when the app sleeps or loses focus.

    I can not call (or at least I don't know how to call) the stream from the audio object as it is continuous and a live mpeg stream which can not be changed to a different format.

    How would I get my project to steam this URL via the audio object so that I can then check the play in background box, so that the stream continues to play in background mode on both Android & iPhone?

    Many thanks

    Adrian

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)