How to play MP3 from VB.NET

0

Posted on : 13-01-2011 | By : ocx | In : Develop
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Please note: this how-to article refer to software developers and software authors.

This article will show you how to play MP3 and more sound files using the MP3 Player ActiveX and VB.NET.

Requirements:

This sample using VB.NET. You can find more samples under the installation program of the component.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
 
    Private Sub cmdPick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPick.Click
 
        OpenAudioFile()
 
    End Sub
 
    Private Sub OpenAudioFile()
 
        'Select the source audio file

        OpenFileDialog1.ShowDialog()
 
        If Trim(OpenFileDialog1.FileName)  "" Then
            txtAudioFile.Text = OpenFileDialog1.FileName
        Else
            Exit Sub
        End If
 
 
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        'Init the control:
        'Must be call before using any
        'functionality of the control.
        '
        'sKey = Your registration key.
        'On trial mode use the "Trial Mode."
        '
        AxcPlayer1.Init("Trial Mode.")
 
    End Sub
 
    Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
 
        'Release the control
        'from the memory:
        AxcPlayer1.DeInit()
 
    End Sub
 
    Private Sub cmdPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlay.Click
 
        'Play

        If txtAudioFile.Text = "" Then
            OpenAudioFile()
            If txtAudioFile.Text = "" Then
                Exit Sub
            End If
        End If
 
        'Open the audio file
        AxcPlayer1.OpenMedia(txtAudioFile.Text)
 
        'Get the length of the media
        sld.Minimum = 0
        sld.Maximum = AxcPlayer1.MediaLength
 
        'Display the total time
        lblTotal.Text = AxcPlayer1.SecondsAsTimeString(AxcPlayer1.MediaLength / 1000)
 
        'Play the audio file
        AxcPlayer1.MediaPlay()
 
        'Enable the timer
        Timer1.Enabled = True
 
    End Sub
 
    Private Sub cmdPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPause.Click
 
        'Pause

        AxcPlayer1.MediaPause()
 
    End Sub
 
    Private Sub cmdUnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUnPause.Click
 
        'Unpause

        AxcPlayer1.MediaPlay()
 
    End Sub
 
    Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
 
        'Stop play

        Timer1.Enabled = False
        AxcPlayer1.MediaStop()
        sld.Value = 0
 
    End Sub
 
    Private Sub sld_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sld.Scroll
 
        'Seek to a new position

        Dim x As Integer
        x = sld.Value
        AxcPlayer1.MediaSeek(x)
 
    End Sub
 
 
    Private Sub sld_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sld.MouseDown
        Timer1.Enabled = False
    End Sub
 
    Private Sub sld_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sld.MouseUp
        Timer1.Enabled = True
    End Sub
 
 
    '******************************************************************
    'EVENTS
    '******************************************************************

    Private Sub AxcPlayer1_OpenMediaEvent(ByVal sender As Object, ByVal e As AxMP3_Player_ActiveX.__cPlayer_OpenMediaEvent) Handles AxcPlayer1.OpenMediaEvent
 
        'The audio file has been opened
        txtStatus.Text = txtStatus.Text & Now & ":" & "Open Media:" & e.sMediaFile & vbCrLf
 
    End Sub
 
    Private Sub AxcPlayer1_Pause(ByVal sender As Object, ByVal e As System.EventArgs) Handles AxcPlayer1.Pause
 
        'Pause play
        txtStatus.Text = txtStatus.Text & Now & ":" & "Pause" & vbCrLf
 
    End Sub
 
    Private Sub AxcPlayer1_Play(ByVal sender As Object, ByVal e As System.EventArgs) Handles AxcPlayer1.Play
 
        'Start play
        txtStatus.Text = txtStatus.Text & Now & ":" & "Play" & vbCrLf
 
    End Sub
 
    Private Sub AxcPlayer1_StopPlay(ByVal sender As Object, ByVal e As System.EventArgs) Handles AxcPlayer1.StopPlay
 
        'Stop play
        txtStatus.Text = txtStatus.Text & Now & ":" & "Stop Play" & vbCrLf
 
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
 
        'Move the slider:
        sld.Value = AxcPlayer1.MediaPosition
        lblPos.Text = AxcPlayer1.SecondsAsTimeString(AxcPlayer1.MediaPosition / 1000)
 
    End Sub

ocx

Hello All! I'm the author of ocx-active.com. My website contains many activex components for software developers and software authors. I've decide to put at the 3howto.com some samples of using the components of my website. Enjoy it :)

Post a comment

You must be logged in to post a comment.