Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Load And Play Audio From External Source

Discussion in 'Getting Started' started by Bill_Martini, Oct 28, 2016.

  1. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    I'm trying to load an audio clip from a folder in the same directory of my game. I can access the files using directoryInfo and have a list of file names. Using WWW I get a clip with the correct name but is empty. Resources only works as part of the project. Is there an equivalent of Resources for an external directory?

    Can someone please explain the proper way. I've done a pretty good job of web searches, but haven't seen anything I can get to work. I guess I could delete all, then copy the files into the Resource folder each time, but that seems like a great big hack to me and drive space waster too.
     
  2. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Ok, I think I have this working as best as it can get but I'm not too happy about it.

    My WWW attempt was correct but didn't give time to actually load the file, which is why I was getting a valid clip file with a correct name but was empty. Moving to a coroutine solved that. The other issue I was having was trying to load mp3's. Took me a while to get that, maybe because I didn't want that version of reality. I understand that this is a licensing issue and can see why Unity wishes to stay away from any copyright storms, but mp3's are ubiquitous and this seems to hurt the developer. What I wanted to do was to provide a way for users to use their own music as the game background music. An external folder called music and any audio files could be add and could be chosen within the game, including looping, next tune, and shuffle.

    I converted a few files to wav and have everything working now, but the lack of mp3 support makes this feature not too desirable. I've downloaded a library that is supposed to convert mp3 to wav to bypass this limitation, but haven't played with it yet. This is something I'll have to revisit down the road. I need to finish the game or it doesn't matter.
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,026
    If you're feeling lazy there is a solution on the Asset Store for $20.

    https://www.assetstore.unity3d.com/en/#!/content/42677

    Only for the relative few that feel the need to implement their own external music playback system. Most players will simply set up Steam or whichever platform they are on and hit play when they start the game. Unfortunately there doesn't seem to be any APIs available for controlling when music starts and stops.

    http://www.howtogeek.com/236316/how...rary-to-steam-and-use-the-steam-music-player/
     
    Last edited: Oct 29, 2016
  4. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Hey, thanks. Didn't see that in the asset store. I downloaded Naudio (it's free) but I'm waiting to try this until I finish the game.

    Free use sound effects are fairly easy to find although it is a time sponge and can easily take many hours, sometimes days searching for what you want. Then a lot of it needs tinkering with in Audacity. And thats the good part of it! Finding background music that fits your game style and play is painstaking. That's why I thought I cheat by suppling a cheesy short loop and allow the user to supply their own by adding to a Music folder. I've developed an aversion to audio assets due to the inordinate amount of time necessary to find things I'm looking for. Most times I settle for something just to end the torture. But maybe it's just me...