示例#1
0
        private void PlaySongButton_Click(object sender, RoutedEventArgs e)
        {
            if (_songPlaying)
            {
                PlaySongButton.Content = PlaySongText;
                MyMedia.Pause();
            }
            else
            {
                // Check if background music is playing (this is an certification app requirement)
                if (!MediaPlayer.GameHasControl)
                {
                    FrameworkDispatcher.Update();

                    var result = MessageBox.Show("Background music is  playing, are you sure you want to play this song?  It will stop the music?", "Music Playing", MessageBoxButton.OKCancel);
                    if (result == MessageBoxResult.Cancel)
                    {
                        return;
                    }
                }

                if (!_songInitialized)
                {
                    MyMedia.AutoPlay = true;
                    if (songData._playFromURI)
                    {
                        // make sure we have a valid URI
                        Uri dataUri = null;
                        try
                        {
                            dataUri = new Uri(UriTextBox.Text);
                        }
                        catch (Exception)
                        {
                            StatusTextBlock.Text = "I'm sorry something is wrong with the URI";
                            return;
                        }
                        // Turn auto play on
                        MyMedia.Source = dataUri;
                    }
                    else
                    {
                        using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            using (Stream isoStream = storage.OpenFile(songData._songFileName, FileMode.Open))
                            {
                                MyMedia.SetSource(isoStream);
                            }
                        }
                    }
                    _songInitialized = true;
                }

                PlaySongButton.Content = PauseSongText;
                MyMedia.Play();
            }
            _songPlaying = !_songPlaying;
        }
示例#2
0
        private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            StorageFile LoadFile = await Load();

            // MediaPlayer MyMediaPlayer = new MediaPlayer();
            if (LoadFile != null)
            {
                // var mediaSource = MediaSource.CreateFromStorageFile(LoadFile);
                var stream = await LoadFile.OpenAsync(Windows.Storage.FileAccessMode.Read);

                // MyMediaPlayer.Source = mediaSource;
                MyMedia.SetSource(stream, LoadFile.ContentType);
                //MyMedia.SetMediaPlayer(MyMediaPlayer);
                MyMedia.Play();
            }
        }