Search code examples
pythonaudio

Play multiple sounds with same timestamp


I am writing an AB listenning test app that can play multiple audio files with a shared timestamp. Say I have two audio files loaded which have the same length, when I start to play sound, I click a button to switch between these two files every several seconds and I want to preserve the timestamp to continue playing another file.

The key is to return the current frame number (timestamp) of played audio stream, and then start playing another file from this frame.

I'm trying to use simpleaudio to achieve this goal but it seems cannot return the current frame. Another approach is to measure the time during this file's playback (between timespan of two clicks) using datetime and calculate the new start index myself, but I think there must be some better way.

Thank you in advance.


Solution

  • If you are using PyQt5 to create your App, PyQt5.QtMultimedia.QMediaPlayer can do this.

    player = QMediaPlayer() # create a player object
    
    player.pause() # pause
    position = self.player.position() # save current playing timestamp
    player.setMedia(content) # switch to another content
    player.setPosition(position) # reset playing position
    player.play() # continue to play