-1

I have multiple live streams running which I'm playing with python-vlc.

So something like this:

stream1 = vlc.MediaPlayer("rtsp://...")
stream2 = vlc.MediaPlayer("rtsp://...")
stream1.play()
stream1.audio_toggle_mute()
stream2.play()
stream2.toggle_fullscreen()

I can use toggle_fullscreen to display one but how can I switch between multiple streams and bring a specific one to the foreground? Would I just have to stop and start it again or is there an easier way?

asm
  • 837
  • 1
  • 16
  • 41
  • How would you trigger it? If the video is foreground, your app doesn't have focus to get keystrokes, right? – Tim Roberts Jul 29 '23 at 00:28
  • Let's say it's based on a timer. There's going to be logic to determine which stream to play in the foreground. I'm not worried about that, just trying to figure out if I can even switch it – asm Jul 29 '23 at 00:31
  • You can call `toggle_fullscreen` on both to switch them. It will turn 2 off and 1 on. – Tim Roberts Jul 29 '23 at 00:33
  • I tried that but it doesn't bring it to the foreground. It just toggles it and I have to manually switch to see the other one – asm Jul 29 '23 at 00:34
  • Hmm. On Windows, you could use `stream1.get_hwnd()` and pass that to `win32gui.SetForegroundWindow`. – Tim Roberts Jul 29 '23 at 00:45
  • I just checked the documentation and it looks like `get_hwnd()` will return the value we've set when we call `set_hwnd()`. I'll check it out once I understand how to properly use `set_hwnd()`. Thanks! – asm Jul 29 '23 at 01:27

1 Answers1

0

I ended up using PyQt5 to create a window and embed the vlc media player in it and then switch using that. The code I used to define the stream is:


class Stream(QtWidgets.QMainWindow):

    def __init__(self, url: str, title: str, parent=None):
        super(Stream, self).__init__(parent)
        self.setWindowTitle(title)

        # creating a basic vlc instance
        self.instance = vlc.Instance()
        self.media_player = self.instance.media_player_new()
        self.video_frame = QtWidgets.QFrame()

        if sys.platform.startswith("linux"):  # for Linux using the X Server
            self.media_player.set_xwindow(self.video_frame.winId())
        elif sys.platform == "win32":  # for Windows
            self.media_player.set_hwnd(self.video_frame.winId())
            print(self.media_player.get_hwnd())
        elif sys.platform == "darwin":  # for MacOS
            self.media_player.set_nsobject(self.video_frame.winId())

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        lay = QtWidgets.QVBoxLayout(central_widget)
        lay.addWidget(self.video_frame)
        self.video_frame.setStyleSheet("border: 0px")
        central_widget.setStyleSheet("border: 0px")
        self.setStyleSheet("border: 0px")

        youtube_url = pafy.new(url).getbest()
        media = self.instance.media_new(youtube_url.url)
        self.media_player.set_media(media)
        self.media_player.audio_set_volume(0)
        self.media_player.play()
        self.showFullScreen()

    def bring_to_foreground(self):
        self.window().activateWindow()
        self.media_player.audio_set_volume(100)

    def mute(self):
        self.media_player.audio_set_volume(0)
asm
  • 837
  • 1
  • 16
  • 41