Unfortunately
getAudioInputStream only plays WAV and PCM files.
How to play an MP3, then?
Here is one possible solution using theh javalayer library
You can get the library here:
http://www.javazoom.net/javalayer/javalayer.html
1) Create a simple form like this:
[img]http://www.jabaco.org/board/index.php?page=Attachment&attachmentID=181&h=50316b49d4584fe293312feeb0d895ff448d3a57[/img]
2) Press F1 to add a classptah
- Press [Add Jar Archive] to import
jl1.0.1.jar.
- open the tree structure and check at least the player class
3)
1st attempt - Use the following code
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Import java.io
Import javazoom.jl.player
Public Sub Command1_Click() ' Start button
' use some mp3-file existing on your system !
Dim f As New FileInputStream("C:\Users\Public\Music\Sample Music\Kalimba.mp3")
p = New Player(f)
p.play
End Sub
Public Sub Command2_Click()
p.close
end Sub
|
Now, the [Start] button should work as desired, the [Stop] button wo'nt:
As the application is
single thereaded, the click event for the stop button
will not be handeld unless the music is over, anyway.
3)
2st attempt - use threading
- create a class module called
clsPlayer using the following code
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Implements java#lang#Runnable
Import java.io
Import javazoom.jl.player
Dim p As Player
Public Sub run()
' use some mp3-file existing on your system !
Dim f As New FileInputStream("C:\Users\Public\Music\Sample Music\Kalimba.mp3")
p = New Player(f)
p.play
End Sub
Public Sub StopPlayer
p.close
End Sub
' Constructor
Public Sub clsPlayer
End Sub
|
The
Implements java#lang#Runnable statement is necessary to ude clsPlayer as a thread.
- now change the Form1 code like this
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
|
Dim th As Thread
Dim pl As New clsPlayer
Public Sub Command1_Click()
th = New Thread(pl)
Call th.start
End Sub
Public Sub Command2_Click()
pl.StopPlayer
End Sub
|
Now the player class ist started a a background thread. The [Stop] button's click event
will now fire and stop the playback as desired.
Further reading:
http://www.cs.princeton.edu/introcs/faq/mp3/mp3.html
http://www.cs.princeton.edu/introcs/faq/mp3/MP3.java.html
http://www.cs.princeton.edu/introcs/faq/mp3/MP3.java.html (icl. I2D-tags, German)
Alternative for library for MP3 playing
javax.sound.sampled
http://www.javaworld.com/javaworld/jw-11…w-1103-mp3.html