📜  Python VLC – 创建 MediaPlayer 对象(1)

📅  最后修改于: 2023-12-03 14:46:06.146000             🧑  作者: Mango

Python VLC – Creating MediaPlayer Object

Python VLC is a python bindings for the VideoLAN Client (VLC) media player. It allows developers to use VLC media player's functionality within their python scripts. In this article, we will discuss how to create a MediaPlayer object using Python VLC.

Installation

Before we can create a MediaPlayer object, we need to install Python VLC. Follow the below steps to install Python VLC:

  1. Install VLC media player on your system (if it is not already installed).
  2. Install python-vlc package using pip. Open your terminal/command prompt and run the following command:
pip install python-vlc
Creating MediaPlayer object

Now that we have installed Python VLC, let's create a MediaPlayer object using the following code:

import vlc

media = vlc.MediaPlayer("path/to/your/video/file")
media.play()

The above code will create a MediaPlayer object and play the video file located at the given path. If the video file path is a URL, then it will play the video from the given URL.

Controlling MediaPlayer object

We can control the MediaPlayer object by using the following methods:

  • play(): This method starts or resumes playing the media.
  • pause(): This method pauses the media playback.
  • stop(): This method stops the media playback.
  • set_fullscreen(boolean): This method sets the fullscreen mode, where boolean is a boolean value (True or False) to enable or disable fullscreen mode.
  • set_position(float): This method sets the media position, where float is a value from 0.0 to 1.0 representing the percentage of the media.
  • set_time(int): This method sets the media time (in milliseconds).
  • set_volume(int): This method sets the media volume, where int is a value from 0 to 100 representing the volume percentage.
Example
import vlc
import time

media = vlc.MediaPlayer("path/to/your/video/file")

media.play()
time.sleep(10) # wait for 10 seconds
media.pause()

media.set_fullscreen(True)

media.set_position(0.5)

media.set_time(10000)

media.set_volume(50)

media.stop()

The above code will play the video file for 10 seconds and then pause it. After that, it will set the MediaPlayer object to fullscreen mode, set the media position to 50%, set the media time to 10 seconds, set the media volume to 50%, and finally stop the media playback.

Conclusion

Python VLC is a powerful library that allows developers to use VLC media player's functionality within their python scripts. In this article, we discussed how to create a MediaPlayer object using Python VLC and how to control it with various methods.