Is there a way i can import audio files in play mode

Yes, you can do this, and you don’t need C++ (just blueprints).

  • In the content browser, create a “Media Player” and a “Stream Media Source”. (Leave the “stream url” of the “Stream Media Source” empty.)
  • Create an actor blueprint and open it.
  • Create a variable of type “Stream Media Source”. In the “Details” panel on the right, select the default value for “Audio Media Source” as the asset you created in step 1.
  • In the “Components” pane of the blueprint (upper left corner), use “Add Component”, “Media Sound”.
  • Select the newly created media sound, and in the “Details” panel on the right, under “Media”, select your Media Player asset that you created in step 1.
  • In the “My Blueprint” pane, under Variables/Components, drag the media sound into the event graph and chose “get”.
  • From this node, drag and search for “Get Media Player”.
  • Create a variable of type Media Player. Drag it into the event graph and select “set”.
  • From the “BeginPlay” event, set your Media Player variable from the “Get Media Player” node
  • When you’re ready, get the input filepath to the audio file by whatever means you choose.
  • Use the Utilities/String/Replace node to replace all backslashes with forward slashes in the filepath.
  • Prepend the filepath with “file://” using the String/Append node.
  • Drag the Stream Media Source variable into the graph with “get”, and drag off of that and search for “set stream url”. Set the url with the string that now begins with “file://”.
  • Optionally, use the Media/MediaPlayer “Can Play Source” node (along with the Media Player variable and the Media Source variables) to validate the audio file.
  • When you are ready to play the audio, use the Media/MediaPlayer “Open Source” node (along with the Media Player variable and the Media Source variables) to begin playing the audio.

One major disadvantage I’ve run into is that the Media Player “Get Time” node is sometimes inaccurate. (Unlike AudioComponent’s OnAudioPlaybackPercent, which is always accurate).

1 Like