python-vlc icon indicating copy to clipboard operation
python-vlc copied to clipboard

Audio must be playing before volume can be set?

Open flatsiedatsie opened this issue 3 years ago • 1 comments

I'm using VLC to stream internet radio. My problem is that there is seemingly no way to set the intended audio volume before the stream is fully loaded and acually playing. This results in the audio always briefly playing at 100% volume before I can quickly lower it.

Before the stream is loaded the output is said to be -1. Then when it's loaded, it's 100.

I've tried setting it to 0 (mute), I've tried setting it to paused, but nothing worked.

In the end I wrote a for loop that tries to lower the volume as quickly as possible.

self.vlc_media = vlc.Media( 'http://direct.fipradio.fr/live/fip-midfi.mp3' )

# setting media
self.vlc_player.set_media( self.vlc_media )

self.vlc_player.audio_set_volume( self.persistent_data['volume'] ) # setting it to 15
print("vol: " + str(self.vlc_player.audio_get_volume())) # reports -1

self.vlc_player.play()

for x in range(15000):
    
    self.vlc_player.audio_set_volume( self.persistent_data['volume'] )
    time.sleep(0.001)
    volume = self.vlc_player.audio_get_volume()
    if volume != -1:
        print("x: " + str(x) + ' volume: ' + str(volume))
        break

time.sleep(.03)
print("volq: " + str(self.vlc_player.audio_get_volume()))
self.vlc_player.audio_set_volume( self.persistent_data['volume'] )
print("volq2: " + str(self.vlc_player.audio_get_volume()))
time.sleep(.07)
self.vlc_player.audio_set_volume( self.persistent_data['volume'] )

# only now has the volume taken hold

oddly, even when this code break out of the loop, reporting that the volume is lowered, in reality it will still output at full blast for a fraction of a second.

This makes it very hard to use this to play music at night, when people are sleeping.

I couldn't find any examples that helped me figure out the correct order or timing. What am I doing wrong?

flatsiedatsie avatar Aug 23 '22 20:08 flatsiedatsie

I am experiencing a similar problem. If I fade out an audio file, when I start the next audio file it starts playing at the faded volume even if I set the volume up again before calling play() or even if I set it immediately after calling play().

I am toying with the idea of pausing playback as soon as the fadeout has reduced the volume to zero and then resetting the volume before actually stopping the playback. I am not in a hurry to try this because:

  1. It would almost certainly mean quite a bit of restructuring of my code, which uses multiple threads.
  2. I am not certain it would even work. The problem might be that the file is not actually playing at the time I set the volume.

re the OP "full blast" problem, I'm guessing that there is a buffer holding decoded samples awaiting transfer to the A/D converter and samples already placed in the buffer have to play out before those at the new volume hit the A/D converter. If I'm correct, it means that by the time your loop identifies that playback has started the buffer is already full.

thewhovian avatar Jan 21 '24 18:01 thewhovian