代码之家  ›  专栏  ›  技术社区  ›  John O'Neil

在Android上播放mp3?

  •  1
  • John O'Neil  · 技术社区  · 7 年前

    我正试图将Kivy的音频播放器作为个人项目,但我注意到我的应用程序无法加载mp3音频。经过一点研究,它似乎与一些许可问题有关?在任何情况下,机器人仍然可以播放mp3文件,当然,那么我该怎么做才能做到这一点呢?

    有谁知道有什么变通方法,因为我似乎找不到?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Patrick    7 年前

    kivy SoundLoader类有一些问题(例如,它在某些mp3文件中无法正确查找)。正如Joey提到的,jnius可以访问的原始android类工作得更好,并且可以处理大多数歌曲文件(mp3、mp4、flac、waves等)

    我创建了两个工作类,一个用于使用jnius的android,另一个用于windows(当然,android类在这里不起作用)。

    而且:kivy logger在android上的unicode上有一些问题,在打印时抛出了一个异常。不过声音很好听。如果这个异常让你恼火,删除kivy。信息行。

    #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()
    
        2
  •  1
  •   Joey daniel darko    7 年前