B4J Question Plays the audio using PC speakers via SourceDataLine

invocker

Active Member
Hi everybody,
Can any one help me to Plays the audio using PC speakers via SourceDataLine and not headphones

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
 

stevel05

Expert
Licensed User
Longtime User
As I offered in the other thread, zip and post your code (B4a and B4j projects) and I'll try to help. Otherwise it would take me an hour or more to recreate your projects and I don't know exactly what they are doing.
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
After a quick look, the Android app wouldn't start for me, I had to add a call to check permissions in activity resume.

After that the main reason you were hearing white noise is that you were passing the whole message, and not just the data portion in the B4j App Astream_NewData.

Change playaudio.SendDataPlayer(Buffer) to playaudio.SendDataPlayer(info.data) and you will hear the proper signal.

There is a residual echo, which is probably caused by the java code merging previous data, I've not needed to use that before so can't comment on it's applicability.

This should get you a bit closer.
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Oh, I also should have said that I moved StartAudioPlayer from AStream_NewData, to the ListenForConnections sub. Just feels like it should be there, may or may not affect the result.
 
Upvote 0
Top