B4J Question Beep on raspberry

micro

Well-Known Member
Licensed User
Longtime User
I use a shell with aplay app inside in raspebrry and a clip audio for simulate a beep
B4X:
Dim js As  Shell
js.Initialize("", "aplay", Array As String(File.DirApp & "/Audio/" & clip))
js.Run(-1)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this code:
B4X:
Sub PlayFile(Dir As String, FileName As String)
   Dim audioSystem As JavaObject
   audioSystem.InitializeStatic("javax.sound.sampled.AudioSystem")
   Dim f As JavaObject
   f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
   Dim in As JavaObject = audioSystem.RunMethod("getAudioInputStream", Array(f))
   Dim clip As JavaObject = audioSystem.RunMethod("getClip", Null)
   clip.RunMethod("open", Array(in))
   clip.RunMethod("start", Null)
   in.RunMethod("close", Null)
End Sub
It works on the PC. I haven't tried it on the RPi.
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
Thanks

work fine both solutions

is it possible put the audio file in Project folder (File)?

i have try to set DirAssets and DirAp, but doen't find the file.
i have also try to copy in temp dir
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
Try this code:
B4X:
Sub PlayFile(Dir As String, FileName As String)
   Dim audioSystem As JavaObject
   audioSystem.InitializeStatic("javax.sound.sampled.AudioSystem")
   Dim f As JavaObject
   f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
   Dim in As JavaObject = audioSystem.RunMethod("getAudioInputStream", Array(f))
   Dim clip As JavaObject = audioSystem.RunMethod("getClip", Null)
   clip.RunMethod("open", Array(in))
   clip.RunMethod("start", Null)
   in.RunMethod("close", Null)
End Sub
It works on the PC. I haven't tried it on the RPi.

after 7-8 times, return this error

B4X:
Error occurred on line: 113 (Main)
java.lang.reflect.InvocationTargetException
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
   at b4j.example.main._playfile(main.java:171)
   at b4j.example.main._timersimul_tick(main.java:256)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:607)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
   at sun.reflect.GeneratedMethodAcc
essor1.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
   at anywheresoftware.b4a.objects.Timer$TickTack$1.run(Timer.java:118)
   at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
   at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
   at anywheresoftware.b4a.ShellBA.startMessageLoop(ShellBA.java:111)
   at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:131)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:300)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImp
l.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
   at b4j.example.main.main(main.java:29)
Caused by: java.lang.IllegalArgumentException: No line matching interface Clip supporting format PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian is supported.
   at javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:479)
   at javax.sound.sampled.AudioSystem.getClip(AudioSystem.java:522)
   ... 34 more
 
Upvote 0

Similar Threads

Replies
10
Views
2K
Replies
3
Views
2K
  • Question
B4J Question Beep?
Replies
2
Views
3K
Top