我剛開始構建節拍器應用程序,但我在計時器刻度上播放示例聲音時遇到了麻煩。我設置計時器刻度,例如調用每1秒播放一次聲音的方法。然後它出現了一個問題,即聲音只播放一次,沒有更多。WPF播放聲音定時器刻度 - 不重複聲音
所以,我的問題 - 如何做到這一點在每個計時器滴答聲播放聲音?
<Window x:Class="Metronome.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>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<TextBox Name="textBox1" />
<Button Name="button1" Content="Start" Click="button1_Click" />
<MediaElement Name="mediaElement1" Source="MetroBar1.wav" LoadedBehavior="Manual" />
</StackPanel>
</Grid>
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace Metronome
{
public partial class MainWindow : Window
{
const int secInMin = 60;
int beatRate;
double beatPerMsec;
DispatcherTimer timer1;
public MainWindow()
{
InitializeComponent();
timer1 = new DispatcherTimer();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//beatRate = Convert.ToInt16(textBox1.Text);
//beatPerMsec = Math.Pow((beatRate/secInMin), -1) * 1000; //Amount of beats (timer ticks) per msec
// timer1.Tick += new EventHandler(metronome_Run);
timer1.Tick += metronome_Run;
//timer1.Interval = new TimeSpan(0, 0, 0, 0, Convert.ToInt16(beatPerMsec));
timer1.Interval = new TimeSpan(0, 0, 1);
timer1.Start();
}
private void metronome_Run(object sender, EventArgs e)
{
mediaElement1.Play();
}
}
}
爲什麼要寫Math.Pow(a/b,-1)而不是b/a? :) – Adassko