2

The following code plays a short sound (a simple wav file) every time a button is clicked.

But after putting the computer to sleep and waking it up again - it doesn't play the sound anymore when the button is clicked (though the event handler Button_Click is being called). Why?

The code:

public partial class MainWindow : Window
{
    MediaPlayer player1;

    public MainWindow()
    {
        InitializeComponent();
        player1 = new MediaPlayer();
        player1.Open(new Uri("Some\\Path\\sound.wav"));
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        player1.Position = TimeSpan.Zero;
        player1.Play();
    }
}

I know I can Open the file again when the user logs on again. But that shouldn't be needed.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Perhaps you just need to create new instance of `MediaPlayer` after [wake up](https://stackoverflow.com/q/18206183/1997232). – Sinatr Nov 12 '20 at 14:01
  • @Sinatr That is, in fact, what I intend to do if there is no other solution. But it sounds (pun intended) strange that there won't be a simpler solution. – ispiro Nov 12 '20 at 14:06
  • Any hint from a possible MediaFailed event? – Simon Mourier Nov 24 '20 at 18:55
  • @SimonMourier MediaFailed is never called. You can try the code yourself. The code is complete. – ispiro Nov 24 '20 at 21:44
  • Does this happen on all PCs? I haven't repro'd... but not all computers support applications waking up. REF: https://stackoverflow.com/a/11487302/495455 – Jeremy Thompson Nov 25 '20 at 01:18
  • @JeremyThompson That's about an app waking the computer up. Here the user is doing that. – ispiro Nov 25 '20 at 12:48
  • @JeremyThompson I could reproduce it. Just run the code and then do hibernate. Afterwards you can do nothing with MediaPlayer object(checked with debugger), e.g. the Position will not be set to the TimeSpan.Zero. – Rekshino Nov 30 '20 at 13:14

1 Answers1

2

What I did in my application is just called player1.Stop(); when the Windows is going to suspend mode.

MainWindow.xaml

<Window ...
    Loaded="Window_Loaded">
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Button Click="Button_PlayResume" >Play/Resume</Button>
        <Button Click="Button_Pause" Grid.Row="1">Pause</Button>        
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    MediaPlayer player1;

    // Last play position before going to suspend mode
    private TimeSpan LastPosition = TimeSpan.Zero;

    public MainWindow()
    {
        InitializeComponent();
        player1 = new MediaPlayer();
        player1.MediaEnded += Player1_MediaEnded;            
    }

    private void OpenMedia()
    {
        player1.Open(new Uri(@"M:\Sting.wav"));
        //player1.Open(new Uri(@"M:\ring.wav"));
    }

    private void Player1_MediaEnded(object sender, EventArgs e)
    {
        LastPosition = TimeSpan.Zero;            
    }

    private void Button_PlayResume(object sender, RoutedEventArgs e)
    {
        if (LastPosition != TimeSpan.Zero)
        {
            player1.Position = LastPosition; // Resume    
            LastPosition = TimeSpan.Zero;               
        }
        else
        {
            OpenMedia();
        }
        player1.Play();
    }

    private void Button_Pause(object sender, RoutedEventArgs e)
    {            
        player1.Pause();
        LastPosition = player1.Position;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        SystemEvents.PowerModeChanged += OnPowerModeChanged;
    }

    private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
    {
        if (e.Mode == PowerModes.Suspend)
        {
            if (player1.CanPause)
            {
                player1.Pause();
                LastPosition = player1.Position;
            }
            player1.Stop();
        }
    }
}

The screen-shot:

enter image description here

This will work even when the MediaPlayer is playing something. After resuming from sleeping mode press on Play/Resume button to continue playing from the latest position.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33