0

I have a byte array from a socket, client (app) receives those bytes, I need a way to play those bytes on android. I have tested PyAudio (works wonders when it's installed properly, on linux.) But I can't use PyAudio on android. (I'm using Kivy to build my app)

are there some tools to play audio in byte format on android?

JareBear
  • 467
  • 9
  • 27

2 Answers2

1

you can use this system i made, it works with url,bytes,bytes stream and local file

class Audio:
    _total_audio_instances=[]
    
    def main_audio_init():
        import os
        try:
            from jnius import autoclass 
        except:
            os.system("pip install jnius")
            from jnius import autoclass
        try:
            import threading 
        except:
            os.system("pip install threading")
            
        
        Audio._autoclass=autoclass
        Audio._threading=threading
        
    def close_all_audio_instances():
        for inst in Audio._total_audio_instances:
            inst.stop()
            inst.release()
    
    def __init__(self,source:str,looping:bool=False):
        self._class=Audio._autoclass('android.media.MediaPlayer')()
        self._class.setDataSource(source)
        self._class.prepare()
        self._busy=0
        self._position=self.get_duration()
        self._class.setLooping(looping)
        Audio._total_audio_instances.append(self._class)
                
    def help(self):
        h="""
Audio system_help:

init args:
    <source>
        -path
        -url
        -bytes
    <looping>
        -True
        -False

commands:
    <help>
        helps you
    <play>
        plays the audio
    <stop>
        stops the audio
    <get_duration>
        get total audio length in seconds
    <get_busy>
        checks if the audio is playing
    <wait>
        waits for the audio to finish playing
    <pause>
        pauses the audio
    <set_looping>
        sets if the audio loops   
        """
        print(h)
        
    def play(self,busy_check=True):
        if busy_check:
            Audio._threading.Thread(target=self._gb0,daemon=True).start()
        self._class.start()
    
    def stop(self):
        self._class.stop()
        
    def get_duration(self):
        return self._class.getDuration()/1000
    
    def get_busy(self):
        if self.get_duration()==self._position:
            return False
        return True
        
    def _gb0(self):
        self._busy=1
        dur=round(self.get_duration()*1000)
        for p in range(dur):
            p+=1
            self._position=p/1000
            time.sleep(0.001)
        
        self._busy=0
        
     
    def wait(self):
        while self.get_busy():
            pass
            
    def pause(self):
        self._class.pause()
        
         
    
    def set_looping(self,a0:bool):
        if a0!=True and a0!=False:
            raise ValueError("invalid argument: avaiable: -True -False")
        self._class.setLooping(a0)

0

Well, I would suggest using the default android audio player. You can access it using jinus.

from jnius import autoclass
MediaPlayer = autoclass('android.media.MediaPlayer')
AudioManager = autoclass('android.media.AudioManager')

self.sound = MediaPlayer()
self.sound.setDataSource(yourDataSource) #you can provide any data source, if its on the devie then the file path, or its url if you are playing online
self.sound.prepare()
self.sound.setLooping(False) #you can set it to true if you want to loop
self.sound.start()
# You can also use the following according to your needs
#self.sound.pause()
#self.sound.stop()
#self.sound.release()
#self.sound.getCurrentPosition()
#self.sound.getDuration()
Ankit Sangwan
  • 1,138
  • 1
  • 7
  • 20
  • Hello! Thank you very much for sharing this! I'm wondering how I would setup this up to play live audio (since i'm receiving bytes from a socket, I need a way to play them as the come in.) I found some ways to use `jnius` to play audio files from these links: https://stackoverflow.com/questions/45061116/playing-mp3-on-android https://github.com/kivy/pyjnius/blob/master/docs/source/android.rst Your answer doesn't fully answer my question to tools to play audio in byte format? – JareBear Feb 16 '21 at 03:11