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

using WWW to play audio

Discussion in 'Scripting' started by Eric_Lin, Jan 9, 2015.

  1. Eric_Lin

    Eric_Lin

    Joined:
    Oct 29, 2014
    Posts:
    32
    Hi,
    Recently, I've been trying to load the music file(.mp3 or .ogg) selected by user to play. With an android plugin, I can get the file path. Here comes my question, how can I use www with file path to locate audio file and attach it to a audio source to play? My code is described below.

    public string url = "file://";
    public AudioSource song;
    // filepath is something like /mnt/sdcard/123.mp3
    void MyFunction()
    {
    url = url + filepath;
    WWW www = new WWW(url);
    song.clip = www.audioClip;
    song.Play();
    }


    If someone has the answer, please give me some help with this. Thanks.
     
  2. McRonald

    McRonald

    Joined:
    Jan 5, 2015
    Posts:
    19
    Try this:

    It allows you to stream music from disc. See also the WWW.GetAudioClip unity reference.
    Code (CSharp):
    1.  
    2.         void Update()
    3.         {
    4.             if (!audioSrc.audio.isPlaying && audioSrc.clip.isReadyToPlay)
    5.             {
    6.                 WWW www = new WWW(url);  // start a download of the given URL
    7.                 audioSrc.audio.clip = www.GetAudioClip(false, true); // 2D, streaming
    8.                 audioSrc.audio.Play();
    9.             } else
    10.             {
    11.                 Debug.Log("waiting...");
    12.             }
    13.         }
    14.  
    Use an attribuut AudioSource audioSrc, this would be your AudioSource object or component.
     
    Last edited: Jan 9, 2015
    Eric_Lin likes this.
  3. Eric_Lin

    Eric_Lin

    Joined:
    Oct 29, 2014
    Posts:
    32
    Wow, that works. You are awesome!! Thanks for helping me out.
     
    McRonald likes this.
  4. Eric_Lin

    Eric_Lin

    Joined:
    Oct 29, 2014
    Posts:
    32
    There is one more thing, is there any solution for me to jump to the certain point of music to play. For example, with a input time I would make audio play at the input time. I tried audio.PlayScheduled()....... but it seems not helpful.
     
  5. McRonald

    McRonald

    Joined:
    Jan 5, 2015
    Posts:
    19
    As described in the Unity3D AudioSource Reference, function PlaySheduled does this:
    My guess is that you don't use AudioSettings.dspTime, but just want to do some playback. So, what you do need, is the variable time of the AudioSource component, see AudioSource.time here. And just set it like this:
    Code (CSharp):
    1. audioSrc.time = 10 // 10 seconds (allows double, no float)
    in which audioSrc is your AudioSource component.
     
    Eric_Lin likes this.
  6. Eric_Lin

    Eric_Lin

    Joined:
    Oct 29, 2014
    Posts:
    32
    Thanks for replying. I did made it work. I set "audio.time" to my specific time and that worked just fine!!!
     
    McRonald likes this.