无法从剪贴板播放mp3文件

时间:2016-03-01 03:01:54

标签: vb.net memory mp3 clipboard

我在VB NET中有这个小项目,应该使用AxWindowsMediaPlayer1从剪贴板播放mp3文件。代码正在进行到最后一行,我想在内存中播放mp3(只是解密)字节,而不是将它们保存到文件中然后播放文件。

这是代码。表单需要3个按钮,1个标签,1个Windows Media Player。你应该选择你的一个mp3文件。希望有人可以帮助我!

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Public Class Form1

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    End
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim rijndael As New RijndaelManaged()
    rijndael.KeySize = 256
    rijndael.BlockSize = 256
    rijndael.IV = New [Byte]() {24, 23, 35, 83, 77, 35, 28, 34, 94, 25, 45, 2, 73, 26, 27, 78, 12, 23, 35, 83, 57, 35, 28, 34, 94, 25, 45, 22, 73, 26, 27, 78}
    Dim password As Byte() = New Byte(31) {}
    UTF8Encoding.UTF8.GetBytes("123abc").CopyTo(password, 0)
    Dim file_to_read As New CryptoStream(File.OpenRead("C:\Users\User\Desktop\aa.mp3"),
  rijndael.CreateEncryptor(password, rijndael.IV), CryptoStreamMode.Read)
    Dim file_to_write As Stream = File.OpenWrite("C:\Users\User\Desktop\bb.mp3")
    file_to_read.CopyTo(file_to_write)
    file_to_write.Flush()
    file_to_write.Close()
    file_to_read.Close()
    Label1.Text = "Crypted!"
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim rijndael As New RijndaelManaged()
    rijndael.KeySize = 256
    rijndael.BlockSize = 256
    rijndael.IV = New [Byte]() {24, 23, 35, 83, 77, 35, 28, 34, 94, 25, 45, 2, 73, 26, 27, 78, 12, 23, 35, 83, 57, 35, 28, 34, 94, 25, 45, 22, 73, 26, 27, 78}
    Dim password As Byte() = New Byte(31) {}
    UTF8Encoding.UTF8.GetBytes("123abc").CopyTo(password, 0)
    Dim mp3Bytes() As Byte
    Using file_to_read As New CryptoStream(File.OpenRead("C:\Users\User\Desktop\bb.mp3"), rijndael.CreateDecryptor(password, rijndael.IV), CryptoStreamMode.Read)
        Using memory_stream As New MemoryStream
            file_to_read.CopyTo(memory_stream)
            mp3Bytes = memory_stream.GetBuffer
        End Using
    End Using
    Label1.Text = "Decrypted!"

    Clipboard.Clear()            'clear the clipboard
    Clipboard.SetAudio(mp3Bytes) 'set the mp3 audio file bytes to the clipboard
    If Clipboard.ContainsAudio Then
        Dim bytes_from_clipboard() As Byte = {}
        Using clipboard_stream As Stream = Clipboard.GetAudioStream
            ReDim bytes_from_clipboard(CInt(clipboard_stream.Length) - 1)
            clipboard_stream.Read(bytes_from_clipboard, 0, CInt(clipboard_stream.Length))
        End Using

        'UP TO THIS POINT IT IS OKAY. FROM HERE THE FREELANCER'S TASK BEGINS
        'I would like to play with Windows Media Player the mp3 file just decrypted into the clipboard.

        AxWindowsMediaPlayer1.Ctlcontrols.play() ' ???? WHAT CAN I DO HERE? IS THERE ANY SOLUTION?

    End If

End Sub

End Class

1 个答案:

答案 0 :(得分:0)

AxWindowsMediaPlayer没有源于其不播放的原因。你应该使用:

AxWindowsMediaPlayer.URL = 'location/source of the mp3 file
AxWindowsMediaPlayer1.Ctlcontrols.play()

希望有所帮助!