Android Question Mixing audiostream

sorex

Expert
Licensed User
Hello,

Did anyone ever succeed to do mixing of 2 (or more) audiostreams?

When I have 2 streams running it first seemed to work fine but after seconds
there is starting to be some delays.

After half a minute it's 10+ seconds already.

I guess it is playing each stream buffer after each other instead of mixing them for sumiltanious play.
 

KZero

Active Member
Licensed User
try to increase the buffered bytes before starting to playback

if the incoming audio packets are encoded try decoding it in a separate thread
 

sorex

Expert
Licensed User
I don't see any option to change the buffer size?

but like I said it seems it's putting the to be played buffers in a queue to be played.
 

KZero

Active Member
Licensed User
I don't see any option to change the buffer size?

but like I said it seems it's putting the to be played buffers in a queue to be played.
you can do it manually by saving the bytes into byte array before writing to the audiostreamer
or you can try AudioTrack Library it has buffersize option
 

sorex

Expert
Licensed User
ok, I figured it out.

It seems to be some kind of Android logic although it makes kind of sense when you think about it.

I had 1 audiostreamer which I changed to 1 for each connection.

This seems to skip the stacking of buffered data that caused the long delays after a few seconds.

So 2 stream players playing different fetched buffers at the same time is what I have now.
 

sorex

Expert
Licensed User
the only thing I can't get working at the moment is multiple listening stream to the broadcasting phone.

when the second listening device connects I get this error

java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.

altho I dim and init it in the new connection sub of the server

is this a limitation of audio record?

B4X:
        Dim astr As AsyncStreams
        astr.InitializePrefix(Null, True, NewSocket.OutputStream, "astream")

        Dim audioStr As AudioStreamer
        audioStr.Initialize("AudioStream", 8000, True,16,audioStr.VOLUME_MUSIC)

        Dim conn As BrConnection
        conn.audiostreamid=audioStr
        conn.asstreamid=astr
        Connections.Put(NewSocket,conn)
     
        Log(audioStr)             
        audioStr.StartRecording   <- error !!!
 

KZero

Active Member
Licensed User
the only thing I can't get working at the moment is multiple listening stream to the broadcasting phone.

when the second listening device connects I get this error

java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.

altho I dim and init it in the new connection sub of the server

is this a limitation of audio record?

B4X:
        Dim astr As AsyncStreams
        astr.InitializePrefix(Null, True, NewSocket.OutputStream, "astream")

        Dim audioStr As AudioStreamer
        audioStr.Initialize("AudioStream", 8000, True,16,audioStr.VOLUME_MUSIC)

        Dim conn As BrConnection
        conn.audiostreamid=audioStr
        conn.asstreamid=astr
        Connections.Put(NewSocket,conn)
    
        Log(audioStr)            
        audioStr.StartRecording   <- error !!!
using different bit-rate and audio format for each initialization may fix it
 

sorex

Expert
Licensed User
@KZero : I tried it like this

B4X:
        Dim audioStr As AudioStreamer
        Try
        audioStr.Initialize("AudioStream", 8000, True,16,audioStr.VOLUME_MUSIC)      
        Log("trying 8000")
        Catch
            Log("8000 failed")
            Try
            Log("trying 11000")          
            audioStr.Initialize("AudioStream", 11000, True,16,audioStr.VOLUME_MUSIC)
            Catch
            Log("11000 failed")
                Try
                audioStr.Initialize("AudioStream", 16000, True,16,audioStr.VOLUME_MUSIC)
                Catch
                Log("16000 failed")
                    Try
                    audioStr.Initialize("AudioStream", 22000, True,16,audioStr.VOLUME_MUSIC)
                    Catch
                    Log("22000 failed")
                        Try
                        audioStr.Initialize("AudioStream", 44100, True,16,audioStr.VOLUME_MUSIC)
                        Catch
                        Log("44100 failed")
                        End Try              
                    End Try
                End Try  
            End Try      
        End Try
the first connection goes through at 8000.

the second one give again that crash with only "trying 8000" in the log, the cath is not even reached?
 
Last edited:

sorex

Expert
Licensed User
ignore that, the crash happends later and my log("trying ...") should've been before that init line :)

back to the code...
 

sorex

Expert
Licensed User
this fails aswell

B4X:
        Select Case Connections.Size
        Case 0
            audioStr.Initialize("AudioStream", 8000, True,16,audioStr.VOLUME_MUSIC)       
        Case 1
            audioStr.Initialize("AudioStream", 44100, True,16,audioStr.VOLUME_MUSIC)       
        End Select
I guess I can only open up 1 recording stream object.
 

sorex

Expert
Licensed User
ok, it seems it has nothing to do with the sockets, asyncstream.

it's pure the definition of 2 audiostreamers that is conflicting somewhere.

I've attached an example which is just a handfull lines of code that reproduces it.
 

Attachments

KZero

Active Member
Licensed User
this code worked normally on my samsung note edge but failed on Lenovo A3000

you should call audioStr.StopRecording before starting the other recorder

why you need to record the same audio using 2 different AudioStreamers ?
 

sorex

Expert
Licensed User
I thought it was a requirement to prevent the delays like in the receiver.

But apparently 1 recorder is enough you just need to send the recorded buffer to the different connected ASstreams.

It works fine now.
 
Top