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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Play mp3 file from url

Discussion in 'Audio & Video' started by Tarzan111, Jun 9, 2016.

  1. Tarzan111

    Tarzan111

    Joined:
    Oct 8, 2014
    Posts:
    72
    Hi, I'm having some issue to play mp3 file from url with www.

    I have few url in a string[] table name "urlsound".
    This is working pretty weirdly. I'm having some empty error message, so i don't know where it come from. Sometimes this is working perfectly for one or 2 tracks and not working at the next track. And the sound can start after few seconds...don't know why too.

    Here is my code :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class managesound : MonoBehaviour {
    5.  
    6.     public string[] urlsound;
    7.     private int i;
    8.     public AudioSource audio;
    9.     WWW www;
    10.     public bool loading = false;
    11.  
    12.     public void startsound () { //start audio player
    13.         i=Random.Range(0,urlsound.Length); //first track is random
    14.         setup();
    15.     }
    16.     void Update () {
    17.         if(loading && audio.clip.isReadyToPlay){ //when ready to play
    18.             loading=false;
    19.              audio.Play();
    20.          }
    21.     }
    22.     public void nextsound () { //click next track
    23.         i++;
    24.         if(i>=urlsound.Length) i=0;
    25.         setup();
    26.     }
    27.     public void previoussound () { //click previous track
    28.         i--;
    29.         if(i<0) i=urlsound.Length-1;
    30.         setup();
    31.     }
    32.     void setup(){
    33.         StartCoroutine(DownloadAndPlay());
    34.     }
    35.     IEnumerator DownloadAndPlay()
    36.     {
    37.         audio.Stop(); //reset www and AudioSource
    38.         www=null;  //reset www and AudioSource
    39.         audio.clip =null;//reset www and AudioSource
    40.         www = new WWW (urlsound[i]); //load new track url
    41.         audio.clip = www.GetAudioClip(false,true);
    42.         loading=true;
    43.     }
    44. }
    45.  
     
  2. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    I'm not sure. Have you tried using www.audioClip instead of of GetAudioClip()? Does it produce the same weirdness?
     
  3. Tarzan111

    Tarzan111

    Joined:
    Oct 8, 2014
    Posts:
    72
    I just tried and it's working perfectly with www.audioClip
    But most of my sounds are 10Mo+ and with www.audioClip i have to wait the end of the download to Play the music and it can be more than 30 seconds...
     
  4. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    I'm not sure what the solution is. The GetAudioClip() method doesn't give much control to how the audio is streamed. Is it possible to specify how much data is needed before streaming? I've no idea.

    I guess there's a good reason why you are not simply bundling the audio into the game. Perhaps you can try further compressing the audio files to make them smaller? Or maybe there exists a better third party file streaming plugin?