How can I make a sound sound every so often?

1

I am trying to realize that when a hand passes a coordinate, a sound will sound. The problem arises when passing the hand, which is tried to run constantly since it is a sound of about 2 seconds. Could you do it in a way that you do it every so often?

//Sector 3
if (FuncionActivada == 1 && VariablesGlobales.HandRightX >= 0.38 && VariablesGlobales.HandRightY >= 0.35 && VariablesGlobales.HandRightZ >= 0.9
    && VariablesGlobales.HandRightZ <= 1.3)
{
    SoundPlayer miSonido3 = new SoundPlayer(@"c:\Windows\Media\Ring03.wav");
    miSonido3.Play();
}
    
asked by Daniel Potrimba 30.05.2017 в 11:03
source

2 answers

1

You could put a timer :

DispatcherTimer dispathcer  = new DispatcherTimer();

//Intervalo de 1 segundo
dispatcher.Interval = new TimeSpan (0,0,1);
dispatcher.Tick += (s, a) => {
//la función a ejecutar, en este caso el sonido
}
dispathcer.Start();
    
answered by 30.05.2017 в 11:07
1

Implement a small solution that when you pass the mouse over a rectangle it shows in a label "beep .." and after finishing the sound it waits for five seconds and goes off.

I added that it shows the ticks so that you see that no matter how many times you enter and leave the rectangle it does not sound again until the previous one ends.

I use BackgroundWorker to control the flow of execution in a thread.

<Window x:Class="WpfTimer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="100" MouseEnter="Rectangle_MouseEnter"/>
    <Label Name="lblSalida" Content="Label" HorizontalAlignment="Left" Margin="115,10,0,0" VerticalAlignment="Top" Width="392"/>
</Grid>

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace WpfTimer
{
public partial class MainWindow : Window
{
    BackgroundWorker parlante;

    public MainWindow()
    {
        InitializeComponent();
        parlante = new BackgroundWorker();
        parlante.DoWork += sonar;
        parlante.RunWorkerCompleted += apagar;
    }

    private void sonar(object sender, DoWorkEventArgs e)
    {
        //Reproducir sonido
        //demorar hasta el siguiente beep
        System.Threading.Thread.Sleep(5000);
    }

    private void apagar(object sender, RunWorkerCompletedEventArgs e)
    {
        lblSalida.Content = "";
    }

    private void Rectangle_MouseEnter(object sender, MouseEventArgs e)
    {
        if (!parlante.IsBusy)
        {
            lblSalida.Content = "beep.. " + DateTime.Now.Ticks;
            parlante.RunWorkerAsync();
        }
    }
}
}
    
answered by 30.05.2017 в 15:52