在 Android 上播放 mp3? [英] Playing mp3 on Android?

查看:33
本文介绍了在 Android 上播放 mp3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Kivy 制作一个音频播放器作为个人项目,但是我注意到我的应用程序无法加载 mp3 音频.经过一番研究,它似乎与某些许可问题有关?在任何情况下,机器人当然仍然可以播放 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?

推荐答案

kivy SoundLoader 类存在一些问题(例如,它在某些 mp3 文件中无法正确搜索).正如 Joey 提到的,jnius 可以访问的原始 android 类效果更好,并且可以使用大多数歌曲文件(mp3、mp4、flac、waves 等)

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).

您可能需要更新到最新的 kivy 并添加 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆