📜  Python VLC MediaPlayer – 设置媒体角色(1)

📅  最后修改于: 2023-12-03 15:19:02.518000             🧑  作者: Mango

Python VLC MediaPlayer – Setting Media Role

Python VLC MediaPlayer is a useful module that allows programmers to create media player applications in Python programming language. One of the important features of this module is the ability to set media roles.

Introduction to Media Roles

Media roles are an essential aspect of multimedia applications that specify how a particular media file should be played. Different roles have different priorities and behaviors, which makes them suitable for different types of media content.

There are various types of media roles available in VLC media player, such as:

  • Music
  • Video
  • Audiovisual
  • Communicate
  • Game
  • Presentation
  • TV
  • Recording

The media role you choose will depend on the type of media content you are playing.

Setting Media Role in Python VLC MediaPlayer

Python VLC MediaPlayer makes it easy to set media roles using the following steps:

Step 1: Import Required Libraries

The first step is to import the required libraries, which include VLC and PyQt5. You can install these libraries using pip.

import vlc
from PyQt5 import QtWidgets, QtGui
Step 2: Create Media Player Instance

Create an instance of the media player using the vlc.MediaPlayer() method.

player = vlc.MediaPlayer()
Step 3: Set Media Role

Use the set_role() method to set the media role of the player.

player.set_role(vlc.Role.Video)

In this case, we have set the media player to the Video role.

Full Code Example

Here is the full code example that sets the media role of the Player.

import vlc
from PyQt5 import QtWidgets, QtGui

class VLCPlayer(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(VLCPlayer, self).__init__(parent)
        self.setWindowTitle("VLC Player")
        self.instance = vlc.Instance("--no-xlib --quiet")
        self.player = self.instance.media_player_new()
        self.player.set_role(vlc.Role.Video)
        self.widget = QtWidgets.QWidget(self)
        self.setCentralWidget(self.widget)
        hLayout = QtWidgets.QHBoxLayout(self.widget)
        hLayout.setContentsMargins(0, 0, 0, 0)
        self.vlc_widget = QtWidgets.QFrame(self.widget)
        hLayout.addWidget(self.vlc_widget)
        vlc_layout = QtWidgets.QVBoxLayout(self.vlc_widget)
        vlc_layout.setContentsMargins(0, 0, 0, 0)
        vlc_layout.addWidget(self.get_vlc())
        self.timer = QtCore.QTimer(self)
        self.timer.setInterval(200)
        self.timer.timeout.connect(self.update_ui)
        self.timer.start()

    def get_vlc(self):
        if sys.platform.startswith('linux'):
            self.player.set_xwindow(self.vlc_widget.winId())
        elif sys.platform == "win32":
            self.player.set_hwnd(self.vlc_widget.winId())
        elif sys.platform == "darwin":
            self.player.set_nsobject(int(self.vlc_widget.winId()))

    def update_ui(self):
        pass

app = QtWidgets.QApplication(sys.argv)
player = VLCPlayer()
player.show()
sys.exit(app.exec_())
Conclusion

Setting media roles is an essential aspect of multimedia applications, and the Python VLC MediaPlayer module makes it easy to accomplish. By following the above steps, you can set the media role of your media player applications with ease.