Beispiel #1
0
 public void LoadFile(string targetFileIN)
 {
     targetFile = targetFileIN;
     if (!_loadedTarget || UAudio.targetFile != targetFile)
     {
         _loadedTarget = true;
         UAudio.LoadFile(targetFileIN);
     }
 }
Beispiel #2
0
        public void Play(TimeSpan?startOff = null)
        {
#if uAudio_debug
            UnityEngine.Debug.LogWarning("&c&");
#endif
            if (State != uAudio_backend.PlayBackState.Playing)
            {
#if uAudio_debug
                UnityEngine.Debug.LogWarning("&d&");
#endif
                if (State == uAudio_backend.PlayBackState.Paused)
                {
                    Pause();
                }
                else
                {
                    State = uAudio_backend.PlayBackState.Playing;

                    try
                    {
                        //LoadFile(targetFile);
#if uAudio_debug
                        UnityEngine.Debug.LogWarning("B");
#endif
                        SongDone          = false;
                        flare_SongEnd     = false;
                        UAudio.targetFile = targetFile;

                        if (myAudioSource.clip == null)
                        {
#if uAudio_debug
                            UnityEngine.Debug.LogWarning("%B1%");
#endif
                            if (UAudio.LoadMainOutputStream())
                            {
#if uAudio_debug
                                UnityEngine.Debug.LogWarning("%B2%");
#endif

#if !UNITY_STANDALONE_WIN && blank
                                //WWW w = new WWW(targetFile);

                                //int song_sampleSize;
                                //SongReadLoop = new AudioClip.PCMReaderCallback(Song_Stream_Loop);
#if uAudio_debug
                                UnityEngine.Debug.LogWarning("%B4%");
#endif
                                //song_sampleSize = (int)UAudio.uwa.audioPlayback.TotalSamples;
#if uAudio_debug
                                UnityEngine.Debug.LogWarning("%B5%");
#endif
                                if (!targetFile.StartsWith("file://"))
                                {
                                    targetFile = "file://" + targetFile;
                                }

                                var www = new UnityEngine.WWW(targetFile);

                                UnityEngine.AudioClip myAudioClip = www.GetAudioClip(true, false, UnityEngine.AudioType.MPEG);                                // UnityEngine.AudioType.MPEG);

                                //  yield return www;
                                // Next line hangs
                                var clip = myAudioClip;
                                int i    = 100;

                                // todo : check this -=---> clip.loadState != UnityEngine.AudioDataLoadState.Loaded
#if !UNITY_2_6 && !UNITY_2_6_1 && !UNITY_3_0 && !UNITY_3_0_0 && !UNITY_3_1 && !UNITY_3_2 && !UNITY_3_3 && !UNITY_3_4 && !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2 && !UNITY_4_3 && !UNITY_4_5 && !UNITY_4_6 && !UNITY_4_7
                                while (clip.loadState != UnityEngine.AudioDataLoadState.Loaded && i > 0)
#endif
                                {
                                    i--;
                                    System.Threading.Thread.Sleep(10);
                                }
                                myAudioSource.clip = clip;
#else
#if !UNITY_STANDALONE_WIN && !UNITY_EDITOR
                                if (!targetFile.StartsWith("file://"))
                                {
                                    targetFile = "file://" + targetFile;
                                }
#endif


                                long song_sampleSize;
#if uAudio_debug
                                UnityEngine.Debug.LogWarning("%B3%");
#endif
                                AudioClip.PCMReaderCallback SongReadLoop;
                                SongReadLoop = new AudioClip.PCMReaderCallback(Song_Stream_Loop);

                                System.IO.Stream s = System.IO.File.OpenRead(targetFile);

                                if (readFullyStream != null)
                                {
                                    readFullyStream.Dispose();
                                }

                                readFullyStream = new uAudioDemo.Mp3StreamingDemo.ReadFullyStream(s);
                                readFullyStream.stream_CanSeek = true;

                                byte[]          buff = new byte[1024];
                                NLayer.MpegFile c    = new NLayer.MpegFile(readFullyStream, true);
                                //playbackDevice = m;

                                if (startOff == TimeSpan.Zero)
                                {
                                    c.ReadSamples(buff, 0, buff.Length);
                                }

                                playbackDevice = c;
                                //   UAudio.TotalTime
                                //     song_sampleSize = playbackDevice.Length;// * playbackDevice.SampleRate;
                                song_sampleSize = UAudio.SongLength;
                                // hack
                                // song_sampleSize = int.MaxValue;// need to better alocate this

                                int setSongSize;
                                if (song_sampleSize > int.MaxValue)
                                {
                                    UnityEngine.Debug.LogWarning("uAudioPlayer - Song size over size on int #4sgh54h45h45");
                                    setSongSize = int.MaxValue;
                                }
                                else
                                {
                                    setSongSize = (int)song_sampleSize;
                                }

                                myAudioSource.clip =
                                    UnityEngine.AudioClip.Create("uAudio_song", setSongSize,
                                                                 c.WaveFormat.Channels,
                                                                 c.WaveFormat.SampleRate,
                                                                 true, SongReadLoop);

                                if (!startOff.HasValue)
                                {
                                    CurrentTime = TimeSpan.Zero;
                                }
                                else
                                {
                                    CurrentTime = startOff.Value;
                                }
#endif


                                try
                                {
                                    if (sendPlaybackState != null)
                                    {
                                        sendPlaybackState(uAudio_backend.PlayBackState.Playing);
                                    }
                                }
                                catch
                                {
                                    UnityEngine.Debug.LogWarning("theAudioStream_sendStartLoopPump #32fw46hw465h45h");
                                }
#if uAudio_debug
                                UnityEngine.Debug.LogWarning("%B6%");
#endif
                            }
                            else
                            {
                                myAudioSource.clip = null;
                            }
                        }
                        else
                        {
#if uAudio_debug
                            UnityEngine.Debug.LogWarning("%B!%");
#endif
                        }

                        if (myAudioSource.clip != null)
                        {
#if uAudio_debug
                            UnityEngine.Debug.LogWarning("%B7%");
#endif
                            if (!myAudioSource.isPlaying)
                            {
                                myAudioSource.Play();
                            }

#if uAudio_debug
                            UnityEngine.Debug.LogWarning("%B8%");
#endif
                            updateTime = true;
                        }
                        else
                        {
                            State = uAudio_backend.PlayBackState.Stopped;
                        }
                    }
                    catch (System.Exception ex)
                    {
#if uAudio_debug
                        UnityEngine.Debug.LogWarning("%B9%" + System.Environment.NewLine + ex.Message);
#endif
                        State = uAudio_backend.PlayBackState.Stopped;
                        UnityEngine.Debug.LogWarning("uAudioPlayer - Play #j356j536j356j56j");
                        UnityEngine.Debug.LogWarning(ex);
                    }
                }
            }
        }