这里音乐播放器使用pygame模块,将来还会用到requests模块
使用python10和pycham
创建一个工程
首先创建几个工具,在tool.py文件里
从MP4中提取MP3文件
def mp4_mp3(mp4path, mp3path):
from moviepy import editor
video = editor.VideoFileClip(mp4path)
audio = video.audio
audio.write_audiofile(mp3path)
# 拓展名转换工具
def ExtensionNameChangeover(filepath, ExtensionName):
import os
name, suffix = os.path.splitext(filepath)
return name + "." + ExtensionName
然后在创建player类
# 可能有一些代码没有完善,请谅解
import pygame
import sys
from pygame._sdl2 import messagebox
head = sys.argv
print(head)
class Player:
def __init__(self):
pass
@staticmethod
def load(path):
pygame.init()
pygame.mixer.init()
try:
pygame.mixer.music.load(repr(path))
except pygame.error:
import tool
messagebox("此文件无法打开,我们正在尝试处理该文件")
tool.mp4_mp3(repr(path), tool.ExtensionNameChangeover(repr(path), 'mp3'))
@staticmethod
def play():
try:
pygame.mixer.music.play()
except pygame.error:
pygame.mixer.init()
pygame.mixer.music.load(r"example.mp3")
pygame.mixer.music.play()
@staticmethod
def stop():
try:
pygame.mixer.music.pause()
except pygame.error:
pass
@staticmethod
def set_volume(voice):
try:
pygame.mixer.music.set_volume(voice)
except pygame.error:
pass
@staticmethod
def get_volume():
try:
return pygame.mixer.music.get_volume()
except pygame.error:
return
@staticmethod
def getpos():
try:
return pygame.mixer.music.get_pos()
except pygame.error:
return
@staticmethod
def setpos(pos):
try:
pygame.mixer.music.set_pos(pos)
except pygame.error:
pass
我们将不断更新~~~