Der Pc-Blog ist umgezogen. Du findest ihn ab jetzt unter www.meinpc-blog.de

Sonntag, 13. März 2011

Visual Basic Tutorialreihe Teil 9 - Musikplayer mit Playlist

In diesem Teil meiner Visual Basic Tutorialreihe zeige ich euch, wie ihr unseren Musikplayer um eine Playlist ergänzt. Außerdem lernt ihr Funktionen besser kennen, was ein Timer ist und wofür die restlichen Parameter bei mciSendString sind.



Quellcode:

Imports System.IO

Public Class Form1
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpszCommand As String, ByVal lpszReturnString As String, ByVal cchReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

    Dim IsPlaying As Boolean = False
    Dim currentTime As Double = 0
    Dim currentlength As Integer = 0

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Not IsPlaying Then
            'Datei abspielen
            PlayDatei()
        Else
            'Pause
            mciSendString("pause Datei", Nothing, 0, 0)
            IsPlaying = False
            Button1.BackgroundImage = ImageList1.Images(0)
            Timer.Stop()
        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Stop
        If IsPlaying Then
            mciSendString("stop Datei", Nothing, 0, 0)
            mciSendString("close Datei", Nothing, 0, 0)
            Button1.BackgroundImage = ImageList1.Images(0)
            Timer.Stop()
        End If
    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        OpenFileDialog1.ShowDialog()

    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        ListBox1.Items.Add(OpenFileDialog1.FileName)
        GroupBox1.Enabled = True
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        Catch ex As Exception
        End Try

        If ListBox1.Items.Count = 0 Then
            GroupBox1.Enabled = False
            mciSendString("stop Datei", Nothing, 0, 0)
            Button1.BackgroundImage = ImageList1.Images(0)
        End If
    End Sub

    Private Function ErmitteleLaenge()

        If IsPlaying Then
            Dim laenge As Double
            Dim sReturn As String = Space$(256)
            mciSendString("status Datei length", sReturn, Len(sReturn), 0)
            laenge = Convert.ToDouble(Val(sReturn) / 1000)
            Return laenge
        Else
            Return 0
        End If

    End Function

    Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
        If currentTime < currentlength Then
            currentTime += 1
            ToolStripStatusLabel2.Text = currentTime
        Else
            Timer.Stop()
            If ListBox1.Items.Count - 1 <> ListBox1.SelectedIndex Then
                mciSendString("stop Datei", Nothing, 0, 0)
                mciSendString("close Datei", Nothing, 0, 0)
                IsPlaying = False
                ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
                PlayDatei()
            Else
                Button1.BackgroundImage = ImageList1.Images(0)
            End If
        End If

    End Sub

    Private Sub PlayDatei()
        If IsPlaying Then
            mciSendString("stop Datei", Nothing, 0, 0)
            mciSendString("close Datei", Nothing, 0, 0)
            IsPlaying = False
        End If
        mciSendString("open " & ListBox1.SelectedItem.ToString & " alias Datei", Nothing, 0, 0)
        mciSendString("play Datei", Nothing, 0, 0)
        IsPlaying = True
        Button1.BackgroundImage = ImageList1.Images(1)
        currentTime = 0
        currentlength = ErmitteleLaenge()
        Timer.Start()
        ToolStripStatusLabel1.Text = ListBox1.SelectedItem

    End Sub


    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        PlayDatei()
    End Sub
End Class

Keine Kommentare:

Kommentar veröffentlichen