参考下列代码,编写播放mp3的python代码。#!/usr/bin/env python # -*- coding: utf-8 -*- import pygame import os from tkinter import * root = Tk() root.title('music播放器') root.geometry('800x600') pygame.init() music_path = r'E:\music\zhousen' current_play = '3249277192.mp3' music_name = StringVar() def play(): music_name.set(current_play) pygame.mixer.music.load(os.path.join(music_path, current_play)) pygame.mixer.music.play() label1=Label(root, textvariable=music_name, font=('宋体', 14), justify='center',bg='#ADD8E6').place(relx=0, rely=0.1, relwidth=1, relheight=0.3) #place 布局 button1=Button(root, text="播放", command=play).place(relx=0, rely=0.5, relwidth=0.2, relheight=0.1) button2=Button(root, text="暂停", command=lambda: pygame.mixer.music.pause()).place(relx=0.2, rely=0.5, relwidth=0.2, relheight=0.1) button3=Button(root, text="恢复", command=lambda: pygame.mixer.music.unpause()).place(relx=0.4, rely=0.5, relwidth=0.2, relheight=0.1) button4=Button(root, text="停止", command=lambda: pygame.mixer.music.stop()).place(relx=0.6, rely=0.5, relwidth=0.2, relheight=0.1) button5=Button(root, text="重播", command=lambda: pygame.mixer.music.rewind()).place(relx=0.8, rely=0.5, relwidth=0.2, relheight=0.1) root.mainloop()
时间: 2023-11-28 13:48:23 浏览: 85
你可以使用pyglet库来播放mp3文件,以下是一个简单的示例代码:
```python
import pyglet
music_path = 'E:/music/zhousen'
current_play = '3249277192.mp3'
def play():
music = pyglet.media.load(f"{music_path}/{current_play}")
music.play()
play_button = pyglet.window.Window(width=200, height=100, caption='Play MP3')
@play_button.event
def on_draw():
play_button.clear()
pyglet.gl.glColor3f(1, 1, 1)
pyglet.graphics.draw(2, pyglet.gl.GL_LINES,
('v2i', (10, 50, 190, 50))
)
label = pyglet.text.Label(
current_play,
font_name='Times New Roman',
font_size=18,
x=play_button.width//2, y=play_button.height//2,
anchor_x='center', anchor_y='center'
)
label.draw()
@play_button.event
def on_mouse_press(x, y, button, modifiers):
if button == pyglet.window.mouse.LEFT:
play()
pyglet.app.run()
```
这个代码中,我们创建了一个Pyglet窗口来显示当前播放的歌曲,并且在窗口上添加了一个按钮,当用户点击按钮时,就会播放当前选择的mp3文件。注意,这个代码只是一个示例,你需要根据自己的需求来修改它。
阅读全文