18. november 2005 - 19:41Der er
10 kommentarer og 1 løsning
play wave
sidder og roder lidt med VB 2005 express edition.. prøver at få den til at afspille en wav fil, men det vil ik rigtig lykkes for mig.. de kode-eksempler jeg har, er fra ældre versioner af VB.. og de er derfor åbenbart ikke kompatible med VB 2005.. Kan nogen hjælpe mig med en meget simpel måde at afspille wav filer på? gerne gennem noget API..
Du kunne fx lave en form med en knap: Her er koden til det, og den virker (bare skriv en anden fil hvis du har lyst til det)
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New() MyBase.New()
'This call is required by the Windows Form Designer. InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub
'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(200, 40) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(208, 96) Me.Button1.TabIndex = 0 Me.Button1.Text = "Button1" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(440, 350) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False)
End Sub
#End Region Declare Auto Function sndPlaySound Lib "WINMM.DLL" (ByVal FileName As String, ByVal Options As Int32) As Int32
Const SND_SYNC = &H0 'synchronize playback - 'locks' app until sound finished Const SND_ASYNC = &H1 ' played async - app continues while sound playing Const SND_NODEFAULT = &H2 ' No default sound played if file not found Const SND_LOOP = &H8 ' 'loop the wave Const SND_NOSTOP = &H10 'don't stop current sound if one playing Const SND_NOWAIT = &H2000 'Do not wait if the sound driver is busy. Const SND_ALIAS = &H10000 ' Play a Windows sound (such as SystemStart, Asterisk, etc.).
Public Sub PlayAWave(ByVal Soundfile As String, Optional ByVal MaxLoops As Integer = 1) ' Play a wav file one or more times, as required Dim i As Integer For i = 1 To MaxLoops ' Play Async - ie. Don't lock up the application sndPlaySound("C:\program files\messenger\online.wav", SND_ASYNC And SND_NOSTOP) Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click PlayAWave("C:\program files\messenger\online.wav", 1) End Sub End Class
Det er faktisk kun det her du skal bruge, hvis du opretter knappen visuelt: Declare Auto Function sndPlaySound Lib "WINMM.DLL" (ByVal FileName As String, ByVal Options As Int32) As Int32
Const SND_SYNC = &H0 'synchronize playback - 'locks' app until sound finished Const SND_ASYNC = &H1 ' played async - app continues while sound playing Const SND_NODEFAULT = &H2 ' No default sound played if file not found Const SND_LOOP = &H8 ' 'loop the wave Const SND_NOSTOP = &H10 'don't stop current sound if one playing Const SND_NOWAIT = &H2000 'Do not wait if the sound driver is busy. Const SND_ALIAS = &H10000 ' Play a Windows sound (such as SystemStart, Asterisk, etc.).
Public Sub PlayAWave(ByVal Soundfile As String, Optional ByVal MaxLoops As Integer = 1) ' Play a wav file one or more times, as required Dim i As Integer For i = 1 To MaxLoops ' Play Async - ie. Don't lock up the application sndPlaySound("C:\program files\messenger\online.wav", SND_ASYNC And SND_NOSTOP) Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click PlayAWave("C:\program files\messenger\online.wav", 1) End Sub
jeg kan simpelthen ik få det til at virke.. får diverse fejl hele tiden.. sikker på det virker i VB 2005 express? kan du ik lave det som et projekt og sende det til mig?
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.