在Android中使用媒体播放器播放音频时出现未知错误

时间:2013-04-11 17:25:57

标签: android media-player

在我的应用中,我必须播放外部存储中的歌曲。它工作正常,但有时媒体播放器停止没有错误日志或警告。这是我的logcat出现时:

04-12 11:18:31.528: D/jdwp(12465): sendBufferedRequest : len=0x39
04-12 11:18:31.541: D/jdwp(12465): sendBufferedRequest : len=0x3B
04-11 22:39:16.320: D/dalvikvm(16048): threadid=10: interp stack at 0x4bf6e000

04-11 22:39:16.326: D/jdwp(16048): sendBufferedRequest : len=0x3B

04-11 22:39:16.436: D/dalvikvm(16048): threadid=11: interp stack at 0x4bfa8000

04-11 22:39:16.436: D/dalvikvm(16048): threadid=11: calling run()

04-11 22:39:16.437: D/dalvikvm(16048): threadid=12: interp stack at 0x4bfb0000

04-11 22:39:16.437: D/dalvikvm(16048): threadid=12: calling run()

04-11 22:39:16.476: D/dalvikvm(16048): threadid=13: interp stack at 0x4bfb8000

04-11 22:39:16.476: D/dalvikvm(16048): threadid=13: calling run()

04-11 22:39:16.486: D/dalvikvm(16048): threadid=13: exiting

04-11 23:42:26.737: D/dalvikvm(3500): threadid=13: bye!

04-11 23:42:26.737: D/dalvikvm(3500): threadid=0: freeing

04-11 23:42:26.753: D/ACRA(3500): Using default Report Fields

04-11 23:42:26.765: D/ACRA(3500): Looking for error files in /data/data/com.pakagename/files

我正在尝试使用media.setdatasource(file.getFD)作为某个主题的建议,但它不起作用 我需要你的帮助,谢谢

以下是创建mediaplayer时的代码:

try {
        try {
            Log.d("file music ", musicObject.getFilePath());
            FileInputStream fileInputStream = new FileInputStream(musicObject.getFilePath());
            mediaPlayer.setDataSource(fileInputStream.getFD());

        } catch (IOException e1) {

            e1.printStackTrace();
        }
        mediaPlayer.playlistEntry = musicObject;

        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.e("",
                        "onCompletion getPlaylistPlaybackMode "
                                + mPlaylist.getPlaylistPlaybackMode()
                                + " seleected track id "
                                + mPlaylist.getSelectedIndex());
                mMPHandler.sendEmptyMessageDelayed(MSG_MP_RELEASE, 500);
            }

        });

        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.preparing = false;

                // we may start playing
                if (mPlaylist.getSelectedTrack() == mediaPlayer.playlistEntry
                        && mediaPlayer.playAfterPrepare) {
                    mediaPlayer.playAfterPrepare = false;
                    play();
                    Log.d("onprepared", "true");
                }

            }

        });

        mediaPlayer
                .setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

                    @Override
                    public void onBufferingUpdate(MediaPlayer mp,
                            int percent) {
                        if (mPlayerEngineListener != null) {
                            mPlayerEngineListener.onTrackBuffering(percent);
                        }
                    }

                });

        mediaPlayer.setOnErrorListener(new OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.w(UILApplication.TAG, "PlayerEngineImpl fail, what ("
                        + what + ") extra (" + extra + ")");
                CommonUtils.writetoFile( "PlayerEngineImpl fail, what ("
                        + what + ") extra (" + extra + ")", context);

                if (what == MediaPlayer.MEDIA_ERROR_UNKNOWN) {

                    if (mPlayerEngineListener != null) {
                        mPlayerEngineListener.onTrackStreamError();
                    }
                    stop();
                    return true;
                }

                if (what == -1) {
                    long failTime = System.currentTimeMillis();
                    if (failTime - mLastFailTime > FAIL_TIME_FRAME) {

                        mTimesFailed = 1;
                        mLastFailTime = failTime;
                        Log.w(UILApplication.TAG, "PlayerEngineImpl "
                                + mTimesFailed
                                + " fail within FAIL_TIME_FRAME");
                    } else {

                        mTimesFailed++;
                        if (mTimesFailed > ACCEPTABLE_FAIL_NUMBER) {
                            Log.w(UILApplication.TAG,
                                    "PlayerEngineImpl too many fails, aborting playback");
                            if (mPlayerEngineListener != null) {
                                mPlayerEngineListener.onTrackStreamError();
                            }
                            stop();
                            return true;
                        }
                    }
                }
                return false;
            }
        });

        mediaPlayer.preparing = true;
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {

            e.printStackTrace();
        }

        if (mPlayerEngineListener != null) {
            mPlayerEngineListener.onTrackChanged(mPlaylist
                    .getSelectedTrack());
        }

        return mediaPlayer;
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }

玩游戏时:

if (mCurrentMediaPlayer != null
                && mCurrentMediaPlayer.musicObject != mPlaylist
                        .getSelected()) {
            try {
            mCurrentMediaPlayer.stop();
        } catch (IllegalStateException e) {
            // this may happen sometimes
        } finally {
            mCurrentMediaPlayer.release();
            mCurrentMediaPlayer = null;
        }
            mCurrentMediaPlayer = build(mPlaylist.getSelectedTrack());
        }

oncompletionListener中的Handeler:

protected Handler mMPHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if(msg.what == MSG_MP_RELEASE) {
            if (mPlaylist.getPlaylistPlaybackMode() == PlaylistPlaybackMode.SHUFFLE_AND_REPEAT1
                    || mPlaylist.getPlaylistPlaybackMode() == PlaylistPlaybackMode.REPEAT1) {
                Log.e("",
                        "onCompletion getPlaylistPlaybackMode "
                                + mPlaylist.getPlaylistPlaybackMode()
                                + " seleected track id "
                                + mPlaylist.getSelectedIndex());
                play(mPlaylist.getSelectedIndex());
            } else if (!mPlaylist.isLastTrackOnList()
                    || mPlaylist.getPlaylistPlaybackMode() == PlaylistPlaybackMode.REPEAT
                    || mPlaylist.getPlaylistPlaybackMode() == PlaylistPlaybackMode.SHUFFLE_AND_REPEAT) {
                next();
            } else

            {
                stop();
            }
        }
    }
};

编辑:现在使用,我不知道发生了什么。它出现在我做一些或很多背景动作的侧面应用程序

 04-12 11:18:31.528: D/jdwp(12465): sendBufferedRequest : len=0x39
04-12 11:18:31.541: D/jdwp(12465): sendBufferedRequest : len=0x3B

也许是问题

编辑:我在android deverloper网页上找到了一些信息:

  

只有在内存不足时,Android系统才会强制停止服务   它必须为具有用户的活动恢复系统资源   焦点。如果服务绑定到具有用户焦点的活动,   然后它不太可能被杀死,如果声明服务   在前台运行(稍后讨论),然后它几乎永远不会   杀害。否则,如果服务已启动并且长时间运行,   然后系统将降低其在背景列表中的位置   随着时间的推移任务和服务将变得非常容易受到影响   杀戮 - 如果您的服务已启动,那么您必须将其设计为   优雅地处理系统重启。如果系统杀了你的   服务,一旦资源再次可用,它就会重新启动它   (虽然这也取决于你返回的价值   onStartCommand(),稍后讨论)。有关何时的更多信息   系统可能会破坏服务,请参阅进程和线程   文档。

我怎么解决不了?

0 个答案:

没有答案