2016-03-01 67 views
0

我在VB中有這個小項目,應該使用AxWindowsMediaPlayer1播放剪貼板中的mp3文件。代碼正在工作到最後一行,我想要播放內存中的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 
+0

AxWindowsMediaPlayer不知道剪貼板中的內容。如果你不想將它保存到一個文件並且只有一個流,也許你可以試試NAudio庫來播放這個mp3文件:http://stackoverflow.com/questions/184683/play-audio-from-a- stream-using-c-sharp –

+2

我喜歡代碼註釋「到這個地方它是好的,從這裏你可以看到自由人的任務開始」。 – TnTinMn

回答

0

AxWindowsMediaPlayer沒有源代碼,因此它沒有播放。您應該使用:

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

希望有所幫助!