Skip to content

Instantly share code, notes, and snippets.

@rovesoul
Last active February 25, 2020 16:07
Show Gist options
  • Save rovesoul/82b0451d1863c245b61be45a78c5195a to your computer and use it in GitHub Desktop.
Save rovesoul/82b0451d1863c245b61be45a78c5195a to your computer and use it in GitHub Desktop.
Pygame播放音乐
from pygame import mixer
import time
mixer.init()
mixer.music.load('music.mp3')
mixer.music.play(loops=1,start=0.0)
mixer.music.set_volume(1)# 来设置播放的音量,音量value的范围为0.0到1.0。
time.sleep(5) #只播放5秒
mixer.music.stop()
import pygame # pip install pygame
# 貌似只能播放单声道音乐,可能是pygame模块限制
def playMusic(filename, loops=0, start=0.0, value=0.5):
"""
:param filename: 文件名
:param loops: 循环次数
:param start: 从多少秒开始播放
:param value: 设置播放的音量,音量value的范围为0.0到1.0
:return:
"""
flag = False # 是否播放过
pygame.mixer.init() # 音乐模块初始化
while 1:
if flag == 0:
pygame.mixer.music.load(filename)
# pygame.mixer.music.play(loops=0, start=0.0) loops和start分别代表重复的次数和开始播放的位置。
pygame.mixer.music.play(loops=loops, start=start)
pygame.mixer.music.set_volume(value) # 来设置播放的音量,音量value的范围为0.0到1.0。
if pygame.mixer.music.get_busy() == True:
flag = True
else:
if flag:
pygame.mixer.music.stop() # 停止播放
break
playMusic('auido.mp3')
playMusic('out.wav')
from pygame import mixer
mixer.init()
track ='music.wav'
a = mixer.Sound(track)
print("length",a.get_length())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment