[英]Windows Phone sound play
I have a lot of .wav
files to play and because of that i created AudioItem
class:我有很多
.wav
文件要播放,因此我创建了AudioItem
class:
public class AudioItem
{
public string SongName { get; set; }
public string SongText { get; set; }
public AudioItem()
{ }
/// <summary>
/// Initialize AudioItem class
/// </summary>
/// <param name="SongName">The name of the song(with .wav)</param>
/// <param name="SongText">Text to display</param>
public AudioItem(string SongName, string SongText)
{
this.SongName = SongName;
this.SongText = SongText;
}
public void Play()
{
var stream = TitleContainer.OpenStream("Sound/" + SongName);
var effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
}
}
In the view i have a Listbox
of items:在视图中,我有一个项目
Listbox
:
<ListBox Name="soundtrackLbx">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Name="ListButton" HorizontalAlignment="Stretch" >
<Button.Content>
<StackPanel Orientation="Horizontal">
<Image Margin="10" Height="30" Width="30" Source="Picture/audio.png"/>
<TextBlock Margin="10" HorizontalAlignment="Stretch" Text="{Binding SongText}" FontFamily="Verdana" Tap="TextBlock_Tap" />
</StackPanel>
</Button.Content>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
my code behind:我的代码背后:
public ObservableCollection<AudioItem> AudioItems { get; set; }
public MainView()
{
InitializeComponent();
AudioItems = new ObservableCollection<AudioItem>();
Initialize();
this.soundtrackLbx.ItemsSource = AudioItems;
}
private void Initialize()
{
AudioItems.Add(new AudioItem("ApsolutnoDa.wav", "Apsolutno da"));
AudioItems.Add(new AudioItem("Da.wav", "Da"));
}
private void TextBlock_Tap(object sender, GestureEventArgs e)
{
TextBlock tblock = sender as TextBlock;
string text = tblock.Text;
AudioItem item = AudioItems.SingleOrDefault(a => a.SongText == text);
item.Play();
}
well, it happens that .wav
sometimes i hear them play, and sometimes i don't, and i have no clue why is that.好吧,碰巧
.wav
有时我听到他们演奏,有时我听不到,我不知道为什么会这样。
OK.好的。 Here it is, if someone ever got trapped as i did.
在这里,如果有人像我一样被困。 The problem was because i set the
Tap
event on the Textblock
, not to the Button
.问题是因为我在
Textblock
上设置了Tap
事件,而不是Button
。
How did i get content from Button.Content
?我是如何从
Button.Content
获取内容的?
I use tag
property and i bind it to SongName
property.我使用
tag
属性并将其绑定到SongName
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.