pygame 简单播放音乐程序
环境:
python2.7
pygame
功能:
播放指定目录下的歌曲(暂时mp3),可以上一曲、下一曲播放。
文件目录:
font 字体文件夹
image 图片文件夹
music 音乐文件夹
play.py 主程序
settings.py 配置文件
settings.py
- # -*- coding: utf-8 -*-
- # setting 配置文件
- import os
- from os import path
- d = os.path.dirname(__file__)
- ## music file path
- MUSIC_PATH = path.join(d, 'music/')
- ## image file path
- IMAGE_PATH = path.join(d, 'image/')
- ## auto play
- AUTO_PALY = False
- ## paly type we can wav or m4a -> mp3
- PLAY_TYPE = ['.mp3','.wav','.m4a']
- ## DEFAULT play start
- PLAY_START = 0
play.py
- # -*- coding: utf-8 -*-
- import time
- import pygame
- from pygame.locals import *
- import os
- import sys
- from settings import *
- d = os.path.dirname(__file__)
- class window:
- def __init__(self):
- pygame.init()
- pygame.display.set_caption("player")
- bgColor = (255, 230, 230)
- self.screen = pygame.display.set_mode((640, 480))
- self.screen.fill(bgColor)
- ## 显示上一曲
- prve = pygame.image.load("image/prve.png").convert_alpha()
- width, height = prve.get_size()
- self.screen.blit(prve, (240 - width / 2, 40))
- self.prveX = (240 - width / 2, 320 + width / 2)
- self.prveY = (40, 40 + height)
- self.show_play_pause()
- ## 显示下一曲按钮
- next = pygame.image.load("image/next.png").convert_alpha()
- self.screen.blit(next, (400 - width / 2, 40))
- self.nextX = (320 - width / 2, 400 + width / 2)
- self.nextY = (40, 40 + height)
- ## 显示logo
- logo = pygame.image.load("image/pygame_logo.gif").convert_alpha()
- width, height = logo.get_size()
- self.screen.blit(logo, (320 - width / 2, 200))
- pygame.display.flip()
- def show_play_pause(self, type='player'):
- '''显示播放按钮'''
- print type
- if type == 'player':
- player = pygame.image.load("image/player.png").convert_alpha()
- else:
- player = pygame.image.load("image/pause.png").convert_alpha()
- width, height = player.get_size()
- self.screen.blit(player, (320 - width / 2, 40))
- self.playerX = (320 - width / 2, 320 + width / 2)
- self.playerY = (40, 40 + height)
- pygame.display.flip()
- def text_objects(self, text, font):
- ''' font'''
- textSurface = font.render(text, True, (233,150,122))
- return textSurface, textSurface.get_rect()
- def message_diaplay(self, text):
- '''show font'''
- largeText = pygame.font.Font('font/Deng.ttf', 14)
- TextSurf, TextRect = self.text_objects(text, largeText)
- TextRect.center = ((320), (150))
- self.screen.blit(TextSurf, TextRect)
- pygame.display.update()
- class Mp3:
- def __init__(self):
- self.playTrue = False ##播放与停止
- self.playStart = False ##是否已经开始
- def load_music(self, musicPath):
- print musicPath
- pygame.mixer.init()
- self.track = pygame.mixer.music.load(musicPath.encode('utf-8'))
- def play(self):
- if self.playTrue == False:
- if self.playStart == False:
- print '[*] start play'
- pygame.mixer.music.play()
- Mywindow.message_diaplay(u'正在播放: %s' % musicList[PLAY_START])
- self.playTrue = True
- self.playStart = True
- Mywindow.show_play_pause(type="pause")
- else:
- print '[*] start unpause'
- pygame.mixer.music.unpause()
- Mywindow.message_diaplay(u'正在播放: %s' % musicList[PLAY_START])
- self.playTrue = True
- Mywindow.show_play_pause(type="pause")
- else:
- print '[*] start pause'
- pygame.mixer.music.pause()
- Mywindow.message_diaplay(u'暂停播放: %s' % musicList[PLAY_START])
- self.playTrue = False
- Mywindow.show_play_pause()
- def getmusicList():
- print '[*] get music pool!!'
- musicFileList= os.listdir(MUSIC_PATH)
- # print file_lists
- ## get music type
- musicList = []
- for v in musicFileList:
- print u'[*] this song is %s' % v.decode('gbk')
- v = v.decode('gbk')
- file = os.path.splitext(v)
- filename, type = file
- print '[*] filename is %s' % filename
- print '[*] type is %s' % type
- ## 判断当前类型是否存在可以播放的类型
- if type.lower() in PLAY_TYPE:
- print '[*] this song we can play!!'
- musicList.append(v)
- else:
- print '[*] this song we can not play!!'
- print '[*] this musiclist is ', musicList
- return musicList
- def changeMusic():
- print '[*] change music !!'
- if __name__ == '__main__':
- print ''' ____ __ __ __ __ _______ _______
- /__ \\ \\_\\/_/ / / / /____ / ___ / / ___ /
- / /_/ / \\__/ / /___ / /__ / / / / / / / / /
- / ____/ / / / /___/ / / / / / /__/ / / / / /
- /_/ /_/ /_/___/ /_/ /_/ \\_____/ /_/ /_/'''
- musicList = getmusicList()
- Mywindow = window()
- mp3Player = Mp3()
- ## exit()
- if AUTO_PALY:
- print '[*] auto play!!'
- ## default load first song
- mp3Player.load_music(MUSIC_PATH + musicList[PLAY_START])
- ## play
- mp3Player.play()
- else:
- print '[*] no auto paly!!'
- while True:
- # 游戏主循环
- for event in pygame.event.get():
- if event.type == QUIT:
- # 接收到退出事件后退出程序
- print 'Good Bye~~'
- exit()
- elif event.type == MOUSEBUTTONDOWN:
- pressed_array = pygame.mouse.get_pressed()
- for index in range(len(pressed_array)):
- if pressed_array[index]:
- if index == 0:
- if Mywindow.playerX[0] < event.pos[0] < Mywindow.playerX[1] and Mywindow.playerY[0] < \
- event.pos[1] < Mywindow.playerY[1]:
- print '[*] click this player!!'
- ## 默认打开第一首歌曲
- if mp3Player.playStart:
- mp3Player.play()
- else:
- mp3Player.load_music(MUSIC_PATH + musicList[PLAY_START])
- mp3Player.play()
- elif Mywindow.prveX[0] < event.pos[0] < Mywindow.prveX[1] and Mywindow.prveY[0] < \
- event.pos[1] < Mywindow.prveY[1]:
- print '[*] click this prve!!'
- if mp3Player.playStart:
- if PLAY_START == 0:
- print '[*] no song to prve_play'
- else:
- PLAY_START -= 1
- mp3Player = Mp3()
- mp3Player.load_music(MUSIC_PATH + musicList[PLAY_START])
- mp3Player.play()
- else:
- print '[*] no song is play!!'
- elif Mywindow.nextX[0] < event.pos[0] < Mywindow.nextX[1] and Mywindow.nextY[0] < \
- event.pos[1] < Mywindow.nextY[1]:
- print '[*] click this next!!'
- if mp3Player.playStart:
- if PLAY_START == len(musicList)-1:
- print '[*] no song to next_play'
- else:
- PLAY_START += 1
- mp3Player = Mp3()
- mp3Player.load_music(MUSIC_PATH + musicList[PLAY_START])
- mp3Player.play()
- else:
- print '[*] no song is play!!'
- '''
- some problems:
- 1、pygame 写的字如何更新值,而不是在同一位置再写入
- '''
运行效果:
还有部分问题需要解决,当然也只能玩玩~
pygame 简单播放音乐程序的更多相关文章
- Python使用Pygame.mixer播放音乐
Python使用Pygame.mixer播放音乐 frequency这里是调频率... 播放网络中的音频: #!/usr/bin/env python # -*- coding: utf-8 -*- ...
- python3用pygame实现播放音乐文件
import pygameimport time #导入音乐文件file = r'C:\1.wav'pygame.mixer.init()track = pygame.mixer.music.load ...
- 用PHP+H5+Boostrap做简单的音乐播放器(进阶版)
前言:之前做了一个音乐播放器(纯前端),意外的受欢迎,然后有人建议我把后台一起做了,正好也想学习后台,所以学了两天php(不要吐槽我的速度,慢工出细活嘛~)然后在之前的基础上也又完善了一些功能,所以这 ...
- 简单的音乐播放器(VS 2010 + Qt 4.8.5)
昨天历经千辛万苦,配置好了VS 2010中的Qt环境(包括Qt for VS插件),今天决定浅浅地品味一下将两者结合进行编程的魅力. 上网查了一些资料,学习了一些基础知识,决定做一个简单的音乐播放器, ...
- C#播放音乐,调用程序
一:C# 播放音乐 string sound = Application.StartupPath + "/sound/msg.wav"; //Application.Startup ...
- Android之通过网络播放一首简单的音乐
首先,附上程序执行后的效果.例如以下图所看到的: 一.部署一个web项目到tomcatserver上: 1.这个小程序是结合网络来播放一首音乐的,首先,把我们搞好的一个web项目放置在tomcat安装 ...
- 使用Service组件实现简单的音乐播放器功能 --Android基础
1.本例利用Service实现简单的音乐播放功能,下面是效果图.(点击开始播放开启服务,音乐播放,点击“停止播放”关闭服务,音乐停止播放.) 2.核心代码: MusicService.java: pa ...
- swift3.0 简单直播和简单网络音乐播放器
本项目采用swift3.0所写,适配iOS9.0+,所有界面均采用代码布局. 第一个tab写的是简单直播,传统MVC模式,第二个tab写的是简单网络音乐播放器.传说MVVM模式(至于血统是否纯正我就不 ...
- matlab播放音乐
最近在做计算,写了一些matlab代码,脑壳还疼,所以决定发挥一下逗B精神,写一个程序玩一下. 想了想,既然写代码的时候喜欢听歌,而且我的电脑打开网易音乐的速度巨慢(不知道为什么..),那些一个程序直 ...
随机推荐
- MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example 1.4 Labeling the Map
MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example 1.4 Labeling the Map 一.前言 MapServer拥有非常灵活的标签 ...
- iOS sqlite大数据分段加载的实现,sqlite数据库的操作
数据库管理类(自己封装的,挺简单的) // // MyDataBaseManger.m // DB_Test // // Created by admin on 17/2/7. // Copy ...
- SQL Server--------SQL Server问题错误解决
1.错误提示: 修改字段属性时,提示: 消息 5074,级别 16,状态 1,第 1 行对象'DF__dms_deliv__sync___51BAE991' 依赖于 列'sync_ff_result_ ...
- LNMP(二)
第二十一课 LNMP(二) 目录 一.默认虚拟主机 二.Nginx用户认证 三.Nginx域名重定向 四.Nginx访问日志 五.Nginx日志切割 六.静态文件不记录日志和过期时间 七.Nginx防 ...
- Codeforces Round #281 (Div. 2) D(简单博弈)
题目:http://codeforces.com/problemset/problem/493/D 题意:一个n*n的地图,有两个人在比赛,第一个人是白皇后开始在(1,1)位置,第二个人是黑皇后开始在 ...
- VBS下载者助以一臂之力
当拿到shell到手,服务器是内网,你又没有条件映射,服务器又穿不上东西 是不是很郁闷,还有我们还有vbs,能执行cmd命令就有希望 一.VBS下载者: 复制代码 代码如下: Set Post = C ...
- Echarts tooltip 坐标值修改
tooltip: { trigger: 'axis', position:function(p){ //其中p为当前鼠标的位置 console.log(p); ] + , p[] - ]; } },
- keil的可烧写hex文件生成
右键Target1 Options Target for ‘Target1’ ...->Output->Create Executable:->Create HEX File Bui ...
- 关于Error during managed flush [Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1]错误
控制台报错: 08:07:09.293 [http-bio-8080-exec-2] ERROR org.hibernate.internal.SessionImpl - HHH000346: Err ...
- 框架tensorflow1
TensorFlow 1 分类: 1,protocol Buffer 处理结构化数据工具: (xml,json) 2,Bazel 自动化构建工具, 编译: tensor 张量: ...