Пример #1
0
        public async Task <bool> LoadURLAsync(Mediafile mediafile, string uri)
        {
            if (string.IsNullOrEmpty(uri))
            {
                return(false);
            }
            return(await LoadMusicAsync(() =>
            {
                int metaSize = 1024 * 200; //1MB
                MemoryStream metadataStream = new MemoryStream();
                BinaryWriter bw = new BinaryWriter(metadataStream);
                bool done = false;
                CurrentlyPlayingFile = mediafile;
                _handle = Bass.CreateStream(uri, 0, BassFlags.Default | BassFlags.Float | BassFlags.AutoFree, new DownloadProcedure((buffer, length, user) =>
                {
                    if (done)
                    {
                        return; //we are done here.
                    }
                    if (metadataStream.Length <= metaSize)
                    {
                        unsafe
                        {
                            // simply cast the given IntPtr to a native pointer to short values
                            // assuming you receive 16-bit sample data here
                            short *data = (short *)buffer;
                            for (int a = 0; a < length / 2; a++)
                            {
                                // write the received sample data to a local file
                                bw.Write(data[a]);
                            }
                        }
                    }
                    else
                    {
                        //there are HTTP Headers in the start of the recieved stream.
                        //we need to skip those so that we can parse ID3 tags.
                        //HTTP HEADER Removal START
                        var array = metadataStream.ToArray();
                        string str = Encoding.UTF8.GetString(array);
                        int l = str.IndexOf("ID3");
                        string headers = str.Substring(0, l);
                        byte[] id3TagArray = new byte[array.Length - l];
                        Buffer.BlockCopy(array, l, id3TagArray, 0, id3TagArray.Length);
                        //HTTP HEADER Removal END

                        var h = StringToHttpHeaders(headers);
                        ID3TagParser.WriteTagsToMediafile(mediafile, array, Length);
                        CurrentlyPlayingFile = mediafile;
                        MediaChanged?.Invoke(this, new EventArgs());

                        done = true;
                        metadataStream.Dispose();
                        bw.Dispose();
                    }
                }));
            }));
        }
Пример #2
0
 public async Task <bool> LoadStreamAsync(Mediafile mediafile, byte[] array)
 {
     if (array?.Length <= 0)
     {
         return(false);
     }
     return(await LoadMusicAsync(() =>
     {
         _handle = Bass.CreateStream(array, 0, array.Length, BassFlags.Float);
         if (mediafile.MediaLocation != MediaLocationType.Local)
         {
             ID3TagParser.WriteTagsToMediafile(mediafile, array, Length);
             CurrentlyPlayingFile = mediafile;
             MediaChanged?.Invoke(this, new EventArgs());
         }
     }));
 }