Vb.net 等待媒体元素具有自然适应信息
我需要做一个循环来查看很多mp3文件,并使用mediaElement获取它们的自然适应属性。问题是mediaElement需要一些时间来加载每个文件,并且.source属性的工作方式类似于异步进程(我认为),因为如果我想获得naturalDuration属性,我必须在下面的代码上单击两次。第一次点击我只有00:00:00的值,第二次点击给我真正的值Vb.net 等待媒体元素具有自然适应信息,vb.net,mediaelement,Vb.net,Mediaelement,我需要做一个循环来查看很多mp3文件,并使用mediaElement获取它们的自然适应属性。问题是mediaElement需要一些时间来加载每个文件,并且.source属性的工作方式类似于异步进程(我认为),因为如果我想获得naturalDuration属性,我必须在下面的代码上单击两次。第一次点击我只有00:00:00的值,第二次点击给我真正的值 Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles b
Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
mediaElement.Source = New Uri("\\Mac\Home\Desktop\NOVEDADES01\AbrahamMateo-OldSchool.mp3")
textBlock.Text = mediaElement.NaturalDuration.ToString
End Sub
如果我尝试在.Source指令之后等待,应用程序将保持循环
Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
mediaElement.Source = New Uri("\\Mac\Home\Desktop\NOVEDADES01\AbrahamMateo-OldSchool.mp3")
Do
Loop Until mediaElement.NaturalDuration.TimeSpan.TotalSeconds > 0
textBlock.Text = mediaElement.NaturalDuration.ToString
End Sub
我还通过异步进程尝试设置.source并等待mediaOpenend事件,但看起来mediaElemento在第一次单击结束之前无法结束
在set.source属性之后,如果不播放文件,如何在单个进程或函数中获得naturalDuration的实际值?
非常感谢 好的,很晚了,可能不是很优雅,但这是我用来获取mp3文件持续时间的解决方案。我上过音乐课 公共函数infoMP3(elfichero作为字符串)作为字符串
Dim salida As String = ""
Dim miTask = Task.Run(Async Function() As Task(Of String)
Dim musicFile As StorageFile = Await StorageFile.GetFileFromPathAsync(elfichero)
Dim FileProperties As StorageItemContentProperties = musicFile.Properties
Dim musicFileProperties As MusicProperties = Await FileProperties.GetMusicPropertiesAsync()
Dim tiempo = musicFileProperties.Duration
Dim horas As String
If tiempo.Hours < 10 Then
horas = "0" & tiempo.Hours.ToString
Else
horas = tiempo.Hours.ToString
End If
Dim minutos As String
If tiempo.Minutes < 10 Then
minutos = "0" & tiempo.Minutes.ToString
Else
minutos = tiempo.Minutes.ToString
End If
Dim segundos As String
If tiempo.Seconds < 10 Then
segundos = "0" & tiempo.Seconds.ToString
Else
segundos = tiempo.Seconds.ToString
End If
Dim autor = musicFileProperties.Artist
Dim titulo = musicFileProperties.Title
Dim presalida As String = "[" & horas & ":" & minutos & ":" & segundos & "];[" & titulo & "];[" & autor & "] " & elfichero
Return presalida
End Function)
miTask.Wait()
salida = miTask.Result
Return salida
End Function
Dim salida As String=“”
Dim miTask=Task.Run(异步函数()作为任务)(字符串)
Dim musicFile As StorageFile=等待StorageFile.GetFileFromPathAsync(elfichero)
将文件属性设置为StorageItemContentProperties=musicFile.Properties
将musicFileProperties设置为MusicProperties=Await FileProperties.GetMusicPropertiesAsync()
Dim tiempo=音乐文件属性。持续时间
把霍拉斯当作弦
如果tiempo.小时数小于10,则
horas=“0”和tiempo.Hours.ToString
其他的
horas=tiempo.Hours.ToString
如果结束
作为字符串的暗淡分钟数
如果tiempo.Minutes<10,则
minutos=“0”&tiempo.Minutes.ToString
其他的
分钟数=tiempo.Minutes.ToString
如果结束
作为字符串的Dim segudos
如果tiempo.秒数小于10,则
segundos=“0”&tiempo.Seconds.ToString
其他的
segundos=tiempo.Seconds.ToString
如果结束
Dim autor=musicFileProperties.Artist
Dim titulo=musicFileProperties.Title
Dim presalida As String=“[”&horas&“:”&minutos&“:”&segundos&“];[”&titulo&“];[”&autor&“]”&elfichero
返回前Salida
终端功能)
米塔斯克,等等
萨利达=米塔斯克。结果
返回萨利达
端函数
若要稍后在Windows 10上访问文件,您必须保存文件和/或文件夹的权限。选择它们时执行此操作
。。。
Dim listToken=Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(rutaS)
其中rutaS是一个SotorageFolder对象