SDL2 won't play any format of audio

275 views
Skip to first unread message

Claver Barreto

unread,
Feb 14, 2023, 11:08:24 AM2/14/23
to Kivy users support
Hello mates, so i've been working on this game. that have background musics, when one music finishes, i have a method that will switch to other random music. To achieve this i was using SoundLoader from kivy.core.audio.SoundLoader.

The soundloader works fine, however i noticed that everytime it loads a new music from the method to switch music, it makes the game freeze and it only goes back to normal when the music is loaded .

A mate from Reddit recomended me to use MusicSDL2 from kivy.core.audio.audio_sdl2.MusicSDL2. Now, i've encountered an error that said the audio couldn't be played, and i've fixed that by uninstalling kivy, and installing the libvorbis and libogg packages and by installing the kivy from the master branch.

Now, no matter what i do i cant get the audio to play in my android device, these 5days i've tried everything i could but i always get the same error when i try to play an ".ogg" or any audio format:

02-14 17:31:20.540 32495   713 I python  : [INFO   ] [Base        ] Start application main loop
02-14 17:31:20.554 32495   713 I python  : [INFO   ] [GL          ] NPOT texture support is available
02-14 17:31:24.024 32495   713 I python  : [WARNING] [AudioSDL2   ] Unable to load music sound1.ogg: b"Couldn't open 'sound1.ogg'"
02-14 17:31:34.291 32495   713 I python  : [WARNING] [AudioSDL2   ] Unable to load music sound1.ogg: b"Couldn't open 'sound1.ogg'"
02-14 17:31:41.550 32495   713 I python  : [INFO   ] [Base        ] Leaving application in progress...

but when i use Soundloader and ffpyplayer, everthing works fine in my android device.

TLDR: audio won't play in android if  MusicSDL2 is used, the error output is the above one.

so far i've a tons things, all of it is in this  reddit thread:

Im out of ideas right now, any help is appreciated. Thanks in advance.

Robert

unread,
Feb 14, 2023, 1:43:52 PM2/14/23
to Kivy users support
(Disclaimer, I have no personal experience of using this)

As I understand it you want to use MusicSDL2 on Android, and the dependency for ogg files is:

requirements = python3, kivy, libogg, libvorbis
Builds for me on arm64.

I suggest you revisit the libvorbis build error message superficially described on the Reddit thread.

Claver Barreto

unread,
Feb 14, 2023, 4:09:19 PM2/14/23
to Kivy users support
i've installed both packages with my package manager. i've read that the error libvorbis was showing, it was because livorbis require libogg, and it kinda makes sense because, when i just put libvorbis in the buildozer requirements the error would show up, however, if i put both libvorbis and libogg, buildozer builds the app normally but the audio wont play in the android device, showing the error:
 [AudioSDL2   ] Unable to load music sound1.ogg: b"Couldn't open 'sound1.ogg'"
the error regarding putting only libvorbis in the buildozer requirement outputs that it is not finding the file "ogg.h" in the line "#include <ogg/ogg.h>", so i've downloaded the ogg.h and tried to place it in the folder it was supposed to be but the error would still show up.
I dont know if this is relevant, but since you said it is building for you on arm64, all my builds until now have been for armeabi-v7a, would this have anything to do with the sdl2 audio problem?

Claver Barreto

unread,
Feb 14, 2023, 4:22:21 PM2/14/23
to Kivy users support
Now what i find odd too is that every other format shows the same error, even if use .wav format the errror show up. now in the worst case scenario i dont solve this, is there any other sound provider that would work on kivy and in android, and it wont make the loading process freeze the game by loading the songs into memory or something? if there isn't any other sound provider, then would it be viable to use a python audio module to play sounds instead?

Robert

unread,
Feb 14, 2023, 7:31:16 PM2/14/23
to Kivy users support
Sorry not my area of expertise, I know little about audio players.

Freezing the UI, is usually a symptom of doing IO in the UI thread.

It is an error of reasoning to assume that if somebody says nothing about a platform that means it doesn't work. I just built on arm7 with no issue.

It seem like you have several issues going on at the same time
- building libvorbis
- opening a file with MusicSDL2
- async loading
- choosing a media player

You will find it more productive to abandon the shotgun approach to development and debugging, and focus on one issue at a time. 

Claver Barreto

unread,
Feb 16, 2023, 5:34:20 AM2/16/23
to Kivy users support
its alright mate. So i was seeing what audio providers kivy have and i've been playing with "audio_android", and it does what i want, it plays and switches the sounds without freezing the app, however this isn't exactly a solution, its more like an escapre route, although its working i still will need to solve the sdl2 problem because this "solution" is only effective on android devices and im yet to test the app on iphone. Anyways i'll figure something out when the time comes, for now im ok with the results.

alertforlinux alertforlinux

unread,
Feb 16, 2023, 1:07:19 PM2/16/23
to Kivy users support
Hi, How did you solve the freezing issue while uploading the audio file with audio_android? When I want to load and play the audio file as below, the screen freezes during the loading of the audio file.

from kivy.core.audio import SoundLoader
sound = SoundLoader.load('mytest.ogg')
sound.play()

Best regards,
Lion

Robert

unread,
Feb 16, 2023, 2:22:00 PM2/16/23
to Kivy users support
Does using a thread not work?

Claver Barreto

unread,
Feb 16, 2023, 3:00:11 PM2/16/23
to Kivy users support
hello mate, so ended up using another sound provider that kivy have, which is "audio_android", so with this sound provider you'll only be able to play sound on your android device, and this is because your desktop doesn't have a module called android.
Heres an example on how to use it:

from kivy.core.audio.audio_android import SoundAndroidPlayer
sound = SoundAndroidPlayer(source="path/to/your/songs/song1.ogg"

as you can see its kinda the same as using Soundloader but you have to replace "SoundLoader" with "SoundAndroidPlayer".
Now you may be better off using MusicSDL2 from the audio_sdl2 audio provider, and this is because with audio_android only android device may be able to play sounds, so it will be a problem for devices such as iphones.
for MusicSDL2 is kinda the same as SoundAndroidPlayer:

from kivy.core.audio.audio_sdl2 import MusicSDL2

sound = MusicSDL2(source="path/to/your/songs/song1.ogg"

for methods or functions of these audio providers, i recommend using the "dir" command in your python interpreter to see them all, to gather limitations or things you want and experiment with them.
For more informations on these audio providers and other, check the link bellow, you may look at the code behind them and see the methods you want...


have a good one Lion.

Claver Barreto

unread,
Feb 16, 2023, 3:00:56 PM2/16/23
to Kivy users support
i actually never thought about this, i'll try to use it, and i'll post the results if i get any.

Claver Barreto

unread,
Feb 16, 2023, 3:04:01 PM2/16/23
to Kivy users support
oh i forgot to mention that, them MusicSDL2 method doesnt work on my android device, however, other mates said that it worked fine on their devices which may mean that its a problem with my device, so its better for you to try to use MusicSDL2 first before audio_android's SoundAndroidPlayer. And, YES, it solved my freezing problem.

On Thursday, February 16, 2023 at 9:22:00 PM UTC+2 Robert wrote:

alertforlinux alertforlinux

unread,
Feb 17, 2023, 1:00:44 PM2/17/23
to kivy-...@googlegroups.com
I used threads. nothing has changed, the problem persists.

--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/7Tm7LD62lqk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/726f5b97-c459-4ffd-b199-e96ade574051n%40googlegroups.com.

alertforlinux alertforlinux

unread,
Feb 17, 2023, 2:57:10 PM2/17/23
to Kivy users support
Thanks, I will try this way.

Claver Barreto

unread,
Feb 17, 2023, 4:42:18 PM2/17/23
to Kivy users support
damn i was hoping threads would work. Well, gotta make lemons with what we have.

Claver Barreto

unread,
Feb 17, 2023, 4:45:03 PM2/17/23
to Kivy users support
Best of luck mate, specially with the MusicSDL2, if it doesn't work you're better off with audio_android else: you're gonna need a bunch of luck to make it work. I MusicSDL2 doesn't work and you somehow end up fixing or find a way to make it work, please share it with me.

Elliot Garbus

unread,
Feb 17, 2023, 5:20:42 PM2/17/23
to kivy-...@googlegroups.com

I’m surprised threads did not work.

Have your tired using soundloader in a thread, and starting the thread far enough in advance to ensure the file has been loaded before play is called?

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/bef7257f-af70-4aad-8017-ef05d035ea2en%40googlegroups.com.

 

alertforlinux alertforlinux

unread,
Feb 18, 2023, 6:02:01 AM2/18/23
to kivy-...@googlegroups.com
I tried the thread as below, freezing problem persisted while loading

from kivy.core.audio import SoundLoader

def sound_loader(self, *args):
self.thread_sound = Thread(target=self.sound_load_now)
self.thread_sound.daemon = True
self.thread_sound.start()

def sound_load_now(self, *args):

sound = SoundLoader.load('mytest.ogg')

trigger1 = Clock.create_trigger(self.sound_loader, timeout=0, interval=False, release_ref=True)
trigger1()

sound.play()

Elliot Garbus

unread,
Feb 18, 2023, 12:54:52 PM2/18/23
to kivy-...@googlegroups.com

I’ll assume that it takes some non-trivial amount of time to load the sound.

The sound must have completed loading prior to being played.

As written, you are kicking off a separate thread to load the file, then waiting for it to complete.

You could try loading and playing the sound in a separate thread, or find a way to load the sound far enough in advance of playing the sound that it is loaded prior to being played.

 

Looking specifically at your code, you scheduling the loader, then calling play on the sound.  I’m surprised this does not cause an error.

Is there something unusual about your sound file?  On a laptop, I have never experienced a long delay in loading a file.

 

You might want to try something like this:

 

from kivy.app import App
from kivy.lang import Builder
from kivy.core.audio import SoundLoader
from kivy.clock import Clock

kv =
"""
BoxLayout:
    Label:
        text: 'Audio Load test'
    AnchorLayout:
        Button:
            size_hint: None, None
            size: 100, 48
            text: 'Play'
            on_release: app.play()
"""

class SoundApp(App):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.sound = None

    def
build(self):
       
return Builder.load_string(kv)


   
def play(self):
       
self.sound = SoundLoader.load('15 - Hide Away.mp3') # your sound file here
        Clock.schedule_once(
self._play)

   
def _play(self, _):
       
self.sound.play()

SoundApp().run()

alertforlinux alertforlinux

unread,
Feb 18, 2023, 1:40:09 PM2/18/23
to kivy-...@googlegroups.com
Actually these are the functions inside a class i've posted. The sound play part is not important. There is no problem there. even if I do not play the sound; The screen freezes while the audio file is being loaded, you can observe the problem more easily on android devices, you need to upload longer audio files (ten to fifteen minutes) to observe on the computer. The file size doesn't matter much, the length of the audio is important.

Elliot Garbus

unread,
Feb 18, 2023, 1:44:57 PM2/18/23
to kivy-...@googlegroups.com

Do you have an audio file you can share that illustrates the issue on a laptop?

I’ll try a few solutions.

alertforlinux alertforlinux

unread,
Feb 18, 2023, 2:53:20 PM2/18/23
to kivy-...@googlegroups.com
I made a small change to the code; I added an input text. Try to write something here right after pressing the play button, you will see that you cannot write for a short time. 
music file link: https://easyupload.io/7o5inf 
password : 1234567

from kivy.app import App
from kivy.lang import Builder
from kivy.core.audio import SoundLoader
from kivy.clock import Clock

kv = """
BoxLayout:

Label:
text: 'Audio Load test'
AnchorLayout:
Button:
size_hint: None, None
size: 100, 48
text: 'Play'
on_release: app.play()
    TextInput:
font_size: 40
text: "Try to write something here right after pressing the play button, you will see that you cannot write for a short time."
halign: 'center'
padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
        
"""

class SoundApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.sound = None

def build(self):
return Builder.load_string(kv)


def play(self):
        self.sound = SoundLoader.load('mytest2.mp3') # your sound file here
        Clock.schedule_once(self._play)

def _play(self, _):
self.sound.play()

SoundApp().run()

Elliot Garbus

unread,
Feb 18, 2023, 3:33:27 PM2/18/23
to kivy-...@googlegroups.com

I added some timing info.  That file is loading on my desktop in 0.11 seconds.  I do not see the delay.

Here I have added timing info, and one version threaded, one version running on the main thread.

 

From a cold start:

Play Load Time:  0.117 seconds

start to play time:  0.118 seconds

 

Thread start from a cold start:

Thread Play - Load Time:  0.116 seconds

Thread Play - start to play time:  0.117 seconds

 

Let me know what you see.

 

import threading
from time import perf_counter

from kivy.app import App
from kivy.core.audio import SoundLoader
from kivy.lang import Builder

kv =
"""
<PlayButton@Button>:

    size_hint: None, None
    size: 100, 48
   
BoxLayout:
    Label:
        text: 'Audio Load test'
    AnchorLayout:
        BoxLayout:
            size_hint: None, None
            size: self.minimum_size
            PlayButton:
                text: 'Play'
                on_release: app.play()
            PlayButton:
                text: 'Thread Play'
                on_release: app.thread_play()

    TextInput:
        font_size: 40
        text: "Try to write something here right after pressing the play button, you will see that you cannot write for a short time."
        halign: 'center'
        padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]

"""


class SoundApp(App):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.sound = None

    def
build(self):
       
return
Builder.load_string(kv)

   
def thread_play(self):
        t = threading.Thread(
target=self._play)
        t.start()

   
def _play(self, *args):
        start = perf_counter()
       
self.sound = SoundLoader.load('mytest2.mp3'# your sound file here
       
print(f'Thread Play - Load Time: {perf_counter() - start: 0.3f} seconds')
       
self.sound.play()
       
print(f'Thread Play - start to play time: {perf_counter() - start: 0.3f} seconds')

   
def play(self):
        start = perf_counter()
       
self.sound = SoundLoader.load('mytest2.mp3'# your sound file here
       
print(f'Play Load Time: {perf_counter() - start: 0.3f} seconds')
       
self.sound.play()
       
print(f'start to play time: {perf_counter() - start: 0.3f} seconds')


SoundApp().run()

alertforlinux alertforlinux

unread,
Feb 18, 2023, 4:06:41 PM2/18/23
to kivy-...@googlegroups.com
Play Load Time:  1.323 seconds
start to play time:  1.323 seconds
Thread Play - Load Time:  1.219 seconds
Thread Play - start to play time:  1.219 seconds

Try increasing the file size by copying the audio file and appending it to the end. 
cp mytest2.mp3  mytest3.mp3
cat mytest2.mp3>>mytest3.mp3

Or  if you try it on android phone, you can observe the problem. Since the screen freezes for two or three seconds on my laptop I use, I cannot write in the text field.


Elliot Garbus

unread,
Feb 18, 2023, 4:24:22 PM2/18/23
to kivy-...@googlegroups.com

When you used the threaded version, can you type in the TextInput?

alertforlinux alertforlinux

unread,
Feb 18, 2023, 4:40:57 PM2/18/23
to kivy-...@googlegroups.com

Elliot Garbus

unread,
Feb 18, 2023, 4:43:07 PM2/18/23
to kivy-...@googlegroups.com

Hmmm.  Strange.  I’m heading out to so some errands – but I’ll create a long audio file and see if I can create a reproducer.

Elliot Garbus

unread,
Feb 18, 2023, 7:39:23 PM2/18/23
to kivy-...@googlegroups.com

I opened your audio file in audacity, created multiple copies and exported as an mp3 file.  The resulting file is 98,486 KB, and has a playtime of over an hour.  It still works fine for me.  Load time is 0.11 seconds.

 

I’m running Windows11, using Gstreamer.

Could you have a system configuration issue?

alertforlinux alertforlinux

unread,
Feb 19, 2023, 5:53:46 AM2/19/23
to kivy-...@googlegroups.com
I'm using an intel macbook pro. If you create an apk and try it; you can observe it more easily. During a 30-second sound file loading; freezes the app for about 7 seconds

Elliot Garbus

unread,
Feb 19, 2023, 8:03:21 AM2/19/23
to kivy-...@googlegroups.com

I don’t have any android platforms.  I can do a run on an intel mac.

What audio provider are you using?  Do you see the problem on Mac, or only on Android?

alertforlinux alertforlinux

unread,
Feb 19, 2023, 8:10:45 AM2/19/23
to kivy-...@googlegroups.com
yes, i see the problem on mac,
[Audio ] Providers: audio_sdl2 (audio_ffpyplayer, audio_avplayer ignored)
IDE: PyCharm

Elliot Garbus

unread,
Feb 19, 2023, 8:48:37 AM2/19/23
to kivy-...@googlegroups.com
I do not see the issue on the Mac.

[INFO   ] [SoundFFPy   ] Using ffpyplayer 4.3.5
[INFO   ] [Audio       ] Providers: audio_ffpyplayer, audio_sdl2 (audio_avplayer ignored)

Play Load Time:  0.037 seconds
start to play time:  0.037 seconds

I would suggest reinstalling kivy.  I do a pip install into a venv.



alertforlinux alertforlinux

unread,
Feb 19, 2023, 9:32:16 AM2/19/23
to kivy-...@googlegroups.com
Remove your ffpyplayer package from your venv and try again.
my other config files:
[INFO] [Kivy] v2.1.0
[INFO ] [Python ] v3.9.2 (v3.9.2:1a79785e3e, 19 Feb 2021, 09:06:10)
By the way, I have an android application in the market and the problem continues on the android device.
this is not related to my local venv

ElliotG

unread,
Feb 19, 2023, 1:38:14 PM2/19/23
to Kivy users support
I set the audio player using the environment variable.
Using ffpyplayer, all worked as expected.
forcing the use of sdl2, loading has a 1 second delay;  using threads caused a lockup.

import threading
from time import perf_counter
import os
os.environ['KIVY_AUDIO'] = 'ffpyplayer'


from kivy.app import App
from kivy.core.audio import SoundLoader
from kivy.lang import Builder



kv = """
<PlayButton@Button>:
    size_hint: None, None
    size: dp(100), dp(48)
    def _play_continue(self):
        self.sound.play()


    def play(self):
        start = perf_counter()
        self.sound = SoundLoader.load('mytest2.mp3')  
        print(f'Play Load Time: {perf_counter() - start: 0.3f} seconds')
        self.sound.play()
        print(f'start to play time: {perf_counter() - start: 0.3f} seconds')


SoundApp().run()

alertforlinux alertforlinux

unread,
Feb 19, 2023, 2:26:43 PM2/19/23
to kivy-...@googlegroups.com
Everytime i start Kivy with KIVY_AUDIO=ffpyplayer, it shows:

objc[44991]: Class SDLApplication is implemented in both /Test/venv/lib/python3.9/site-packages/kivy/.dylibs/SDL2 (0x1082082b0) and /Test/venv/lib/python3.9/site-packages/ffpyplayer/.dylibs/libSDL2-2.0.0.dylib (0x1111ea558). One of the two will be used. Which one is undefined.

objc[44991]: Class SDLAppDelegate is implemented in both /Test/venv/lib/python3.9/site-packages/kivy/.dylibs/SDL2 (0x108208300) and /Test/venv/lib/python3.9/site-packages/ffpyplayer/.dylibs/libSDL2-2.0.0.dylib (0x1111ea5a8). One of the two will be used. Which one is undefined.

objc[44991]: Class SDLTranslatorResponder is implemented in both /Test/venv/lib/python3.9/site-packages/kivy/.dylibs/SDL2 (0x108208418) and /Test/venv/lib/python3.9/site-packages/ffpyplayer/.dylibs/libSDL2-2.0.0.dylib (0x1111ea620). One of the two will be used. Which one is undefined.

objc[44991]: Class SDLMessageBoxPresenter is implemented in both /Test/venv/lib/python3.9/site-packages/kivy/.dylibs/SDL2 (0x108208440) and /Test/venv/lib/python3.9/site-packages/ffpyplayer/.dylibs/libSDL2-2.0.0.dylib (0x1111ea648). One of the two will be used. Which one is undefined.

objc[44991]: Class SDL_cocoametalview is implemented in both /Test/venv/lib/python3.9/site-packages/kivy/.dylibs/SDL2 (0x1082083a0) and /Test/venv/lib/python3.9/site-packages/ffpyplayer/.dylibs/libSDL2-2.0.0.dylib (0x1111ea698). One of the two will be used. Which one is undefined.

objc[44991]: Class SDLOpenGLContext is implemented in both /Test/venv/lib/python3.9/site-packages/kivy/.dylibs/SDL2 (0x108208350) and /Test/venv/lib/python3.9/site-packages/ffpyplayer/.dylibs/libSDL2-2.0.0.dylib (0x1111ea6e8). One of the two will be used. Which one is undefined.

objc[44991]: Class SDLWindow is implemented in both /Test/venv/lib/python3.9/site-packages/kivy/.dylibs/SDL2 (0x108208490) and /Test/venv/lib/python3.9/site-packages/ffpyplayer/.dylibs/libSDL2-2.0.0.dylib (0x1111ea878). One of the two will be used. Which one is undefined.

objc[44991]: Class Cocoa_WindowListener is implemented in both /Test/venv/lib/python3.9/site-packages/kivy/.dylibs/SDL2 (0x1082084b8) and /Test/venv/lib/python3.9/site-packages/ffpyplayer/.dylibs/libSDL2-2.0.0.dylib (0x1111ea8a0). One of the two will be used. Which one is undefined.

objc[44991]: Class SDLView is implemented in both /Test/venv/lib/python3.9/site-packages/kivy/.dylibs/SDL2 (0x108208530) and /Test/venv/lib/python3.9/site-packages/ffpyplayer/.dylibs/libSDL2-2.0.0.dylib (0x1111ea918). One of the two will be used. Which one is undefined.

objc[44991]: Class METAL_RenderData is implemented in both /Test/venv/lib/python3.9/site-packages/kivy/.dylibs/SDL2 (0x108208210) and /Test/venv/lib/python3.9/site-packages/ffpyplayer/.dylibs/libSDL2-2.0.0.dylib (0x1111ea990). One of the two will be used. Which one is undefined.

objc[44991]: Class METAL_TextureData is implemented in both /Test/venv/lib/python3.9/site-packages/kivy/.dylibs/SDL2 (0x108208260) and /Test/venv/lib/python3.9/site-packages/ffpyplayer/.dylibs/libSDL2-2.0.0.dylib (0x1111ea9e0). One of the two will be used. Which one is undefined.


Robert

unread,
Feb 19, 2023, 2:38:51 PM2/19/23
to Kivy users support
Is this an Kivy-iOS question?
Try asking here https://discord.gg/rqcvkb45

Elliot Garbus

unread,
Feb 19, 2023, 2:53:28 PM2/19/23
to kivy-...@googlegroups.com

alertforlinux alertforlinux

unread,
Feb 19, 2023, 3:10:36 PM2/19/23
to kivy-...@googlegroups.com
No, I get this message when I install and define ffpyplayer on macbook and then run kivy application with pycharm

alertforlinux alertforlinux

unread,
Feb 22, 2023, 6:57:57 PM2/22/23
to Kivy users support
When I installed ffpyplayer, I did not observe the freezing problem on the laptop; but when I create apk by making ffpyplayer definitions, the freezing problem continues.

Elliot Garbus

unread,
Feb 22, 2023, 8:23:16 PM2/22/23
to kivy-...@googlegroups.com

alertforlinux alertforlinux

unread,
Feb 23, 2023, 6:44:05 AM2/23/23
to Kivy users support
I will try to find another solution; Thanks a lot for your support,
Reply all
Reply to author
Forward
0 new messages