示例#1
0
        private async void DownLoadButton_Click(object sender, RoutedEventArgs e)
        {
            Uri uri = new Uri(MyBox.Text);

            MyMediaPlayer.Source = uri;
            string FileName = System.IO.Path.GetFileName(uri.LocalPath);


            var httpClient   = new HttpClient();
            var httpResponse = new Windows.Web.Http.HttpResponseMessage();
            var file         = await KnownFolders.MusicLibrary.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);

            string httpResponseBody = "";

            try
            {
                httpResponse = await httpClient.GetAsync(uri);

                httpResponse.EnsureSuccessStatusCode();
                httpResponseBody = await httpResponse.Content.ReadAsStringAsync();

                var sourceStream = await httpResponse.Content.ReadAsInputStreamAsync();

                using (var destinationStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    using (var destinationOutputStream = destinationStream.GetOutputStreamAt(0))
                    {
                        await RandomAccessStream.CopyAndCloseAsync(sourceStream, destinationStream);
                    }
                }
                var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

                MyMediaPlayer.SetSource(stream, file.ContentType);
                MyMediaPlayer.Play();
            }
            catch { }
        }
 protected override void OnNavigatedTo(NavigationEventArgs e)
 {
     base.OnNavigatedTo(e);
     MyMediaPlayer.Play();
 }