我正在尝试为Kivy制作一个音频播放器作为个人项目但是我注意到我的应用无法加载mp3音频。经过一番研究后,似乎与某些许可问题有关?在任何情况下,机器人当然仍然可以播放mp3文件,所以我该怎么做才能实现呢?
目前我正在使用kivy的普通SoundLoader()类。我认为我的Android音频是使用sdl2但我可能会弄错,因为我不知道在哪里检查这个我只记得在某个地方看到它。我也尝试过更改KIVY_AUDIO环境变量,但它没有用(我假设我做错了)。
有没有人知道任何工作,因为我似乎找不到任何工作?
答案 0 :(得分:1)
答案 1 :(得分:1)
kivy SoundLoader类有一些问题(例如,它在某些mp3文件中没有正确搜索)。正如Joey提到的那样,jnius可以访问的原始android类工作效果更好,并且你可以使用大多数歌曲文件(mp3,mp4,flac,wave等)。
我创建了两个工作类,一个用于android,它使用jnius,一个用于windows(当然android类在这里不起作用)。
您可能需要更新到最新的kivy并添加gstreamer,如文档中所述。 并且:kivy logger在android上有unicode的一些问题,并在打印时抛出异常。虽然声音很好。如果异常使您烦恼,请删除kivy.info行。
#coding: utf-8
from kivy.core.audio import SoundLoader
from kivy.utils import platform
from kivy.logger import Logger
import time
class MusicPlayerAndroid(object):
def __init__(self):
from jnius import autoclass
MediaPlayer = autoclass('android.media.MediaPlayer')
self.mplayer = MediaPlayer()
self.secs = 0
self.actualsong = ''
self.length = 0
self.isplaying = False
def __del__(self):
self.stop()
self.mplayer.release()
Logger.info('mplayer: deleted')
def load(self, filename):
try:
self.actualsong = filename
self.secs = 0
self.mplayer.setDataSource(filename)
self.mplayer.prepare()
self.length = self.mplayer.getDuration() / 1000
Logger.info('mplayer load: %s' %filename)
Logger.info ('type: %s' %type(filename) )
return True
except:
Logger.info('error in title: %s' % filename)
return False
def unload(self):
self.mplayer.reset()
def play(self):
self.mplayer.start()
self.isplaying = True
Logger.info('mplayer: play')
def stop(self):
self.mplayer.stop()
self.secs=0
self.isplaying = False
Logger.info('mplayer: stop')
def seek(self,timepos_secs):
self.mplayer.seekTo(timepos_secs * 1000)
Logger.info ('mplayer: seek %s' %int(timepos_secs))
class MusicPlayerWindows(object):
def __init__(self):
self.secs = 0
self.actualsong = ''
self.length = 0
self.isplaying = False
self.sound = None
def __del__(self):
if self.sound:
self.sound.unload()
Logger.info('mplayer: deleted')
def load(self, filename):
self.__init__()
if type(filename) == unicode: filename = filename.encode('utf-8') #unicode does not work !
self.sound = SoundLoader.load(filename)
if self.sound:
if self.sound.length != -1 :
self.length = self.sound.length
self.actualsong = filename
Logger.info('mplayer: load %s' %filename)
return True
else:
Logger.info ('mplayer: songlength = -1 ...')
return False
def unload(self):
if self.sound != None:
self.sound.unload()
self.__init__ # reset vars
def play(self):
if self.sound:
self.sound.play()
self.isplaying = True
Logger.info('mplayer: play')
def stop(self):
self.isplaying = False
self.secs=0
if self.sound:
self.sound.stop()
Logger.info('mplayer: stop')
def seek(self, timepos_secs):
self.sound.seek(timepos_secs)
Logger.info('mplayer: seek %s' %int(timepos_secs))
def main():
songs = [
'f:\\_mp3_\\_testdir_\\file of ☠☢☣.mp3', #insert songs here
'f:\\_mp3_\\Patricks Mp3s\\electro\\Echotek - Freak Africa.mp3',
'f:\\_mp3_diverse_\\Testsuite\\flac\\01 - Jam & Spoon - Stella (Jam & Spoon Mix).flac',
'f:\\_mp3_\\P1\\1Start\\Hot Chip - boy from school.mp4'
]
Logger.info ('platform: %s' %platform)
if platform == 'win':
mplayer = MusicPlayerWindows()
elif platform == 'android':
mplayer = MusicPlayerAndroid()
else:
exit()
for s in songs:
if mplayer.load(s): # checking load, seek
mplayer.play()
time.sleep(2)
mplayer.seek(90)
time.sleep(2)
mplayer.stop()
mplayer.unload()
else:
Logger.info ('cant load song: %s' %s)
if __name__ == '__main__':
main()