我使用本指南制作自定义的长列表选择器,并在选择项目时播放声音。
http://channel9.msdn.com/Series/Windows-Phone-8-Development-for-Absolute-Beginners/Part-15-Playing-a-Sound-when-a-ListItem-is-Selected
一切都有效。
现在我正试图找到一种方法来停止声音,如果相同的项目被轻敲,它仍在播放声音,但无法找到正确的解决方案。有什么想法吗?
非常感谢你。
修改的
现在它仍在播放时单击停止。但如果我点击另一个元素,它也会停止。有什么建议?请
private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LongListSelector selector = sender as LongListSelector;
// verifying our sender is actually a LongListSelector
if (selector == null)
return;
if (AudioPlayer.CurrentState == System.Windows.Media.MediaElementState.Playing)
{
AudioPlayer.Stop();
selector.SelectedItem = null;
}
else
{
SoundData data = selector.SelectedItem as SoundData;
// verifying our sender is actually SoundData
if (data == null)
return;
AudioPlayer.Source = new Uri(data.FilePath, UriKind.RelativeOrAbsolute);
// resetting selected so we can play the same sound over and over again
selector.SelectedItem = null;
}
}
答案 0 :(得分:0)
看看你的代码,你只是停下来。这并不难,你只需要记住你之前点击的内容:
private SounData selected = null;
private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LongListSelector selector = sender as LongListSelector;
if (AudioPlayer.CurrentState == System.Windows.Media.MediaElementState.Playing &&
((SoundData)selector.SelectedItem == selected))
{
AudioPlayer.Stop();
selector.SelectedItem = null;
}
else
{
SoundData data = selector.SelectedItem as SoundData;
// verifying our sender is actually SoundData
if (data == null)
return;
AudioPlayer.Source = new Uri(data.FilePath, UriKind.RelativeOrAbsolute);
// resetting selected so we can play the same sound over and over again
selected = data;
selector.SelectedItem = null;
}
}
我认为其余的你应该能够弄清楚。