示例#1
0
        public static AudioClip CreateAudioClipFromStream(string streamFileName, int bankIndex)
        {
            AudioStream audio_stream = null;

            System.DateTime startTime = System.DateTime.Now;

            int    streams_bank_index = bankIndex;
            string streams_file_name  = streamFileName;
            string key = streams_file_name + "." + streams_bank_index;


            if ((s_gtaAudioFiles != null) && (streams_bank_index >= 0))
            {
                try
                {
                    Stream stream = s_gtaAudioFiles.OpenStreamsAudioStreamByName(streams_file_name, (uint)streams_bank_index);
                    if (stream != null)
                    {
                        audio_stream = new AudioStream(stream, key, true);
                    }
                }
                catch (System.Exception e)
                {
                    Debug.LogError(e);
                }
            }


            if (audio_stream != null)
            {
                System.TimeSpan time_span = System.DateTime.Now - startTime;
                Debug.Log("\"" + key + "\" took " + time_span.TotalSeconds + " seconds.");

                return(audio_stream.AudioClip);
            }

            return(null);
        }
        /// <summary>
        /// Play streams audio coroutine
        /// </summary>
        /// <returns>Nothing</returns>
        private IEnumerator PlayStreamsAudioCoroutine()
        {
            DateTime  time = DateTime.Now;
            int       streams_bank_index = StreamsBankIndex;
            string    streams_file_name  = StreamsFileName;
            string    key  = streams_file_name + "." + streams_bank_index;
            AudioClip clip = null;

            if (streamsAudioClips.ContainsKey(key))
            {
                clip = streamsAudioClips[key];
            }
            else
            {
                if ((gtaAudioFiles != null) && (streams_bank_index >= 0))
                {
                    bool   success = false;
                    string path    = Path.Combine(Application.temporaryCachePath, key + ".ogg");
                    try
                    {
                        using (Stream audio_stream = gtaAudioFiles.OpenStreamsAudioStreamByName(streams_file_name, (uint)streams_bank_index))
                        {
                            if (audio_stream != null)
                            {
                                if (File.Exists(path))
                                {
                                    File.Delete(path);
                                }
                                using (FileStream file_stream = File.Open(path, FileMode.Create))
                                {
                                    byte[] buffer = new byte[2048];
                                    int    len;
                                    while ((len = Mathf.Min((int)(audio_stream.Length - audio_stream.Position), buffer.Length)) > 0)
                                    {
                                        if (audio_stream.Read(buffer, 0, len) == len)
                                        {
                                            file_stream.Write(buffer, 0, len);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    success = true;
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e);
                    }
                    if (success)
                    {
                        WWW www = new WWW((new Uri(path)).AbsoluteUri);
                        while (!(www.isDone))
                        {
                            yield return(null);
                        }
                        clip = www.GetAudioClip(false, false, AudioType.OGGVORBIS);
                        if (clip != null)
                        {
                            clip.name = key;
                            streamsAudioClips.Add(key, clip);
                        }
                    }
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                }
            }
            if ((audioSource != null) && (clip != null))
            {
                audioSource.time = 0.0f;
                audioSource.clip = clip;
                audioSource.Play();
                StatusText = "Playing \"" + clip.name + "\"";
            }
            if (clip != null)
            {
                TimeSpan time_span = DateTime.Now - time;
                Debug.Log("\"" + clip.name + "\" took " + time_span.TotalSeconds + " seconds.");
            }
        }