B4J Question Play streaming audio with b4j

invocker

Active Member
I use this module hier playDataAudio to play data received from b4a that use AudioStreamer from b4j

B4X:
Private Sub Astream_NewData (Buffer() As Byte)

log("data arrived")

playaudio.SendDataPlayer(Buffer)

B4X:
Sub Class_Globals
    Private nativeMe As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize

    nativeMe = Me
    nativeMe.RunMethod( "initialaudio", Null)
End Sub

Public Sub StartAudioPlayer
    nativeMe.RunMethod( "startaudio", Null)
End Sub

Public Sub StopAudioPlayer
    nativeMe.RunMethod( "stopaudio", Null)
End Sub

Public Sub SendDataPlayer (data() As Byte)
    
    nativeMe.RunMethod( "playaudio", Array(data,data.Length))
End Sub


#IF JAVA
import javax.sound.sampled.*;

    SourceDataLine _speaker;

    public void initialaudio() throws LineUnavailableException{
        //  specifying the audio format
        AudioFormat _format = new AudioFormat(22050.F,// Sample Rate
                16,     // Size of SampleBits
                1,      // Number of Channels
                true,   // Is Signed?
                false   // Is Big Endian?
        );

        //  creating the DataLine Info For the speaker format
        DataLine.Info speakerInfo = new DataLine.Info(SourceDataLine.class, _format);

        //  getting the mixer For the speaker
        _speaker = (SourceDataLine) AudioSystem.getLine(speakerInfo);
        _speaker.open(_format);
    }

    public void startaudio() {
        try {
            _speaker.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }       
    public void playaudio(byte[] data, int readCount) {
        try {
            if(readCount > 0){
                    _speaker.write(data, 0, readCount);
             }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
        
    public void stopaudio() {
        try {
            _speaker.drain();
            _speaker.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


#End If

but not work for me
 

Attachments

  • PlayDataAudio.bas
    1.9 KB · Views: 19

stevel05

Expert
Licensed User
Longtime User
Do you receive any data?: log("data arrived")

Also, make sure that the Format (SampleRate, Sample size, Number of channels, Signed and BigEndian) settings Match on both ends, and that your PC sound card can handle 22050, 16 bit Mono samples.

If that doesn't help, you'll need to zip and post the Android Project and the B4j Project so that they can be tested. Without any errors it's impossible to provide an accurate response.
 
Upvote 0

invocker

Active Member
the first problem now resolved in b4a
B4X:
streamer.Initialize("streamer", 44100, True, 16, streamer.VOLUME_MUSIC)
and in b4j module first it like this
B4X:
AudioFormat _format = new AudioFormat(22050.F,// Sample Rate
                16,     // Size of SampleBits
                1,      // Number of Channels
                true,   // Is Signed?
                false   // Is Big Endian?
        );
I change it like this
B4X:
 AudioFormat _format =new AudioFormat(44100.F,  // Sample Rate (must match sender)
                                    16,       // Sample Size in bits
                                    1,        // Channels (1=mono, 2=stereo)
                                    true,     // Signed
                                    false);    // Little Endian
now i hear correpted sound chchchchchchchchchc 😄
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Android natively uses LittleEndian, but try changing that to true (big endian) in the AudioFormat definition. It may be that the AudioStreamer changes it. Or if you are sending an audio file, worth a try.
 
Last edited:
Upvote 0

invocker

Active Member
I am working with audio streaming between devices using B4X (B4A and B4J), with this project from erel Erel Project and I encountering a "zzzzzz" sound issue when trying to play the streamed audio in B4J without storing it.
Based on the AudioLibrary v1.5 thread need a real-time audio streaming between B4A and B4J if you can do that for me please
 
Upvote 0

invocker

Active Member
I test with jAudioStreamer library but get error (NullPointerException) java.lang.NullPointerException: Cannot invoke "java.lang.Thread.interrupt()" because "this.playThread" is null my b4j code is
B4X:
Private AudioS As AudioStreamer
Sub AudioS_DataReceived(buffer() As Byte)
    If astream.IsInitialized Then
        astream.Write(buffer)
        
        Log("write buffer" )
    End If
End Sub
Sub AudioS_Error(msg As String)
    Log(msg)
End Sub

Sub AudioS_PlaybackComplete
    Log("Playback Complete")
End Sub
Private Sub Astream_NewData (Buffer() As Byte)
Case "AUD"
                
'                      buffers.Add(Buffer)
                AudioS_DataReceived(Buffer)
'                If AudioS.IsInitialized Then AudioS.writeData(Buffer)
'                PlayerBufferSize 3776
                AudioS.RecorderBufferSize =3776 ' 44100
                
                AudioS.startPlayer
the b4a send PlayerBufferSize 3776
 
Upvote 0

invocker

Active Member
i forget to initialize audio
B4X:
Dim sampleRate As Int = 44100 ' or 48000
    Dim channels As Int = 1 ' 1 for mono, 2 for stereo
    Dim bufferSize As Int = 1024 ' Typical buffer size

    If AudioS.Initialize("AudioS", 0, 0, sampleRate, 16, channels, bufferSize, bufferSize) Then
        Log("Audio Streamer successfully initialized")
    Else
        Log("Cannot initialize Audio Streamer")
        Return
    End If
now i get error not java.lang.IllegalArgumentException: Line unsupported: interface TargetDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian
 
Upvote 0

invocker

Active Member
i resolve the error like this
B4X:
If AudioS.Initialize("AudioS", 6, 3, 8000, 16, 1, 512, 512) Then' Match sample rate & channels Then
        Log("Audio Streamer successfully initialized")
    Else
        Log("Cannot initialize Audio Streamer")
        Return
    End If  ' 6 for primary sound capture driver  or 7 for microphone
but still not hear any sound
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User

1754417669562.png
 
Upvote 0
Top