等到media元素有naturalDuration信息

时间:2016-04-15 12:01:55

标签: vb.net mediaelement

我需要创建一个循环来查看很多mp3文件并使用mediaElement获取他们的naturalDuration属性。问题是mediaElement需要一些时间来加载每个文件,而.source属性就像异步进程一样(我认为),因为如果我想获取naturalDuration属性,我必须在下面的代码上单击两次。首先点击我只有00:00:00的价值,第二次clik给我真正的价值。

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在第一次点击结束前无法结束

如何在设置.source属性并且不播放文件之后,在单个进程或函数中获得naturalDuration的实际值? 非常感谢!!

1 个答案:

答案 0 :(得分:0)

好的,迟到的可能不是那么优雅,但这是我获取mp3文件持续时间的解决方案。我使用了MusicProperties Class。

Public Function infoMP3(elfichero As String)As String

    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

要在以后在Windows 10上访问这些文件,您必须保存文件和/或文件夹的权限。选择它们时,请执行此操作。

... Dim listToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(rutaS) ...

其中rutaS是SotorageFolder对象。