设置mediaElement源的问题 [英] Problems with setting the source for mediaElement
问题描述
基本上我正在尝试创建一个简单的游戏,您将正在播放的歌曲与其所属的专辑封面相匹配。
我有一个类Song.vb只有属性
命名空间模型
公共类歌曲
公共财产标识As Integer
Public Property Title As String
Public Property Artist As String
Public Property Album As String
Public Property SongFile As StorageFile
Public Property Selected As Boolean
公共属性用作布尔值
公共属性AlbumCover为BitmapImage
结束类
结束命名空间
它会加载,拍照并开始游戏,问题是当声音应该播放时它会发挥作用,但事实并非如此。
我有这个StartCooldown函数准备新游戏,并调用我的CountDown故事板
Private Sub StartCooldown()
'启动我们的storyBoard for animation
_playingMusic = False
Dim brush As New SolidColorBrush(Colors.Blue)
MyProgressBar.Foreground = brush
InstructionTextBlock.Text = String.Format("准备好轮次{0} ...",_round + 1)
InstructionTextBlock.Foreground =刷
CountDown.Begin()
结束子
和故事板Xaml
< Storyboard x:Name =" CountDown"完成= QUOT; CoolDown_Completed">
< DoubleAnimationUsingKeyFrames EnableDependentAnimation =" True"
Storyboard.TargetName =" MyProgressBar"
Storyboard.TargetProperty ="(RangeBase.Value)">
< DiscreteDoubleKeyFrame KeyTime =" 0"值= QUOT; 100" />
< DiscreteDoubleKeyFrame KeyTime =" 0:0:1"值= QUOT; 100" />
< DiscreteDoubleKeyFrame KeyTime =" 0:0:2"值= QUOT; 90" />
< DiscreteDoubleKeyFrame KeyTime =" 0:0:3"值= QUOT; 80 QUOT; />
< DiscreteDoubleKeyFrame KeyTime =" 0:0:4"值= QUOT; 70" />
< DiscreteDoubleKeyFrame KeyTime =" 0:0:5"值= QUOT; 60" />
< DiscreteDoubleKeyFrame KeyTime =" 0:0:6"值= QUOT; 50" />
< DiscreteDoubleKeyFrame KeyTime =" 0:0:7"值= QUOT; 40" />
< DiscreteDoubleKeyFrame KeyTime =" 0:0:8"值= QUOT; 30英寸; />
< DiscreteDoubleKeyFrame KeyTime =" 0:0:9"值= QUOT; 20" />
< DiscreteDoubleKeyFrame KeyTime =" 0:0:10"值= QUOT; 10" />
< / DoubleAnimationUsingKeyFrames>故事板执行后,
< / Storyboard>
转到具有PickSong功能的CoolDown_Completed事件
rivate Async Sub CoolDown_Completed(sender As Object,e As Object)
如果不是_playingMusic那么
'开始播放音乐
Dim song = PickSong()
MyMediaElement.SetSource(等待song.SongFile.OpenAsync(FileAccessMode.Read),song.SongFile.ContentType)
'开始倒计时
StartCountDown()
End if
End Sub
Private Function PickSong()
Dim random As New Random
Dim unusedSongs = Songs.Where(Function(p)p.Used = False)
Dim randomNumber = random.Next(unusedSongs.Count)
Dim randomSong = unusedSongs.ElementAt(randomNumber)
randomSong.Selected = True
返回randomS ong
结束函数
当我调试它时,它会在行中抛出异常
MyMediaElement.SetSource(Await song.SongFile.OpenAsync(FileAccessMode.Read),song.SongFile.ContentType)
System.ArgumentNullException: '值不能为空。'
此部分代码的值:
等待song.SongFile.OpenAsync(FileAccessMode.Read)
是: " {System._ComObject}"
并从此部分:
song.SongFile.ContentType
是: " audio / mpeg"
我所有的歌曲文件都是mp3。
我的问题是,为什么我会得到空值异常,以及如何我可以播放我的歌吗?
确保
MyMediaElement 不是Nothing。还要检查 song.SourceFile 的其他成员是否看起来不错。
使用一些显式存储文件尝试简化的
SetSource 。 跨度> 跨度>
Basicly i am trying to create a simple game where you match song that is playing to the album cover that it belongs to.
I have a class Song.vb that just has properties
Namespace Models Public Class Song Public Property Id As Integer Public Property Title As String Public Property Artist As String Public Property Album As String Public Property SongFile As StorageFile Public Property Selected As Boolean Public Property Used As Boolean Public Property AlbumCover As BitmapImage End Class End Namespace
it loads, pictures, and starts the game, problem is when it comes to the part when the sound is supposed to be playing, it doesn't.
I have this StartCooldown Function that prepares for the new game, and calls my CountDown Storyboard
Private Sub StartCooldown() 'starts our storyBoard for animation _playingMusic = False Dim brush As New SolidColorBrush(Colors.Blue) MyProgressBar.Foreground = brush InstructionTextBlock.Text = String.Format("Get ready for round {0} ...", _round + 1) InstructionTextBlock.Foreground = brush CountDown.Begin() End Sub
and the storyboard in Xaml
<Storyboard x:Name="CountDown" Completed="CoolDown_Completed"> <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetName="MyProgressBar" Storyboard.TargetProperty="(RangeBase.Value)"> <DiscreteDoubleKeyFrame KeyTime="0" Value="100" /> <DiscreteDoubleKeyFrame KeyTime="0:0:1" Value="100" /> <DiscreteDoubleKeyFrame KeyTime="0:0:2" Value="90" /> <DiscreteDoubleKeyFrame KeyTime="0:0:3" Value="80" /> <DiscreteDoubleKeyFrame KeyTime="0:0:4" Value="70" /> <DiscreteDoubleKeyFrame KeyTime="0:0:5" Value="60" /> <DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="50" /> <DiscreteDoubleKeyFrame KeyTime="0:0:7" Value="40" /> <DiscreteDoubleKeyFrame KeyTime="0:0:8" Value="30" /> <DiscreteDoubleKeyFrame KeyTime="0:0:9" Value="20" /> <DiscreteDoubleKeyFrame KeyTime="0:0:10" Value="10" /> </DoubleAnimationUsingKeyFrames> </Storyboard>
after the storyboard executes it goes to CoolDown_Completed event that has PickSong function
rivate Async Sub CoolDown_Completed(sender As Object, e As Object) If Not _playingMusic Then 'start playing music Dim song = PickSong() MyMediaElement.SetSource(Await song.SongFile.OpenAsync(FileAccessMode.Read), song.SongFile.ContentType) 'start countdown StartCountDown() End If End Sub Private Function PickSong() Dim random As New Random Dim unusedSongs = Songs.Where(Function(p) p.Used = False) Dim randomNumber = random.Next(unusedSongs.Count) Dim randomSong = unusedSongs.ElementAt(randomNumber) randomSong.Selected = True Return randomSong End Function
When i debugg it, it throws me an exception at line
MyMediaElement.SetSource(Await song.SongFile.OpenAsync(FileAccessMode.Read), song.SongFile.ContentType)
System.ArgumentNullException: 'Value cannot be null.'
The value of this part of code:
Await song.SongFile.OpenAsync(FileAccessMode.Read)
is: "{System._ComObject}"
and from this part:
song.SongFile.ContentType
is : "audio/mpeg"
All my song files are mp3.
My question is, why do i get the null value exception, and how can i get it to play my songs?
Make sure that MyMediaElement is not Nothing. Also check if other members of song.SourceFile look good.
Try a simplified SetSource with some explicit storage file.
这篇关于设置mediaElement源的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!