Archive

‘.NET’ 分類過的Archive

VB.NET 播MP3

2009年3月20日 尚無評論

最近要用.Net 做的簡單的小AP,發現元件很多但不知如何播放MP3,原本預設提供的元件只能播放WAV檔,去找了一下資料,顯示於下面,方便自己學習及提供給大家

目前都是要透過winmm.dll 的library來達到,使用方式還算容易啦,就照copy & paste 就可以執行啦!!

Public Class Form1

   ' 宣告 API
   Private Declare Function mciSendStringA Lib “winmm.dll” _
       (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
       ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

   Private Sub Button1_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button1.Click
       PlayMidiFile(“C:\死了都要愛.mp3”) ' 播放 MP3 音樂
       '或
       'PlayMidiFile(“C:\頑皮豹.mid”) ' 播放 MIDI 音樂
   End Sub

   Private Sub Button2_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button2.Click
       StopMidi() ' 停止播放
   End Sub

   Private Sub Button3_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button3.Click
       PauseMidi() ' 暫停播放
   End Sub

   Private Sub Button4_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button4.Click
       ContinueMidi()  ' 繼續播放
   End Sub

   Private Function PlayMidiFile(ByVal MusicFile As String) As Boolean
       If System.IO.File.Exists(MusicFile) Then
           mciSendStringA(“stop music”, “”, 0, 0)
           mciSendStringA(“close music”, “”, 0, 0)
           mciSendStringA(“open ” & MusicFile & ” alias music”, “”, 0, 0)
           PlayMidiFile = mciSendStringA(“play music”, “”, 0, 0) = 0
       End If
   End Function

   Private Function StopMidi() As Boolean
       StopMidi = mciSendStringA(“stop music”, “”, 0, 0) = 0
       mciSendStringA(“close music”, “”, 0, 0)
   End Function

   Private Function PauseMidi() As Boolean
       Return mciSendStringA(“pause music”, “”, 0, 0) = 0
   End Function

   Private Function ContinueMidi() As Boolean
       Return mciSendStringA(“play music”, “”, 0, 0) = 0
   End Function

End Class

資料來源:http://blog.blueshop.com.tw/hammerchou/archive/2008/10/24/57452.aspx

原本以為上面的範例不能背景重複播放音樂,但是小弟的程式有誤,一切正常

Imports System
Imports System.Runtime.InteropServices
Imports System.Resources
Imports System.IO

Public Class Sound

Private Shared SND_ASYNC As Integer = 1
Private Shared SND_MEMORY As Integer = 4

Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As Byte(), ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer

Public Shared Sub PlayWavResource(ByVal wav As String)

' get the namespace
Dim strNameSpace As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()

' get the resource into a stream
Dim resourceStream As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(strNameSpace + "." + wav)
If resourceStream Is Nothing Then Exit Sub

' bring stream into a byte array
Dim wavData As Byte()
ReDim wavData(CInt(resourceStream.Length))
resourceStream.Read(wavData, 0, CInt(resourceStream.Length))

' play the resource
PlaySound(wavData, 0, SND_ASYNC Or SND_MEMORY)
End Sub
End Class

http://www.gringod.com/2005/01/21/playing-a-wav-from-a-resource-in-vbnet/
使用MP3的音樂是比播WAV麻煩,包含這些資源檔封裝後是不可以播放的~真是太可惜了(有可能是小弟才疏學淺,如其它大大知道,再和小弟說一下)

仔細想想,十幾年沒碰VB啦~~(幾年前還有用C寫一個小程式呢)

Categories: .NET Tags: , , ,