C# 是否可以在BackgroundAudioPlayer中设置开始和结束位置
我正在四处寻找设置C# 是否可以在BackgroundAudioPlayer中设置开始和结束位置,c#,windows-phone-8,C#,Windows Phone 8,我正在四处寻找设置BackgroundAudioPlayer的开始和结束位置。虽然我可以设置开始位置,但是我也希望设置结束位置 假设,我的音频文件是22分钟,用户只希望听到10分钟到18分钟。是否可以在BackgroundAudioPlayer中设置开始和结束位置 谢谢 我在桌面应用程序中遇到了这个难题,我通过启动一个计时器,将滴答时间设置为“停止-启动”位置来解决它。当计时器启动时,停止音频播放器和计时器 你也可以用一个线程来实现这一点,但定时器似乎是一个更干净的解决方案。下面是一个例子——尽
BackgroundAudioPlayer
的开始和结束位置。虽然我可以设置开始位置,但是我也希望设置结束位置
假设,我的音频文件是22分钟,用户只希望听到10分钟到18分钟。是否可以在BackgroundAudioPlayer
中设置开始和结束位置
谢谢 我在桌面应用程序中遇到了这个难题,我通过启动一个计时器,将滴答时间设置为“停止-启动”位置来解决它。当计时器启动时,停止音频播放器和计时器 你也可以用一个线程来实现这一点,但定时器似乎是一个更干净的解决方案。下面是一个例子——尽管我使用了一个属于GUI的MediaElement(这就是我使用pinvoke的原因)
代替计时器,不会等待Task.Delay(TimeSpan.FromSeconds(duration))代码>工作?嗯。。。也许会。实际上,这可能是一个更好的解决方案:)那么让我们由OP来决定。在我的例子中,我有一个桌面应用程序,“等待”不是一个解决方案。谢谢,我正在考虑使用计时器,但是如果有帮助的话,让我使用@patrick解决方案。它没有理由不工作。请确保您的GUI线程没有被WAIT子句阻塞。谢谢,我使用了计时器,它的工作原理略有不同。我正在检查@Patrick solution。等待是我的问题:(
void sndPlayer_MediaOpened(object sender, EventArgs e)
{
// Set the current position in the recording
sndPlayer.Position = currentRecording.tPosition;
// Start playing
sndPlayer.Play();
// Schedule a function to stop the player
timerControlPlayer.Interval = (double)currentDuration * 1000.0;
timerControlPlayer.Start();
}
void timerControlPlayer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// We don't need the timer to pop in again
timerControlPlayer.Stop();
// Access the gui thread (the sound player belongs to that one)
guiDispatcher.Invoke(() =>
{
sndPlayer.Stop();
SoundPlayed(this, new SoundPlayedEventArgs(currentRecording.nSpeakerCount));
sndPlayer.Close();
});
}
// Used as an elegant solution to start / stop the sound player
timerControlPlayer = new System.Timers.Timer();
timerControlPlayer.Elapsed += timerControlPlayer_Elapsed;