UNPKG

jsge

Version:

Javascript Game Engine

105 lines (91 loc) 5.66 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="expires" content="Wen, 05 Jun 2024 07:01:00 GMT"> <title>JSDoc: Tutorial: How to add and use audio</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="stage-title">Tutorial: How to add and use audio</h1> <section> <header> <h2>How to add and use audio</h2> </header> <article> <p>To add a file, use <code>stage.iLoader.addAudio()</code> in the <a href="tutorial-stages_lifecycle.html">GameStage.register()</a>:</p> <pre class="prettyprint source"><code>register() { this.iLoader.addAudio(&quot;audio_key&quot;, &quot;./audio.mp3&quot;); ... } </code></pre> <h2>Using AudioInterface:</h2> <p>The AudioInterface serves as a control center for all audio tracks.<br> The default AudioInterface is available via <code>stage.audio</code>.<br> You can add any other audio interfaces with:</p> <pre class="prettyprint source"><code>this.music = new AudioInterface(this.iLoader); </code></pre> <h3>How to use tracks with AudioInterface</h3> <ol> <li>First, register the added and loaded audio file in <a href="tutorial-stages_lifecycle.html">the GameStage.init(), or GameStage.start()</a>:</li> </ol> <pre class="prettyprint source"><code>stage.audio.registerAudio(&quot;audio_key&quot;); </code></pre> <ol start="2"> <li>Then get the audio track:</li> </ol> <pre class="prettyprint source"><code>const track = stage.audio.getAudio(&quot;audio_key&quot;); </code></pre> <ol start="3"> <li>To play or pause the track, use:</li> </ol> <pre class="prettyprint source"><code>track.play(); track.pause(); </code></pre> <ol start="4"> <li>To loop the audio track, set the <code>track.loop</code> parameter to <code>true</code>:</li> </ol> <pre class="prettyprint source"><code>const audioTrack = this.audio.getAudio(&quot;audio_key&quot;); audioTrack.loop = true; audioTrack.play(); </code></pre> <ol start="5"> <li>If you want to take a copy of the track, use <code>getAudioCloned()</code>:</li> </ol> <pre class="prettyprint source"><code>this.audio.getAudioCloned(&quot;audio_key&quot;).play(); </code></pre> <ol start="6"> <li>In order to control the volume,<br> set <code>audio.volume</code> from 0 to 1(this will affect only on registered tracks):</li> </ol> <pre class="prettyprint source"><code>this.audio.volume = 0.5; </code></pre> <h2>Live Example</h2> <p class="codepen" data-height="500" data-default-tab="js,result" data-slug-hash="WNPBpgz" data-user="yaalfred" style="height: 500px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;"> <span>See the Pen <a href="https://codepen.io/yaalfred/pen/WNPBpgz"> JsGE - audio</a> by Arturas-Alfredas Lapinskas (<a href="https://codepen.io/yaalfred">@yaalfred</a>) on <a href="https://codepen.io">CodePen</a>.</span> </p> <script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script> </article> </section> </div> <nav> <div class="switcher"><ul><li class="active"><a href="/">1.5.9</a></li></div> <h2><a href="/">Home</a></h2><h3>Classes</h3><ul><li><a href="DrawCircleObject.html">DrawCircleObject</a></li><li><a href="DrawConusObject.html">DrawConusObject</a></li><li><a href="DrawImageObject.html">DrawImageObject</a></li><li><a href="DrawLineObject.html">DrawLineObject</a></li><li><a href="DrawObjectFactory.html">DrawObjectFactory</a></li><li><a href="DrawPolygonObject.html">DrawPolygonObject</a></li><li><a href="DrawRectObject.html">DrawRectObject</a></li><li><a href="DrawShapeObject.html">DrawShapeObject</a></li><li><a href="DrawTextObject.html">DrawTextObject</a></li><li><a href="DrawTiledLayer.html">DrawTiledLayer</a></li><li><a href="GameStage.html">GameStage</a></li><li><a href="GameStageData.html">GameStageData</a></li><li><a href="IExtension.html">IExtension</a></li><li><a href="INetwork.html">INetwork</a></li><li><a href="IRender.html">IRender</a></li><li><a href="ISystem.html">ISystem</a></li><li><a href="ISystemAudio.html">ISystemAudio</a></li><li><a href="RenderLoop.html">RenderLoop</a></li><li><a href="RenderLoopDebug.html">RenderLoopDebug</a></li><li><a href="System.html">System</a></li><li><a href="SystemSettings.html">SystemSettings</a></li></ul><h3>Tutorials</h3><ul><li><a href="tutorial-application_scheme.html">Application Scheme</a></li><li><a href="tutorial-assets_manager.html">Assets Manager</a></li><li><a href="tutorial-common_issues.html">Common Issues</a></li><li><a href="tutorial-how_to_add_and_use_audio.html">How to add and use audio</a></li><li><a href="tutorial-how_to_do_animations.html">How to Create Animations</a></li><li><a href="tutorial-how_to_load_and_use_tilemaps.html">How to Load and Use Tilemaps</a></li><li><a href="tutorial-quick_start.html">Quick Start</a></li><li><a href="tutorial-spine_animations.html">How to Add Spine Animations</a></li><li><a href="tutorial-stages_lifecycle.html">Stages Lifecycle</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Wed Jun 18 2025 06:53:54 GMT+0000 (Coordinated Universal Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>