Android MediaPlayer错误(1,-1010)

时间:2018-01-11 11:13:05

标签: android android-mediaplayer

我试图从网址流式传输音乐。 我使用SeekBar来改变歌曲的位置。 我在尝试将歌曲的位置更改为尚未下载的位置时收到错误。在其他情况下(如果完整下载的歌曲代码工作正常。

获得以下错误: E / MediaPlayerNative:错误(1,-1010) E / MediaPlayer:错误(1,-1010)

您能否告知如何解决问题。 这是代码:

    mSeekBar = findViewById(R.id.seekBar);

    if (mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer();
    }

    if (mMediaPlayer.isPlaying()) {
        mMediaPlayer.reset();
    }
    mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            return false;
        }
    });
    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });

    mMediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
        @Override
        public boolean onInfo(MediaPlayer mp, int what, int extra) {
            return false;
        }
    });
    mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.reset();
        }
    });

    mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (mMediaPlayer != null && fromUser) {

                mMediaPlayer.seekTo(progress * 1000);
                seekBar.setProgress(progress);
            }
        }
    });

    playAudio(url);
}


private void playAudio(String url) {

    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mMediaPlayer.setDataSource(url);
        mMediaPlayer.prepare();
        mSeekBar.setMax(mMediaPlayer.getDuration() / 1000);
        getAudioInfo();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private void getAudioInfo() {
    PlayMusicActivity.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
                int mCurrentPosition = mMediaPlayer.getCurrentPosition() / 1000;
                mSeekBar.setProgress(mCurrentPosition);
            }
            mHandler.postDelayed(this, 1000);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

亲爱的朋友,问题是我试图跳过尚未下载的位置。在开始寻找该位置之前,我要检查是否已下载该部分。如果不是,我等到它将被下载并在此之后寻求该位置。这是代码:

private Handler mHandlerDownloadUpdate = new Handler();
public static MediaPlayer mMediaPlayer;

    if (mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer();
    } else {
        mMediaPlayer.release();
        mMediaPlayer = new MediaPlayer();
    }

    mMediaPlayer.setOnBufferingUpdateListener(bufferingUpdateListener);
    mSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);

private void initRunnable(final int progress) {
   mRunnable = new Runnable() {
     @Override
     public void run() {
        if (progress * 100 / mDuration < mPercentOfDownload) {
            mMediaPlayer.seekTo(progress * 1000);
            mMediaPlayer.start();
            return;
        }
        mHandlerDownloadUpdate.postDelayed(this, 1000);
    }
};

MediaPlayer.OnBufferingUpdateListener bufferingUpdateListener = new 
MediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    mPercentOfDownload = percent;
    if (mDuration != 0) {
        int second = percent * mDuration / 100;
        mSeekBar.setSecondaryProgress(second);
    }
}

SeekBar.OnSeekBarChangeListener seekBarChangeListener = new     SeekBar.OnSeekBarChangeListener() {
   public void onProgressChanged(SeekBar seekBar, final int progress, 
   boolean fromUser) {
    if (mMediaPlayer != null && fromUser) {
        if (mMediaPlayer.isPlaying()) {
            mMediaPlayer.pause();
            seekBar.setProgress(progress);
            int a = (progress * 100 / mDuration);
            if (a < mPercentOfDownload) {
                mMediaPlayer.seekTo(progress * 1000);
                mMediaPlayer.start();
            } else {
                mHandlerDownloadUpdate.removeCallbacks(mRunnable);
                initRunnable(progress);
                PlayMusicActivity.this.runOnUiThread(mRunnable);
            }
        } 
    }
}