-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?

6
  • How would you trigger it? If the video is foreground, your app doesn't have focus to get keystrokes, right? Commented Jul 29, 2023 at 0: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 Commented Jul 29, 2023 at 0:31
  • You can call toggle_fullscreen on both to switch them. It will turn 2 off and 1 on. Commented Jul 29, 2023 at 0: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 Commented Jul 29, 2023 at 0:34
  • Hmm. On Windows, you could use stream1.get_hwnd() and pass that to win32gui.SetForegroundWindow. Commented Jul 29, 2023 at 0:45

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.