Exemplo n.º 1
0
        public IntPtr LoadAudioStreamFromUrl(string url, string cacheDirectory)
        {
            lock (audioUrlStreamLoaderLockObject) {
                lock (audioDataCache) {
                    if (audioDataCache.ContainsKey(url))
                    {
                        return(audioDataCache[url].AudioStreamPointer);
                    }
                }
                AudioDataCache dataCache = new AudioDataCache();

                dataCache.CachePath   = cacheDirectory + Path.GetFileName(url);
                dataCache.CacheStream = new FileStream(dataCache.CachePath + ".tmp", FileMode.Create, FileAccess.Write);

                dataCache.CallbackDelegate = new DownloadCallbackDelegate(AudioDownloadCallback);

                IntPtr urlPtr = Marshal.StringToHGlobalUni(url);
                lock (audioDataCache) {
                    if (audioDataCache.ContainsKey(url) == false)
                    {
                        audioDataCache.Add(url, dataCache);
                    }
                    else
                    {
                        dataCache.CallbackDelegate = null;
                        if (urlPtr != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(urlPtr);
                            urlPtr = IntPtr.Zero;
                        }
                    }
                }

                dataCache.AudioStreamPointer = Bass.BASS_StreamCreateURL(url, 0, 0, dataCache.CallbackDelegate, urlPtr);

                if (dataCache.AudioStreamPointer == IntPtr.Zero)
                {
                    // Uh oh! We couldn't play the sfx stream!
                    lock (audioDataCache) {
                        if (audioDataCache.ContainsKey(url))
                        {
                            audioDataCache.RemoveAtKey(url);
                        }
                        if (urlPtr != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(urlPtr);
                            urlPtr = IntPtr.Zero;
                        }
                    }
                    dataCache.CacheStream.Close();
                    File.Delete(dataCache.CachePath + ".tmp");
                    dataCache.CachePath = null;
                }

                return(dataCache.AudioStreamPointer);
            }
        }
Exemplo n.º 2
0
 public void AudioDownloadCallback(IntPtr bufferPointer, int length, IntPtr user)
 {
     try
     {
         AudioDataCache dataCache = null;
         string         url       = Marshal.PtrToStringUni(user);
         lock (audioDataCache)
         {
             if (audioDataCache.ContainsKey(url))
             {
                 dataCache = audioDataCache[url];
             }
         }
         if (dataCache != null)
         {
             if (length > 0)
             {
                 byte[] buffer = new byte[length];
                 Marshal.Copy(bufferPointer, buffer, 0, length);
                 dataCache.CacheStream.Write(buffer, 0, length);
             }
             else if (length == 0)
             {
                 Marshal.FreeHGlobal(user);
                 dataCache.CacheStream.Close();
                 lock (audioDataCache)
                 {
                     if (audioDataCache.ContainsKey(url))
                     {
                         audioDataCache.RemoveAtKey(url);
                     }
                 }
                 if (dataCache.AudioStreamPointer != IntPtr.Zero)
                 {
                     File.Copy(dataCache.CachePath + ".tmp", dataCache.CachePath, true);
                 }
                 File.Delete(dataCache.CachePath + ".tmp");
             }
         }
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message + " @ Audio Download Callback");
     }
 }
Exemplo n.º 3
0
        public IntPtr LoadAudioStreamFromUrl(string url, string cacheDirectory)
        {
            lock (audioUrlStreamLoaderLockObject) {
                lock (audioDataCache) {
                    if (audioDataCache.ContainsKey(url)) {
                        return audioDataCache[url].AudioStreamPointer;
                    }
                }
                AudioDataCache dataCache = new AudioDataCache();

                dataCache.CachePath = cacheDirectory + Path.GetFileName(url);
                dataCache.CacheStream = new FileStream(dataCache.CachePath + ".tmp", FileMode.Create, FileAccess.Write);

                dataCache.CallbackDelegate = new DownloadCallbackDelegate(AudioDownloadCallback);

                IntPtr urlPtr = Marshal.StringToHGlobalUni(url);
                lock (audioDataCache) {
                    if (audioDataCache.ContainsKey(url) == false) {
                        audioDataCache.Add(url, dataCache);
                    } else {
                        dataCache.CallbackDelegate = null;
                        if (urlPtr != IntPtr.Zero) {
                            Marshal.FreeHGlobal(urlPtr);
                            urlPtr = IntPtr.Zero;
                        }
                    }
                }

                dataCache.AudioStreamPointer = Bass.BASS_StreamCreateURL(url, 0, 0, dataCache.CallbackDelegate, urlPtr);

                if (dataCache.AudioStreamPointer == IntPtr.Zero) {
                    // Uh oh! We couldn't play the sfx stream!
                    lock (audioDataCache) {
                        if (audioDataCache.ContainsKey(url)) {
                            audioDataCache.RemoveAtKey(url);
                        }
                        if (urlPtr != IntPtr.Zero) {
                            Marshal.FreeHGlobal(urlPtr);
                            urlPtr = IntPtr.Zero;
                        }
                    }
                    dataCache.CacheStream.Close();
                    File.Delete(dataCache.CachePath + ".tmp");
                    dataCache.CachePath = null;
                }

                return dataCache.AudioStreamPointer;
            }
        }