Search code examples
pythonpython-3.xaudiovlc

How to play an audio file starting at a specific time


I want to play an audio file at a specific duration, for example, start it at 1:00 minute or at 500 milliseconds using the media player module in python.

    import vlc
    song = vlc.MediaPlayer('song.mp3')
    song.play()

now with song.play() i can play the file but only from the beginning, so is there any way to start it at a specific duration?


Solution

  • Use the set_time() method of the MediaPlayer object:

    import vlc
    song = vlc.MediaPlayer('song.mp3')
    song.play()
    song.set_time(10000)    # play at 10,000 ms (10 seconds)
    

    There is also set_position() which works with values between 0.0 and 1.0:

    song.set_position(0.5)   # half way through media file