Tipp 0003 Wav-Datei mit API abspielen
Autor/Einsender:
Datum:
  Michael Werner
14.01.2003
Entwicklungsumgebung:   VB.Net 2002
Framework:   1.0
Dieser Tipp zeigt, wie mit Hilfe des Schlüsselworts DllImport eine API-Funktion aufgerufen werden kann. DllImport benötigt den Namespace System.Runtime.InteropServices.
Mit Hilfe der API sndPlaySound kann eine wav-Datei als Loop oder einmal abgespielt werden. Das eingebundene Steuerelement OpenFileDialog ermöglicht eine freie Auswahl der Sounddatei.
 
Imports System.Runtime.InteropServices
Imports System.IO
 
Public Class Form1
  Inherits System.Windows.Forms.Form
Vom Windows Form Designer generierter Code
  <DllImport("winmm.dll", EntryPoint:="sndPlaySoundA")> _
  Public Shared Function PlayWav (ByVal lpszName As String, _
         ByVal dwFlags As Integer) As Integer
  End Function
 
  Private Const SND_ASYNC = &H1
  Private Const SND_LOOP = &H8
  Private Const SND_MEMORY = &H4
 
  Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click

    Dim nRet As Integer
    If TextBox1.Text = "" Then
      SoundDateiWaehlen()
      If TextBox1.Text = "" Then Exit Sub
    End If
 
    Dim fi As New FileInfo(TextBox1.Text)
    If fi.Exists = False Then
      MsgBox("Falsche Pfadangabe" & vbNewLine & "Wählen Sie " & _
             "eine Wav-Datei, um Sound abspielen zu können!", _
             MsgBoxStyle.Exclamation, "Falsche Pfadangabe")
      SoundDateiWaehlen()
      If fi.Exists = False Then Exit Sub
    End If
 
    nRet = PlayWav(TextBox1.Text, SND_ASYNC)
  End Sub
 
  Private Sub Button2_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button2.Click

    Dim nRet As Integer
    If TextBox1.Text = "" Then
      SoundDateiWaehlen()
      If TextBox1.Text = "" Then Exit Sub
    End If
 
    Dim fi As New FileInfo(TextBox1.Text)
    If fi.Exists = False Then
      MsgBox("Falsche Pfadangabe" & vbNewLine & "Wählen Sie " & _
             "eine Wav-Datei, um Sound abspielen zu können!", _
             MsgBoxStyle.Exclamation, "Falsche Pfadangabe")
      SoundDateiWaehlen()
      If fi.Exists = False Then Exit Sub
    End If
 
    nRet = PlayWav(TextBox1.Text, SND_LOOP Or SND_ASYNC)
  End Sub
 
  Private Sub Button3_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button3.Click

    Dim nRet As Integer
    nRet = PlayWav(TextBox1.Text, _
               SND_LOOP Or SND_ASYNC Or SND_MEMORY)
  End Sub
 
  Private Sub Button4_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button4.Click
    Me.Close()
  End Sub
 
  Private Sub Button5_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button5.Click
    SoundDateiWaehlen()
  End Sub
 
  Sub SoundDateiWaehlen()
    With OpenFileDialog1
      Dim sPath As String
 
      sPath = Environment.SystemDirectory.Substring( _
              0, Environment.SystemDirectory.LastIndexOf("\")) & _
              "\Media"
      .InitialDirectory = sPath
      .Title = "Wählen Sie eine Wav-Datei"
      .Filter = "Wav-Dateien (*.wav)|*.wav"
      .RestoreDirectory = True
 
      If .ShowDialog() = DialogResult.OK Then
        TextBox1.Text = .FileName
      End If
    End With
  End Sub
End Class
 
Weitere Links zum Thema
Winamp fernsteuern

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (6,9 kB) Downloads bisher: [ 1276 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Samstag, 21. Januar 2012