B4J Question JavaxMidi-B4xLib for B4j

Luca1967

Active Member
Hi,

I am trying to use this nice library but I am having problems
1) I can't read the instruments in the tracks
2) I can't mute a channel
3) Are there any explanations or examples online? If so, where can I get them?
Thank you
 

stevel05

Expert
Licensed User
Longtime User
Not with MidiSystemStatic.GetSequence, but download the latest library and you can use either of these.

B4X:
Seq = MidiSystemStatic.GetSequence3(File.GetUri(File.DirAssets,FileName))
Seqr.SetSequence2(Seq)

Or

B4X:
Dim Inp As InputStream = File.OpenInput(File.DirAssets,FileName)
Seq = MidiSystemStatic.GetSequence2(Inp)
Seqr.SetSequence2(Seq)

I have only modified MidiSystemStatic GetSequence2 and GetSequence3 if you still have the library modules in your project you can copy and replace just those subs.
 
Last edited:
Upvote 0

Luca1967

Active Member
Not with MidiSystemStatic.GetSequence, but download the latest library and you can use either of these.

B4X:
Seq = MidiSystemStatic.GetSequence3(File.GetUri(File.DirAssets,FileName))
Seqr.SetSequence2(Seq)

Or

B4X:
Dim Inp As InputStream = File.OpenInput(File.DirAssets,FileName)
Seq = MidiSystemStatic.GetSequence2(Inp)
Seqr.SetSequence2(Seq)

I have only modified MidiSystemStatic GetSequence2 and GetSequence3 if you just want to copy those subs.
Thanks
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
you should also fix the "getformat"
Do you mean MidiSystemStatic.GetMidiFileFormat?

GetMidiFileFormat2 is working, It works from a directory on the disk. but it can't read files from the Assets folder either, it appears it may require write permissions the Assets directory is read only. See the documentation here: https://docs.oracle.com/javase/8/do...m.html#getMidiFileFormat-java.io.InputStream-:

GetMidiFileFormat3 is fixed for the next release and works with the Assets directory, if you want to drop it in the code is here:

B4X:
'Obtains the MIDI file format of the data in the specified URL.
'Pass a FileUri obtained with File.GetUri(Dir, FileName)
Public Sub GetMidiFileFormat3(Url As String) As MidiFileFormat
    Dim MidiSystem1 As JavaObject
    MidiSystem1.InitializeStatic("javax.sound.midi.MidiSystem")
    Dim jURL As JavaObject
    jURL.InitializeNewInstance("java.net.URL",Array(Url))
    Dim Wrapper As MidiFileFormat
    Wrapper.Initialize
    Wrapper.SetObject(MidiSystem1.RunMethod("getMidiFileFormat",Array As Object(jURL)))
    Return Wrapper
End Sub

Pass a FileUri obtained with File.GetUri(Dir, FileName)
 
Last edited:
Upvote 0

drponciano

Member
Licensed User
Longtime User
Do you mean MidiSystemStatic.GetMidiFileFormat?

GetMidiFileFormat2 is working, It works from a directory on the disk. but it can't read files from the Assets folder either, it appears it may require write permissions the Assets directory is read only. See the documentation here: https://docs.oracle.com/javase/8/do...m.html#getMidiFileFormat-java.io.InputStream-:

GetMidiFileFormat3 is fixed for the next release and works with the Assets directory, if you want to drop it in the code is here:

B4X:
'Obtains the MIDI file format of the data in the specified URL.
'Pass a FileUri obtained with File.GetUri(Dir, FileName)
Public Sub GetMidiFileFormat3(Url As String) As MidiFileFormat
    Dim MidiSystem1 As JavaObject
    MidiSystem1.InitializeStatic("javax.sound.midi.MidiSystem")
    Dim jURL As JavaObject
    jURL.InitializeNewInstance("java.net.URL",Array(Url))
    Dim Wrapper As MidiFileFormat
    Wrapper.Initialize
    Wrapper.SetObject(MidiSystem1.RunMethod("getMidiFileFormat",Array As Object(jURL)))
    Return Wrapper
End Sub

Pass a FileUri obtained with File.GetUri(Dir, FileName)
Hello, I just upgraded to B4J 10.0 but my program does not work any more. I get the following error when:

Antes de iniciar Midi
midi_gm._add (java line: 544)
java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextNode(Unknown Source)
at java.util.LinkedHashMap$LinkedKeyIterator.next(Unknown Source)
during
Log("Antes de iniciar Midi")
' Buscamos los dispositivos que están conectados.
MidiSystemStatic.Initialize
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I'm not at my computer until tomorrow but can you post your project or the code that causes this issue? I will have a look at it then.
 
Upvote 0

drponciano

Member
Licensed User
Longtime User
I'm not at my computer until tomorrow but can you post your project or the code that causes this issue? I will have a look at it then.
I did not have time to try again as I'm using the 9.8 code but now I want to enhance the app with latest version of B4J.

This is the offending code:

MIDI:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Midi_JianPu")
    MainForm.Show

    ' Tamaño de la ventana
    MainForm.WindowHeight=fx.PrimaryScreen.Maxy-20dip' 600
    MainForm.WindowWidth=fx.PrimaryScreen.MaxX-20dip'1024
    MainForm.WindowLeft=0
    MainForm.WindowTop=0
    
    Log("Antes de iniciar Midi")
    ' Buscamos los dispositivos que están conectados.
    MidiSystemStatic.Initialize
    'Dim devices As List = MidiSystemStatic.GetMidiDeviceInfo
    Log("Antes de device info")
    devices= MidiSystemStatic.GetMidiDeviceInfo

The problem seems to be in the MidiSystemStatic.Initialize statement.

When it is compiled with B4J 9.8 I get the following log output:
Antes de iniciar Midi
Antes de device info
Hay 4 dispositivos
Nombre: Gervill
Nombre: Microsoft MIDI Mapper
Nombre: Microsoft GS Wavetable Synth
Nombre: Real Time Sequencer
Leido archivo INI

----------------------------------------------------------------

But compiled with B4J 10.0:

Antes de iniciar Midi
midi_gm._add (java line: 544)
java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:719)
at java.util.LinkedHashMap$LinkedKeyIterator.next(LinkedHashMap.java:742)
at anywheresoftware.b4a.objects.collections.Map$IterableMap.Get(Map.java:186)
at b4j.example.midi_gm._add(midi_gm.java:544)
at b4j.example.midi_gm._initialize(midi_gm.java:720)
at b4j.example.midisystemstatic._initialize(midisystemstatic.java:399)
at b4j.example.main._appstart(main.java:128)
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:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at b4j.example.main.start(main.java:37)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
at java.lang.Thread.run(Thread.java:748)

Thanks
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It's a long time since I looked at this, but I have just uploaded my latest version which has a few changes. Can you download the new version from https://www.b4x.com/android/forum/threads/b4j-javax-midi-lib.113466/#content. Which seems to fix this issue.

Let me know if there are any other issues with it as I can't remember exactly what was changed, I don't think there are any major or breaking changes or I would probably have noted them.
 
Upvote 0

drponciano

Member
Licensed User
Longtime User
It's a long time since I looked at this, but I have just uploaded my latest version which has a few changes. Can you download the new version from https://www.b4x.com/android/forum/threads/b4j-javax-midi-lib.113466/#content. Which seems to fix this issue.

Let me know if there are any other issues with it as I can't remember exactly what was changed, I don't think there are any major or breaking changes or I would probably have noted them.
That worked! I was using version 1.7. Thanks ??
 
Upvote 0
Top