Button pause music player

Asked

Viewed 161 times

1

I’m a programming intern and I’ve decided to do something I’ve never tried before, a music player. I am building the functions that each button should do but I noticed that I made a mistake in the Pause part by actually making the button stop the music, I wanted a lot of help to discover my mistake.

//===================================================================================
//Começa a reproduzir a música escolhida
private void Iniciar_Click(object sender, RoutedEventArgs e)
{
    //Código que reproduz uma música escolhida
    System.Media.SoundPlayer player = new System.Media.SoundPlayer();
    player.SoundLocation = @"C:\Users\Juan\Desktop\Biblioteca do Juan\Praticando WPF\musicPlayer\musicPlayer\Vessel.WAV";
    player.Play();

    //esconder botão iniciar
    Iniciar.Visibility = Visibility.Collapsed;
    Pausar.Visibility = Visibility.Visible;

}

//===================================================================================
//Para a música que está sendo reproduzida
private void Pausar_Click(object sender, RoutedEventArgs e)
{
    //Código que para a música que está rodando
    System.Media.SoundPlayer player = new System.Media.SoundPlayer();
    player.Stop();

    //esconder butão pause
    Iniciar.Visibility = Visibility.Visible;
    Pausar.Visibility = Visibility.Collapsed;
}

1 answer

1


Don’t use the class System.Media.SoundPlayer this is a very limited functionality and does not have the method Pause that you need.

Use the class System.Windows.Controls.MediaElement who possesses the methods Play, Pause and Stop.

To select the file to play you change the property Source.

Edited:

The class MediaElement only plays media if it has representation of the XAML tree.

Then in Toolbox select a media element:

inserir a descrição da imagem aqui

In the XAML on the line MediaElement add the property Name="MediaElement1":

inserir a descrição da imagem aqui

private void Iniciar_Click(object sender, RoutedEventArgs e)
{
        var openFileDialog1 = new OpenFileDialog();

        if (openFileDialog1.ShowDialog() == true)
        {
            //Essa linha pode ser colocada na inicialização. 
            //Está quai para mostrar sua necessidade.
            MediaElement1.LoadedBehavior = MediaState.Manual;

            MediaElement1.Stop();


            // Nescessário converter de string para uri
            MediaElement1.Source = new Uri(openFileDialog1.FileName);

            MediaElement1.Play();

            //mostra botão iniciar
            Iniciar.Visibility = Visibility.Visible;
            Pausar.Visibility = Visibility.Collapsed;
        }

}


private void Pausar_Click(object sender, RoutedEventArgs e)
{

    MediaElement1.Stop();

    //esconder butão pause
    Iniciar.Visibility = Visibility.Visible;
    Pausar.Visibility = Visibility.Collapsed;
}
  • I tried to use this code but when I got to the part of "if(openFileDialog1.Showdialog() == Dialogresult.OK" he accused that ". OK" was incorrect, as was "media.Source = openFileDialog1.Filename;" it accuses a code error after the "="

  • If your program is console using System.Windows.Forms; If wpf put using Microsoft.Win32;

  • Opa Augusto, I’m using WPF msm but already used Win32, when I debug the application I get two errors 1- over the ". OK" ( read below ) 'bool? 'does not contain a definition for 'OK' and no accessible extension method 'OK' accepts a first argument of type 'bool'? 'can be found (you are missing a directive using or a reference from Assembly?) 2- on openFileDialog1,Filename; (below tbm) Cannot implicity Convert type 'string' to 'System.Uri'

  • Barter if (openFileDialog1.ShowDialog() == DialogResult.OK) for if (openFileDialog1.ShowDialog())

  • I still have 2 errors, I think instead of if (openFileDialog1.Showdialog() == Dialogresult.OK) or if (openFileDialog1.Showdialog().... i should put if(openFileDialog1.Showdialog() == true) and on the line below a media.Open(new Uri(openFileDialog1.Filename));

  • although it would still be wrong because of "media. Open"

  • I made an edition the code runs 100%.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.