コード例 #1
0
        private async System.Threading.Tasks.Task <bool> RecordTrack(string wavFile, string deviceID, int startSector, int endSector)
        {
            try
            {
                bool result = false;
                if (string.IsNullOrEmpty(deviceID))
                {
                    LogMessage("Empty deviceID");
                    return(result);
                }

                // Stop the current stream
                mediaPlayer.Source = null;
                mediaPlayerElement.PosterSource = null;
                mediaPlayer.AutoPlay            = true;


                CDTrackStream s = await CDTrackStream.Create(deviceID, startSector, endSector);

                if (s != null)
                {
                    Windows.Storage.StorageFile file = await GetFileFromLocalPathUrl(wavFile);

                    if (file != null)
                    {
                        LogMessage("Writing into : " + file.Path);

                        Stream fs = await file.OpenStreamForWriteAsync();

                        if (fs != null)
                        {
                            const int bufferSize   = 2352 * 20 * 16;
                            const int WAVHeaderLen = 44;
                            ulong     FileSize     = s.MaxSize;
                            byte[]    array        = new byte[bufferSize];
                            ulong     currentSize  = WAVHeaderLen;
                            for (ulong i = 0; i < FileSize + bufferSize; i += currentSize)
                            {
                                if (i == WAVHeaderLen)
                                {
                                    currentSize = bufferSize;
                                }
                                var b = await s.ReadAsync(array.AsBuffer(), (uint)currentSize, InputStreamOptions.None);

                                if (b != null)
                                {
                                    fs.Write(array, 0, (int)b.Length);
                                    LogMessage("Writing : " + b.Length.ToString() + " bytes " + ((((ulong)fs.Position + 1) * 100) / FileSize).ToString() + "% copied ");
                                    if (currentSize != b.Length)
                                    {
                                        break;
                                    }
                                }
                            }
                            fs.Flush();
                            return(true);
                        }
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                LogMessage("Exception Recording Track into WAV File : " + ex.Message.ToString());
            }
            return(false);
        }
コード例 #2
0
        private async System.Threading.Tasks.Task <bool> StartPlay(string deviceID, int startSector, int endSector)
        {
            try
            {
                bool result = false;
                if (string.IsNullOrEmpty(deviceID))
                {
                    LogMessage("Empty deviceID");
                    return(result);
                }

                // Stop the current stream
                mediaPlayer.Source = null;
                mediaPlayerElement.PosterSource = null;
                mediaPlayer.AutoPlay            = true;

                if (result == true)
                {
                    mediaPlayerElement.Visibility = Visibility.Collapsed;
                }
                else
                {
                    mediaPlayerElement.Visibility = Visibility.Visible;
                }

                string contentType = "audio/wav";
                mediaPlayer.Source = Windows.Media.Core.MediaSource.CreateFromStream(await CDTrackStream.Create(deviceID, startSector, endSector), contentType);
                mediaPlayer.Play();

                return(true);
            }
            catch (Exception ex)
            {
                LogMessage("Exception Playing: " + ex.Message.ToString());
            }
            return(false);
        }