在Android装置上播放mp3吗?

编程入门 行业动态 更新时间:2024-10-26 04:29:07
本文介绍了在Android装置上播放mp3吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将Kivy的音频播放器作为个人项目制作,但是我发现我的应用无法加载mp3音频.经过一些研究,似乎与某些许可问题有关?当然,无论如何,Android仍然可以播放mp3文件,那么该怎么办呢?

I am trying to make an audio player for Kivy as a personal project however I noticed that my app fails to load mp3 audio. After a little research it seems to be related to some licensing issue? In any case androids can still play mp3 files of course so what can I do to make this happen?

按现状,我使用的是来自kivy的普通SoundLoader()类.我认为我的Android音频使用的是sdl2,但我可能会误认为我不确定该在哪里检查,我只是记得在某处看到过它.我还尝试过更改KIVY_AUDIO环境变量,但是它没有用(我以为我做错了).

As it stands I am using the normal SoundLoader() class from kivy. I think my android audio is using sdl2 but I could be mistaken as I am not sure where to check this I just remember seeing it somewhere. I have also tried changing the KIVY_AUDIO environment variable but it didn't work (I assume I did something wrong).

有人因为我似乎找不到任何解决方法而知道任何解决方法吗?

Does anyone know of any work arounds because I can't seem to find any?

推荐答案

奇异的SoundLoader类存在一些问题(例如,在某些mp3文件中无法正确搜索).正如Joey提到的那样,jnius可以访问原始的android类,效果更好,并且可以将大多数歌曲文件(mp3,mp4,flac,wave等)扔给它.

the kivy SoundLoader class has some problems (eg. it does not seek correctly in certain mp3 files). As Joey mentions the original android class which can be accessed by jnius works better and with most songfiles you throw at it (mp3, mp4, flac,waves etc.)

我制作了两个工作类,一个工作类用于使用jnius的android,另一个工作于Windows(当然android类在这里不起作用).

I made two working classes, one for android which uses jnius and one for windows (of course the android class does not work here).

您可能必须更新到最新版本,并添加gstreamer,如文档中所述.并且:kivy logger在android上的unicode存在一些问题,并且在打印时会引发异常.声音听起来不错.如果异常使您烦恼,请删除kivy.info行.

You may have to update to the newest kivy and add gstreamer like described in the documentation. and: kivy logger has some problems with unicode on android and throws an exception when printing. sound plays fine though. If the exception annoys you, delete the kivy.info lines.

#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()

更多推荐

在Android装置上播放mp3吗?

本文发布于:2023-11-23 15:40:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:装置   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!