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: , , ,

Top 20 replies by Programmers to Testers when their programs don't work..老外篇

2007年2月9日 4 則評論

最近看到的 蠻有趣的~

大家欣賞一下自已曾說過的話吧!!

也許這就是工程師的common sense吧…

有些看不太懂~不敢亂翻~有些也可能會錯意
請各位再補充給我

Top 20 replies by Programmers to Testers when their programs don't work

COUNT DOWN……

20. “That's weird…” 那太不可思議了..

19. “It's never done that before.” 以前從來沒有那樣過

18. “It worked yesterday.”  它昨天還是正常的.

17. “How is that possible?” 那是有可能的好嗎?

16. “It must be a hardware problem.” 這一定是硬體的問題

15. “What did you type in wrong to get it to crash?” 你是輸入了什麼而得到這個讓它crash的錯誤呢?

14. “There is something funky in your data.” 有一些怪資料在我的data裡

13. “I haven't touched that module in weeks!” 我在這星期並沒有改到那個模組

12. “You must have the wrong version.” 你一定使用了錯誤的版本

11. “It's just some unlucky coincidence.” 這就只是些不幸的巧合罷了

10. “I can't test everything!”  我不能每件事都測試過!!

9. “THIS can't be the source of THAT.”

8. “It works, but it hasn't been tested.”

7. “Somebody must have changed my code.” 一定有人改過我的code

6. “Did you check for a virus on your system?” 你確定檢查過你的電腦沒有中病毒嗎?

5. “Even though it doesn't work, how does it feel?

4. “You can't use that version on your system.” 在你的電腦不能使用這個版本

3. “Why do you want to do it that way?” 為什麼你要用那種方式執行它呢?

2. “Where were you when the program blew up?” 當程式出問題時,你在哪?

1. “It works on my machine”  在我的機器上是可以動的!!

文章出處:http://underthesunz.blogspot.com/2006/11/top-20-replies-by-programmers-to.html

Categories: Project Management Tags: