你的位置:首页 > 软件开发 > ASP.net > UWP简单示例(一):快速合成音乐MV

UWP简单示例(一):快速合成音乐MV

发布时间:2016-06-06 04:00:06
准备  IDE:Visual Studio 2015  为你的项目安装Nuget包 SharpDx.XAudio2  为你的项目安装Nuget包 Win2D.UWP  了解并学习:Win2D官方博客  了解并学习:Win2D官方示例第一节 波形  获取实时时域数据。Imports ...

准备

  IDE:Visual Studio 2015

  为你的项目安装Nuget包 SharpDx.XAudio2

  为你的项目安装Nuget包 Win2D.UWP

  了解并学习:Win2D官方博客

  了解并学习:Win2D官方示例

第一节 波形

  获取实时时域数据。

UWP简单示例(一):快速合成音乐MVUWP简单示例(一):快速合成音乐MV
Imports SharpDX.MultimediaImports SharpDX.XAudio2Public Class AudioPlayer  Public Event WavePlaying(e As WavePlayingEventArgs)  Public Property Device As XAudio2  Public Property Voice As SourceVoice  Private CurrentFormat As WaveFormat  Private CurrentBuffer As AudioBuffer  Private PacketsInfo As UInteger()  Public Sub New()    Device = New XAudio2()    Device.StartEngine()    Dim mv As New MasteringVoice(Device)  End Sub  Public Async Function LoadFile(fileName As String) As Task(Of Boolean)    Try      Voice = Await CreateVoiceFromFile(Device, fileName)      LoadBuffer()      Return True    Catch      Return False    End Try  End Function  Public Sub Play(Optional volume As Single = 1.0F)    Voice?.SetVolume(volume)    Voice?.Start()    ReadBuffer()  End Sub  Public Sub [Stop]()    Voice?.Stop()  End Sub  Protected Async Function CreateVoiceFromFile(device As XAudio2, fileName As String) As Task(Of SourceVoice)    Dim file = Await Package.Current.InstalledLocation.GetFileAsync(fileName)    Dim streamWithContentType = Await file.OpenReadAsync()    Dim st = streamWithContentType.AsStreamForRead()    Using stream = New SoundStream(st)      CurrentFormat = stream.Format      CurrentBuffer = New AudioBuffer() With {      .Stream = stream.ToDataStream(),      .AudioBytes = CInt(stream.Length),      .Flags = BufferFlags.EndOfStream    }      PacketsInfo = stream.DecodedPacketsInfo    End Using    Dim sourceVoice = New SourceVoice(device, CurrentFormat, True)    Return sourceVoice  End Function  Protected Sub LoadBuffer()    Voice?.FlushSourceBuffers()    Voice?.SubmitSourceBuffer(CurrentBuffer, PacketsInfo)  End Sub  ''' <summary>  ''' 从流中读取当前播放的数据  ''' </summary>  Private Async Sub ReadBuffer()    Try      Dim count As Integer = CurrentFormat?.AverageBytesPerSecond / 10      While Voice.State.BuffersQueued > 0        If Voice.State.SamplesPlayed * CurrentFormat.BlockAlign > CurrentBuffer.Stream.Position + count Then          Dim byteArr(count - 1) As Byte          Await CurrentBuffer.Stream.ReadAsync(byteArr, 0, count)          RaiseEvent WavePlaying(New WavePlayingEventArgs(byteArr, CurrentFormat))        Else          Await Task.Delay(10)        End If      End While    Catch      Return    End Try  End Sub  Public Function Position() As Integer    Return Voice.State.SamplesPlayed / CurrentFormat.SampleRate  End Function  Protected Overrides Sub Finalize()    Try      Dispose(False)    Finally      MyBase.Finalize()    End Try  End Sub  Public Sub Dispose()    Dispose(True)    GC.SuppressFinalize(Me)  End Sub  Private Sub Dispose(isDisposing As Boolean)    If Not isDisposing Then      Return    End If    Voice.DestroyVoice()    Voice.Dispose()    CurrentBuffer.Stream.Dispose()  End SubEnd Class

原标题:UWP简单示例(一):快速合成音乐MV

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录