代码之家  ›  专栏  ›  技术社区  ›  mobibob

Android媒体播放器无法播放MP3,但不支持pvmferr?

  •  1
  • mobibob  · 技术社区  · 14 年前

    我有一个下载的MP3,我可以验证为MP3的亚行拉到我的Mac或PC,并在其各自的媒体播放器播放。然而,来自Android的错误意味着它不支持这种格式。我认为这可能与我存储文件的目录位置有关,但这没有意义(/data/data/com.myapp/files/file_audio.mp3)。这是AsyncTask中的代码:

        protected String doInBackground(String... params) {
    
            String filename = params[0];
    
            try {
                StringBuilder filenameMp3 = new StringBuilder().append(context.getFilesDir());
                filenameMp3.append("/").append(filename);
    
                Uri uriMp3 = Uri.parse(filenameMp3.toString());
                Log.d(TAG, "Listen to this ringtone! [" + uriMp3.toString() + "]");
    
                MediaPlayer mp = MediaPlayer.create(context, uriMp3);
                mp.prepare();
                mp.start();
            } catch (IllegalStateException e) {
                Log.e(TAG, e.toString());
                e.printStackTrace();
            } catch (IOException e) {
                Log.e(TAG, e.toString());
                e.printStackTrace();
            }
    
            return null;
        }
    

    这是日志:

    09-07 23:16:09.617 E/MediaPlayerService( 1128): The Default Player is PV_PLAYER***********************
    09-07 23:16:09.617 V/PVPlayer( 1128): PVPlayer constructor
    09-07 23:16:09.617 V/PVPlayer( 1128): construct PlayerDriver
    09-07 23:16:09.617 V/PlayerDriver( 1128): constructor
    09-07 23:16:09.627 V/PlayerDriver( 1128): OpenCore hardware module loaded
    09-07 23:16:09.627 V/PlayerDriver( 1128): start player thread
    09-07 23:16:09.627 V/PlayerDriver( 1128): startPlayerThread
    09-07 23:16:09.627 V/PlayerDriver( 1128): InitializeForThread
    09-07 23:16:09.627 V/PlayerDriver( 1128): OMX_MasterInit
    09-07 23:16:09.647 V/PlayerDriver( 1128): OsclScheduler::Init
    09-07 23:16:09.647 V/PlayerDriver( 1128): CreatePlayer
    09-07 23:16:09.657 V/PlayerDriver( 1128): AddToScheduler
    09-07 23:16:09.657 V/PlayerDriver( 1128): PendForExec
    09-07 23:16:09.657 V/PlayerDriver( 1128): OsclActiveScheduler::Current
    09-07 23:16:09.657 V/PlayerDriver( 1128): StartScheduler
    09-07 23:16:09.657 V/PVPlayer( 1128): send PLAYER_SETUP
    09-07 23:16:09.667 V/PlayerDriver( 1128): Send player code: 2
    09-07 23:16:09.667 V/PlayerDriver( 1128): CommandCompleted
    09-07 23:16:09.667 V/PlayerDriver( 1128): Completed command PLAYER_SETUP status=PVMFSuccess
    09-07 23:16:09.667 V/PVPlayer( 1128): setDataSource(/data/data/com.myapp/files/audio.mp3)
    09-07 23:16:09.667 V/PVPlayer( 1128): prepareAsync
    09-07 23:16:09.667 V/PVPlayer( 1128):   data source = /data/data/com.myapp/files/audio.mp3
    09-07 23:16:09.667 V/PlayerDriver( 1128): Send player code: 3
    09-07 23:16:09.667 V/PlayerDriver( 1128): handleSetDataSource
    09-07 23:16:09.667 V/PlayerDriver( 1128): handleSetDataSource- scanning for extension
    09-07 23:16:09.667 V/PlayerDriver( 1128): HandleInformationalEvent: PVMFInfoErrorHandlingStart
    09-07 23:16:09.667 V/PlayerDriver( 1128): HandleInformationalEvent: type=26 UNHANDLED
    09-07 23:16:09.667 W/MediaPlayer(14630): info/warning (1, 26)
    09-07 23:16:09.667 I/MediaPlayer(14630): Info (1,26)
    09-07 23:16:09.667 V/PlayerDriver( 1128): CommandCompleted
    09-07 23:16:09.667 V/PlayerDriver( 1128): Completed command PLAYER_SET_DATA_SOURCE status=PVMFErrNotSupported
    09-07 23:16:09.667 E/PlayerDriver( 1128): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
    09-07 23:16:09.667 E/MediaPlayer(14630): error (1, -4)
    09-07 23:16:09.677 V/PVPlayer( 1128): run_init s=-2147483648, cancelled=0
    09-07 23:16:09.677 V/PlayerDriver( 1128): HandleInformationalEvent: PVMFInfoErrorHandlingComplete
    09-07 23:16:09.677 W/PlayerDriver( 1128): PVMFInfoErrorHandlingComplete
    

    有什么线索吗?

    3 回复  |  直到 14 年前
        1
  •  2
  •   Alex Volovoy    14 年前
    FileInputStream fis = getBaseContext().openFileInput(filename.toString());
    mp = new MediaPlayer();
    mp.setOnCompletionListener(onCompleteAudioListener);
    mp.setDataSource(fis.getFD());
    mp.prepare();
    mp.start();
    

    加上正常检查和错误处理。

        2
  •  0
  •   plugmind    14 年前

    最好检查要播放的文件是否存在,其长度是否大于0,以确保路径正确:

    File file = new File("/data/...../....mp3");
    
    boolean exists = file.exists();
    
    long length = file.length();
    

    还要确保您的URI以 file://

        3
  •  0
  •   mobibob    14 年前

    好吧-我让它起作用了,但并不像我希望的那样。首先,我必须存储在系统媒体文件夹中:

    File file = new File("/system/media/audio/ringtones/", filename);
    

    第二,我必须停止,准备,启动媒体播放器。我在网上其他地方的帖子上尝试过。

                if (exists && length > 0) {
                    mp = MediaPlayer.create(context, uriMp3);
                    mp.stop();
                    mp.prepare();
                    mp.start();
                } else {
                    if (exists) {
                        return String.format("Ringtone has length %d.", length);
                    } else {
                        return String.format("Ringtone %s is missing.", uriMp3.toString());
                    }
                }