如何不陌路 2016-12-10 06:34 采纳率: 100%
浏览 1388
已结题

Android Mediaplayer报错

顺序播放五六首歌曲的时候机会出现错误
(-38,0);
Attempt to call getDuation withot a valid mediaplayer
下面代码
class OnCompletionListener implements android.media.MediaPlayer.OnCompletionListener {

    @Override
    public void onCompletion(MediaPlayer mp) {
        if (currIndex >= (list.size() - 1)) {
            statethreadFlag = true;
            mp.stop();
            Toast.makeText(getApplicationContext(), "歌曲全部播放完毕", Toast.LENGTH_LONG).show();
        } else {
            statethreadFlag = true;
            mp.stop();
            currIndex++;
            start();
        }
    }
}
public void start() {
    Log.i("mp3", "服务执行了start方法,开始播放歌曲");
    if (bundle.getBoolean("flag")) {
        hMap = list.get(currIndex);
        player.reset();
        try {
            player.setDataSource(hMap.get("path"));
            player.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
        player.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.start();
                serviceFlag = 1;
                statethreadFlag = false;
                new Thread(new StateThread()) {
                }.start();
            }
        });
        class StateThread implements Runnable {
    @Override
    public void run() {
        Log.i("mp3", "服务线程启动");
        while (!statethreadFlag) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }               
            nowTime = player.getCurrentPosition();
            allTime = player.getDuration();
            Log.i("mp3", nowTime + " " + allTime + "");
            Intent stateIntent = new Intent();
            Bundle bundle = new Bundle();
            stateIntent.setAction("com.player.broadcast");
            bundle.putInt("now", nowTime);
            bundle.putInt("all", allTime);
            bundle.putInt("serviceFlag", serviceFlag);
            bundle.putInt("currIndex", currIndex);
            stateIntent.putExtra("state", bundle);
            sendBroadcast(stateIntent);

            if (nowTime >= allTime) {
                break;
            }

        }
    }
}
网上找了一天也没解决这个问题
出错了取得的nowTime和allTime 都是-1261004135
希望大神指教,感激不尽,只剩1c币了
  • 写回答

1条回答 默认 最新

  • qq_36522073 2016-12-10 07:26
    关注

    应该是下标问题,链表的长度没弄好,取总时间的时候没有对应的mediaPlayer播放歌曲 估计是

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料