Utsiye Asked:2022-07-13 04:10:17 +0800 CST2022-07-13 04:10:17 +0800 CST 2022-07-13 04:10:17 +0800 CST 在线播放音乐,Python 772 我正在用 Python 语言 kivy 库创建一个媒体播放器。我想让用户搜索他想要的歌曲的名称并在线播放,但我不知道可以做到这一点的库。我只知道如果有 mp3 分辨率的歌曲本身的 url 链接可以很容易地做到这一点,但我还没有找到可以自动制作这样一个链接的资源。我使用 pygame 播放音乐,但该库没有我需要的功能。 python pygame 2 个回答 Voted Александр 2022-07-13T10:00:24+08:002022-07-13T10:00:24+08:00 如果你不深入细节,像这样) import io from threading import Thread import pygame import requests from kivy.app import App from kivy.config import Config from kivy.uix.boxlayout import BoxLayout from kivy.uix.button import Button from kivy.uix.progressbar import ProgressBar Config.set('graphics', 'width', '300') Config.set('graphics', 'height', '250') class App(App): progress_bar = None def build(self): pygame.init() pygame.mixer.init() self.progress_bar = ProgressBar() box_layout_vertical = BoxLayout( orientation='vertical', size_hint_y=None ) # BoxLayout play_button = Button( text='Eminem - Lose Yourself', size_hint_y=None, height=40 ) play_button.bind(on_press=self.play_button_on_press_handler) box_layout_vertical.add_widget(play_button) box_layout_vertical.add_widget(self.progress_bar) return box_layout_vertical def play_button_on_press_handler(self, event): Thread(target=self.play_audio).start() def get_mp3_object(self): URL = 'https://cdn3.sefon.pro/prev/zlZ2w8X97cVE7irJetLC1w/1657715725/41/Eminem%20-%20Lose%20Yourself%20%28192kbps%29.mp3' headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36', } response = requests.get(URL, headers=headers, stream=True) return response.headers['Content-Length'], response.iter_content(chunk_size=76895) def play_audio(self): bytes_object = io.BytesIO() length, content = self.get_mp3_object() for byte in content: value = int(length) / 100 print(value) self.progress_bar.value += 1 bytes_object.write(byte) # print(bytes_object.getbuffer().nbytes) bytes_object.seek(0) pygame.mixer.music.load(bytes_object) pygame.mixer.music.play(-1) def main(): App().run() if __name__ == '__main__': main() Best Answer Александр 2022-07-14T03:54:03+08:002022-07-14T03:54:03+08:00 Пример без использования pygame и requests import io from threading import Thread import pygame import requests from kivy.app import App from kivy.config import Config from kivy.uix.boxlayout import BoxLayout from kivy.uix.button import Button from kivy.uix.progressbar import ProgressBar from kivy.network.urlrequest import UrlRequest Config.set('graphics', 'width', '300') Config.set('graphics', 'height', '250') class App(App): progress_bar = None def build(self): pygame.init() pygame.mixer.init() self.progress_bar = ProgressBar() box_layout_vertical = BoxLayout( orientation='vertical', size_hint_y=None ) # BoxLayout play_button = Button( text='Eminem - Lose Yourself', size_hint_y=None, height=40 ) play_button.bind(on_press=self.play_button_on_press_handler) box_layout_vertical.add_widget(play_button) box_layout_vertical.add_widget(self.progress_bar) return box_layout_vertical def play_button_on_press_handler(self, event): Thread(target=self.play_audio).start() def get_bytes_mp3(self): URL = "http://audio.xmcdn.com/group58/M03/8D/07/wKgLc1zNaabhA__WAEJyyPUT5k4509.mp3" #URL = 'https://cdn3.sefon.pro/prev/zlZ2w8X97cVE7irJetLC1w/1657715725/41/Eminem%20-%20Lose%20Yourself%20%28192kbps%29.mp3' headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36', } response = UrlRequest(URL, req_headers=headers) response.join() print(response.result) # response = requests.get(URL, headers=headers, stream=True) #value = int(response.headers['Content-Length']) / 100 # return response.iter_content(chunk_size=int(value)) return response.result def play_audio(self): self.progress_bar.value = 0 bytes_object = io.BytesIO() array = bytearray() for byte in self.get_bytes_mp3(): array.append(byte) self.progress_bar.value += 1 # bytes_object.write(byte) bytes_object.write(array) # print(bytes_object.getbuffer().nbytes) bytes_object.seek(0) pygame.mixer.music.load(bytes_object) pygame.mixer.music.play(-1) def main(): App().run() pygame.quit() if __name__ == '__main__': main()
如果你不深入细节,像这样)