# wx.createInnerAudioContext interface quick start

  1. Drag the audio file to the Project panel to import the audio file, and automatically generate the audio clip of the audio file audioclip.



  2. Click the just imported audio file in the Project panel and set it as the entry resource in the Inspector panel, that is, Set As Entry Resource = true. In this way, the corresponding audio clip audioclip resource can be loaded through the engine.loader.load interface.



  3. Right-click "New" -> script in the blank area of ​​the Project panel to create a script file and rename it to testAudio.



  4. Copy the following code to the newly created script file and save it.

     import engine from "engine";
     @engine.decorators.serialize("TestWxInnerAudioContext")
     export default class TestWxInnerAudioContext extends engine.Script {
       private _hasPlay = false
    
       public onAwake() {
         // Call the _load function when the script is activated
         this._load()
       }
       private async _load() {
         // Load the audioclip resource corresponding to the audio file just imported (this file must be set as the entry file)
         const audioclip = await engine.loader.load("Assets/tutorial/audioTutorial/audioDemo/audio/background.mp3").promise as engine.AudioClip
         // Create InnerAudioContext object
         const context = wx.createInnerAudioContext()
         // Set the playing url
         context.src = audioclip.fileSrc
         // listen to the event that the audio enters the playable state
         context.onCanplay(() => {
           if (!this._hasPlay) {// limit can only be played once
             this._hasPlay = true
             context.play() // play audio
             setTimeout(() => {
               context.stop() // stop playing after 10 seconds
             }, 10000)
           }
         })
       }
     }
    


  5. Open the 3D scene resource and create an empty node in the Hierachy panel. Then configure this empty node in the Inspector panel, and add the script resource just created through "+Add component" at the bottom of the Inspector panel -> Script -> TestWxInnerAudioContext. Press ctrl+s (command+s for macOS) to save the scene.



  6. Click "2d/3d presentation" at the top of the tool, set the main 3D scene, and click the "OK" button to save. Then click the "Play" button at the top of the tool to play the scene, and then the audio can be played.



点击咨询小助手