You are not logged in.

OlimilO

Intermediate

  • "OlimilO" is male
  • "OlimilO" started this thread

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

1

Monday, October 12th 2009, 5:23pm

Playing Sound tutorial

How to play sound with Jabaco? It's time for my tiny little sound tutorial I wrote months ago. It's really easy...

Jabaco Source

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
Import java#io 

Dim WinMedia As String 

Public Sub Form_Load() 
' 
' we fetch the windows-directory from the system environment 
' at the beginning, because we may need it often 
' 
WinMedia = system.getenv("windir") & "\Media"  '"
End Sub 

Public Sub Command1_Click() 
' 
' Playback a wave-soundfile 
' ==================== 
' 

' 
' we define some variables 
' 
Dim wavPath As String 
Dim wavFile As File ' in Jabaco the namespace java#io is already imported 
Dim ais As AudioInputStream 'from: javax#sound#sampled 
Dim wavClip As Clip 

' 
' we select the tada-sound and store it into the file path, 
' (normally everybody has this sound in the windows-folder) 
' 
wavPath = WinMedia & "tada.wav" 

' 
' we are making a file-object from the path, so we can get an input stream 
' 
wavFile = New File(wavPath) 

' 
' we use the AudioSystem-Object, that is already existing (singleton) to 
' obtain an input stream from the file 
' 
ais = AudioSystem.getAudioInputStream(wavFile) 

' 
' again we use the AudioSystem-Object that makes a Clip for us 
' 
wavClip = AudioSystem.getClip 

' 
' with the clip we can open the input stream and read it 
' 
wavClip.open(ais) 

' 
' after it is opened it finally can be played 
' 
wavClip.loop(LOOP_CONTINUOUSLY) 
wavClip.start 

End Sub



OlimilO

OlimilO

Intermediate

  • "OlimilO" is male
  • "OlimilO" started this thread

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

2

Monday, October 12th 2009, 5:35pm

even much shorter

Of course we can shorten this a little bit

Jabaco Source

1
2
3
4
5
6
7
8
9
Public Sub Command2_Click()
   Dim wavClip As Clip = AudioSystem.getClip
   
   wavClip.open(AudioSystem.getAudioInputStream( _
      New File("C:\Windows\Media\chimes.wav")))
   
   wavClip.start
   
End Sub

OlimilO

Intermediate

  • "OlimilO" is male
  • "OlimilO" started this thread

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

3

Monday, October 12th 2009, 5:37pm

We can make a procedure out of this, that simply plays a wave-file. Maybe this is suitable if you want to make a simple sound or noise in case the user of your program makes some silly things.

Jabaco Source

1
2
3
4
5
Public Sub PlayWavFile(wavPath As String)
   Dim wavClip As Clip = AudioSystem.getClip
   wavClip.open(AudioSystem.getAudioInputStream(New File(wavPath)))
   wavClip.start
End Sub

axtens

Trainee

  • "axtens" is male

Posts: 37

Date of registration: Mar 16th 2009

Location: Perth, WA, Australia

Occupation: Software/Test Engineer

Hobbies: be a husband and a dad, play ukulele, sing

  • Send private message

4

Monday, October 12th 2009, 5:40pm

Brilliant!! Thanks very much indeed. Vielen Dank! and all that.

NoLars

Beginner

  • "NoLars" is male

Posts: 2

Date of registration: Feb 4th 2010

Location: Germany

Occupation: Developer, IT-Trainer

  • Send private message

5

Thursday, February 4th 2010, 4:55pm

Simple MP3 player with threading

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

This post has been edited 1 times, last edit by "NoLars" (Feb 4th 2010, 5:21pm)


Rate this thread
WoltLab Burning Board