最近、NetEase Cloud Musicは多くの問題を引き起こし、主要なアプリケーションストアからも削除されました。その慣習のいくつか、Xiaobencongも本当に同意しませんが、修正後はもちろん、ストーリースタイルのホットコメントだけでなく、より重要な著作権問題も含まれるようになることを期待しています。
このことから、Xiaobencongはシンプルな音楽プレーヤーを作るというアイデアも生み出しました。最初にレンダリングを確認します。
WeChatパブリックアカウントビデオデモリンク
主な実装は主にPyQt5を使用しており、PyQt5に精通しているパートナーは、次のプログラムを読むときに理解するのが難しくないはずです。最初にレイアウトインターフェイスのボタンを定義してから、関数の実装を記述します。
1.コードの紹介
1.インターフェイス要素を定義する
各インターフェースで使用されるボタン要素を定義するだけです。再生/一時停止、前/次の曲、ループモード、フォルダーを開くなど。
1def __initialize(self):
2 self.setWindowTitle('音乐播放器-学编程的金融客(小笨聪)')
3 self.setWindowIcon(QIcon('icon.ico'))
4 self.songs_list = []
5 self.song_formats = ['mp3', 'm4a', 'flac', 'wav', 'ogg']
6 self.settingfilename = 'setting.ini'
7 self.player = QMediaPlayer()
8 self.cur_path = os.path.abspath(os.path.dirname(__file__))
9 self.cur_playing_song = ''
10 self.is_switching = False
11 self.is_pause = True
12 # 界面元素
13 # 播放时间
14 self.label1 = QLabel('00:00')
15 self.label1.setStyle(QStyleFactory.create('Fusion'))
16 self.label2 = QLabel('00:00')
17 self.label2.setStyle(QStyleFactory.create('Fusion'))
18 # 滑动条
19 # 播放按钮
20 # 上一首按钮
21 # 下一首按钮
22 # 打开文件夹按钮
23 # 显示音乐列表
24 # 播放模式
25 # 计时器
26 # 界面布局
27 self.grid = QGridLayout()
28 self.setLayout(self.grid)
29 self.grid.addWidget(self.next_button, 1, 11, 1, 2)
30 self.grid.addWidget(self.preview_button, 2, 11, 1, 2)
31 self.grid.addWidget(self.cmb, 3, 11, 1, 2)
32 self.grid.addWidget(self.open_button, 4, 11, 1, 2)
2.音楽が保存されているフォルダを選択します
pyqt5の対応する関数を直接呼び出すだけです。
1def openDir(self):
2 self.cur_path = QFileDialog.getExistingDirectory(self, "选取文件夹", self.cur_path)
3 if self.cur_path:
4 self.showMusicList()
5 self.cur_playing_song = ''
6 self.setCurPlaying()
7 self.label1.setText('00:00')
8 self.label2.setText('00:00')
9 self.slider.setSliderPosition(0)
10 self.is_pause = True
11 self.play_button.setText('播放')
フォルダを開いたら、インターフェイスの左側にすべての音楽ファイルを表示し、必要な情報をいくつか保存します。
1def showMusicList(self):
2 self.qlist.clear()
3 self.updateSetting()
4 for song in os.listdir(self.cur_path):
5 if song.split('.')[-1] in self.song_formats:
6 self.songs_list.append([song, os.path.join(self.cur_path, song).replace('\\', '/')])
7 self.qlist.addItem(song)
8 self.qlist.setCurrentRow(0)
9 if self.songs_list:
10 self.cur_playing_song = self.songs_list[self.qlist.currentRow()][-1]
3.音楽プレーヤー
1# 设置当前播放的音乐
2def setCurPlaying(self):
3 self.cur_playing_song = self.songs_list[self.qlist.currentRow()][-1] self.player.setMedia(QMediaContent(QUrl(self.cur_playing_song)))
4# 播放音乐
5def playMusic(self):
6 if self.qlist.count() == 0:
7 self.Tips('当前路径内无可播放的音乐文件')
8 return
9 if not self.player.isAudioAvailable():
10 self.setCurPlaying()
11 if self.is_pause or self.is_switching:
12 self.player.play()
13 self.is_pause = False
14 self.play_button.setText('暂停')
15 elif (not self.is_pause) and (not self.is_switching):
16 self.player.pause()
17 self.is_pause = True
18 self.play_button.setText('播放')
4.音楽スイッチ
音楽を切り替える方法は3つあります。つまり、前の/次の曲をクリックします。
1 # 上一首
2 def previewMusic(self):
3 self.slider.setValue(0)
4 if self.qlist.count() == 0:
5 self.Tips('当前路径内无可播放的音乐文件')
6 return
7 pre_row = self.qlist.currentRow()-1 if self.qlist.currentRow() != 0 else self.qlist.count() - 1
8 self.qlist.setCurrentRow(pre_row)
9 self.is_switching = True
10 self.setCurPlaying()
11 self.playMusic()
12 self.is_switching = False
13 # 下一首
14 def nextMusic(self):
15 self.slider.setValue(0)
16 if self.qlist.count() == 0:
17 self.Tips('当前路径内无可播放的音乐文件')
18 return
19 next_row = self.qlist.currentRow()+1 if self.qlist.currentRow() != self.qlist.count()-1 else 0
20 self.qlist.setCurrentRow(next_row)
21 self.is_switching = True
22 self.setCurPlaying()
23 self.playMusic()
24 self.is_switching = False
曲をダブルクリックします。
1def doubleClicked(self):
2 self.slider.setValue(0)
3 self.is_switching = True
4 self.setCurPlaying()
5 self.playMusic()
6 self.is_switching = False
再生モードを設定します。
1 def playByMode(self):
2 if (not self.is_pause) and (not self.is_switching):
3 self.slider.setMinimum(0)
4 self.slider.setMaximum(self.player.duration())
5 self.slider.setValue(self.slider.value() + 1000)
6 self.label1.setText(time.strftime('%M:%S', time.localtime(self.player.position()/1000)))
7 self.label2.setText(time.strftime('%M:%S', time.localtime(self.player.duration()/1000)))
8 # 顺序播放
9 if (self.cmb.currentIndex() == 0) and (not self.is_pause) and (not self.is_switching):
10 if self.qlist.count() == 0:
11 return
12 if self.player.position() == self.player.duration():
13 self.nextMusic()
14 # 单曲循环
15 elif (self.cmb.currentIndex() == 1) and (not self.is_pause) and (not self.is_switching):
16 if self.qlist.count() == 0:
17 return
18 if self.player.position() == self.player.duration():
19 self.is_switching = True
20 self.setCurPlaying()
21 self.slider.setValue(0)
22 self.playMusic()
23 self.is_switching = False
24 # 随机播放
25 elif (self.cmb.currentIndex() == 2) and (not self.is_pause) and (not self.is_switching):
26 if self.qlist.count() == 0:
27 return
28 if self.player.position() == self.player.duration():
29 self.is_switching = True
30 self.qlist.setCurrentRow(random.randint(0, self.qlist.count()-1))
31 self.setCurPlaying()
32 self.slider.setValue(0)
33 self.playMusic()
34 self.is_switching = False
直接の使用を容易にするために、pyInstallerのパッケージのXiaobenコング最後に、Pythonのソースがします、EXEファイルにパッケージ化され、あなたが任意のPythonのコンパイラ環境は必要ありませんコンピュータ上で直接使用することができますが!
第二に、デモの使用
WeChatパブリックアカウントビデオデモリンク
以上がこの音楽プレイヤーの作成プロセスですWeChat公開アカウント「金融学習者がプログラミングを学ぶ」バックエンド「 音楽プレイヤー」でソースコードを入手。【終了】
過去におすすめ
2. 放浪地球映画レビュー
あなたの「いいね」と注意が私の最大のサポートです!
スキャンコードを保存し、公開番号に注意してください