refactor: player to a class
This commit is contained in:
33
player.py
33
player.py
@@ -1,31 +1,38 @@
|
|||||||
import vlc
|
import vlc
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# Path to your video file
|
|
||||||
video_path = "/home/wancat/mind-blowing.mp4"
|
|
||||||
|
|
||||||
|
class Player:
|
||||||
|
def __init__(self, video_path):
|
||||||
# Creating a VLC instance
|
# Creating a VLC instance
|
||||||
player = vlc.Instance()
|
player = vlc.Instance()
|
||||||
|
|
||||||
# Creating a Media Player
|
# Creating a Media Player
|
||||||
media_player = player.media_player_new()
|
self.media_player = player.media_player_new()
|
||||||
|
|
||||||
# Creating a new Media
|
# Creating a new Media
|
||||||
media = player.media_new(video_path)
|
media = player.media_new(video_path)
|
||||||
|
|
||||||
# Setting media to media player
|
# Setting media to media player
|
||||||
media_player.set_media(media)
|
self.media_player.set_media(media)
|
||||||
|
|
||||||
# Play the media
|
def play(self):
|
||||||
media_player.play()
|
self.media_player.play()
|
||||||
|
|
||||||
# Wait for the video to play
|
def get_length(self):
|
||||||
time.sleep(10) # Waits for 5 seconds; adjust or use a different method to wait for the video to finish
|
return self.media_player.get_length()
|
||||||
|
|
||||||
# Stop playing
|
def stop(self):
|
||||||
media_player.stop()
|
self.media_player.stop()
|
||||||
|
|
||||||
# Note: The time.sleep() here is just to prevent the script from ending immediately.
|
# Note: The time.sleep() here is just to prevent the script from ending immediately.
|
||||||
# For a real application, you'll need a more robust way to check if the video is still playing.
|
# For a real application, you'll need a more robust way to check if the video is still playing.
|
||||||
# This could be done by polling the media player's state or setting up event handlers.
|
# This could be done by polling the media player's state or setting up event handlers.
|
||||||
|
|
||||||
|
# Path to your video file
|
||||||
|
if __name__ == '__main__':
|
||||||
|
video_path = "/home/wancat/mind-blowing.mp4"
|
||||||
|
player = Player(video_path)
|
||||||
|
player.play()
|
||||||
|
time.sleep(0.1)
|
||||||
|
print(player.get_length())
|
||||||
|
time.sleep(5)
|
||||||
|
player.stop()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user