B4J Question JavaObject InitializeNewInstance problem

peter01

Member
Licensed User
Longtime User
Hi,

I have a problem with JavaObject. I construct a new info object out of two existing objects. In Java the equivalent code is working, but my B4J code is not.
In Java the line is:
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat);
audioFormat is an object with the audio format of my file, and SourceDataLine.class is the object describing lines for playback audio.
In B4J I have:
Dim AFInfo As JavaObject AFInfo = Asys.RunMethod("getAudioFileFormat",Array(F)) Log("### AFInfo: " & AFInfo) Dim Class1 As JavaObject Class1.InitializeStatic("java.lang.Class") Dim TLClass As Object = Class1.RunMethod("forName",Array("javax.sound.sampled.SourceDataLine")) Log("### TLClass: " & TLClass) Dim DLInfo As JavaObject (50:)-> DLInfo.InitializeNewInstance("javax.sound.sampled.DataLine.Info",Array(TLClass,AFInfo))
The last line throws the error. Part of the log is here:

### AFInfo: (WaveFileFormat) WAVE (.wav) file, byte length: 176624, data format: PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian, frame length: 44100
### TLClass: interface javax.sound.sampled.SourceDataLine
Error occurred on line: 50 (Main)
java.lang.RuntimeException: Constructor not found.

But on JavaSE8 one valid constructor for DataLine.Info is:
public DataLine.Info(Class<?> lineClass,
AudioFormat format)
Parameters:lineClass - the class of the data line described by the info object , format - desired format

What is wrong????

Thanks for help

Peter
 

stevel05

Expert
Licensed User
Longtime User
From a look at the documentation, Asys.getAudioFileFormat returns an AudioFileFormat object, whereas the constructor for DataLine.Info requires an AudioFormat Object.

This should do it:
B4X:
    Dim ASys As JavaObject

    ASys.InitializeStatic("javax.sound.sampled.AudioSystem")
    Dim F As JavaObject

    F.InitializeNewInstance("java.io.File",Array(File.DirTemp,"1.wav"))

    Dim AFInfo As JavaObject
    AFInfo = ASys.RunMethodJO("getAudioFileFormat",Array(F)).RunMethod("getFormat",Null)

    Log("### AFInfo: " & AFInfo)
    Dim Class1 As JavaObject
    Class1.InitializeStatic("java.lang.Class")
    Dim TLClass As Object = Class1.RunMethod("forName",Array("javax.sound.sampled.SourceDataLine"))
    Log("### TLClass: " & TLClass)
    Log(GetType(TLClass))

    Dim DLInfo As JavaObject
    DLInfo.InitializeNewInstance("javax.sound.sampled.DataLine.Info",Array(TLClass,AFInfo))
 
Last edited:
Upvote 0

peter01

Member
Licensed User
Longtime User
Hi Steve,

with your help my B4J app is now working - the same principle as your AudioSystem app but for Audiotracks instead of Clips. If you are interested in the file I can post the zip here, otherwise many thanks.

Peter
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Hi Peter, Yes please post the result. It could save some head scratching in the future.

Thanks
 
Upvote 0
Top