Exemplo n.º 1
0
    /* bad, no mp3 support at runtime apparently
     * IEnumerator LoadHelper(string uri)
     * {
     *  using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(uri, AudioType.MPEG))
     *  {
     *      yield return www.SendWebRequest();
     *
     *      if (www.isNetworkError)
     *      {
     *          statusText.text = www.error;
     *      }
     *      else
     *      {
     *          statusText.text = "loaded?";
     *          AudioClip myClip = DownloadHandlerAudioClip.GetContent(www);
     *          source.clip = myClip;
     *      }
     *  }
     * }
     */

    /* MP3Stream (LGPL so not compatible with how Unity builds)
     * IEnumerator LoadHelper(string uri)
     * {
     *  statusText.text = "Loading...";
     *  UnityWebRequest www = UnityWebRequest.Get(uri);
     *  yield return www.SendWebRequest();
     *
     *  if(www.isNetworkError || www.isHttpError)
     *  {
     *      statusText.text = www.error;
     *  }
     *  else
     *  {
     *    statusText.text = "loaded";
     *    // Or retrieve results as binary data
     *    byte[] results = www.downloadHandler.data;
     *    var memStream = new System.IO.MemoryStream(results);
     *    var mp3Stream = new MP3Sharp.MP3Stream(memStream);
     *
     *    var audioDataStream = new System.IO.MemoryStream();
     *    byte[] buffer = new byte[4096];
     *    int bytesReturned = -1;
     *
     *    while( bytesReturned != 0 )
     *    {
     *        bytesReturned = mp3Stream.Read(buffer, 0, buffer.Length);
     *        audioDataStream.Write(buffer, 0, bytesReturned);
     *    }
     *
     *    byte[] audioData = audioDataStream.ToArray();
     *
     *    float[] floatArray = new float[audioData.Length / 2];
     *    for(int i = 0; i < floatArray.Length; ++i)
     *    {
     *        floatArray[i] = (float)(System.BitConverter.ToInt16(audioData, i * 2 ) / 32768.0f);
     *    }
     *
     *    var clip = AudioClip.Create("foo", floatArray.Length, 2, mp3Stream.Frequency, false);
     *    clip.SetData(floatArray, 0);
     *    source.clip = clip;
     *  }
     * }
     */

    IEnumerator LoadHelper(string uri)
    {
        statusText.text = "Loading...";
        UnityWebRequest www = UnityWebRequest.Get(uri);

        yield return(www.SendWebRequest());

        if (www.isNetworkError || www.isHttpError)
        {
            statusText.text = www.error;
        }
        else
        {
            statusText.text = "loaded";
            // Or retrieve results as binary data
            byte[] results   = www.downloadHandler.data;
            var    memStream = new System.IO.MemoryStream(results);
            var    mpgFile   = new NLayer.MpegFile(memStream);
            var    samples   = new float[mpgFile.Length];
            mpgFile.ReadSamples(samples, 0, (int)mpgFile.Length);

            var clip = AudioClip.Create("foo", samples.Length, mpgFile.Channels, mpgFile.SampleRate, false);
            clip.SetData(samples, 0);
            source.clip = clip;
        }
    }
Exemplo n.º 2
0
 void MusicBack(float[] data)
 {
     if (mp3MpgFile.ReadSamples(data, 0, data.Length) == 0)
     {
         isPlay = false;
         Debug.Log("播放完毕");
         mp3MpgFile.Dispose();
         mp3MpgFile = null;
     }
     readLen += data.LongLength;
 }
Exemplo n.º 3
0
    void PlayMusic()
    {
        byte[] buff = File.ReadAllBytes("music/刘昊霖 - 儿时.mp3");
        text.text  = "刘昊霖 - 儿时";
        mp3MpgFile = new NLayer.MpegFile(new MemoryStream(buff));
        AudioSource audios = gameObject.GetComponent <AudioSource>();

        if (null == audios)
        {
            audios = gameObject.AddComponent <AudioSource>();
        }
        audios.clip = AudioClip.Create("clip", (int)mp3MpgFile.Length / 8, mp3MpgFile.Channels,
                                       mp3MpgFile.SampleRate, true, MusicBack);
        audios.loop   = false;
        audios.volume = 1;
        audios.Play();
        isPlay = true;
    }
Exemplo n.º 4
0
    private IEnumerator loadSongTest()
    {
        UnityWebRequest audioFile = UnityWebRequest.Get(this.objectSong);

        yield return(audioFile.SendWebRequest());

        if (audioFile.isNetworkError || audioFile.isHttpError)
        {
            Debug.Log(audioFile.error);
        }
        else
        {
            Debug.Log(this.objectSong + "was found");
            byte[] results   = audioFile.downloadHandler.data;
            var    memStream = new System.IO.MemoryStream(results);
            var    mpgFiles  = new NLayer.MpegFile(memStream);
            var    samples   = new float[mpgFiles.Length];
            mpgFiles.ReadSamples(samples, 0, (int)mpgFiles.Length);
            var clip = AudioClip.Create("convertedFile", samples.Length, mpgFiles.Channels, mpgFiles.SampleRate, false);
            clip.SetData(samples, 0);
            menuMusic.clip = clip;
        }

        /*using (UnityWebRequest audioFile = UnityWebRequestMultimedia.GetAudioClip(this.objectSong, AudioType.MPEG))
         * {
         *  yield return audioFile.SendWebRequest();
         *  if (audioFile.result == UnityWebRequest.Result.ConnectionError)
         *      Debug.Log(audioFile.error);
         *  else
         *  {
         *      byte[] results = audioFile.downloadHandler.data;
         *      var memStream = new System.IO.MemoryStream(results);
         *      var mpgFiles = new NLayer.MpegFile(memStream);
         *      var samples = new float[mpgFiles.Length];
         *      mpgFiles.ReadSamples(samples, 0, (int)mpgFiles.Length);
         *      var clip = AudioClip.Create("convertedFile", samples.Length, mpgFiles.Channels, mpgFiles.SampleRate, false);
         *      clip.SetData(samples, 0);
         *      menuMusic.clip = clip;
         *  }
         * }*/
    }
Exemplo n.º 5
0
    IEnumerator AudioImport()
    {
        Debug.Log("Starting import");
        UnityWebRequest www = UnityWebRequest.Get("D:/Temp/Test.mp3");

        yield return(www.SendWebRequest());

        Debug.Log("File obtained");
        byte[] results   = www.downloadHandler.data;
        var    memStream = new System.IO.MemoryStream(results);
        var    mpgFile   = new NLayer.MpegFile(memStream);
        var    samples   = new float[mpgFile.Length];

        mpgFile.ReadSamples(samples, 0, (int)mpgFile.Length);
        Debug.Log("Samples read");
        var clip = AudioClip.Create("foo", samples.Length, mpgFile.Channels, mpgFile.SampleRate, false);

        clip.SetData(samples, 0);
        Debug.Log("Clip created");
        GetComponent <AudioSource>().clip = clip;
        GetComponent <AudioSource>().Play();
        Debug.Log("Playing audio");
    }
Exemplo n.º 6
0
		internal SDL2MusicTrack (string path) {
			_path = path;
			_mp3 = new MpegFile(System.IO.Path.Combine("assets", path));
		}