Android音乐播放器寻求(pos)在错误的位置播放音乐

时间:2015-01-13 08:03:02

标签: android position seekbar seek audio-player android-music-player

在我的应用程序中,我正在从服务器传输Mp3文件。流媒体和负载完美运行。但是媒体播放器中寻求的问题,

像我有2分钟的mp3,如果我已经加载它并且玩,猜缓冲去100%, 在玩了50%(意味着大约1分钟)后,我们寻求25%(意味着30秒), 玩家将从10秒开始,这是音乐播放器非常奇怪的行为

我已经尝试了很多,但我在这里找不到任何解决方案,我现在感到沮丧,请帮助我谢谢

我的代码在

下面

启动

MediaPlayer mPlayer = new MediaPlayer();

mPlayer.setWakeMode(_context, PowerManager.PARTIAL_WAKE_LOCK);
mPlayer.setOnPreparedListener(this);
mPlayer.setOnCompletionListener(this);
mPlayer.setOnErrorListener(this);
mPlayer.setOnInfoListener(this);
mPlayer.setOnBufferingUpdateListener(this);

musicseekbar =(SeekBar)findViewById(R.id.seekbar); 
        currenttime = (TextView)findViewById(R.id.runningtime);
        totaltime = (TextView)findViewById(R.id.totaltime);
        setSeekbar();


try {
    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (Exception e) {
}
mPlayer.setDataSource(manualUrl);
mPlayer.prepareAsync();

其他stuf

       private void setSeekbar() {
        // TODO Auto-generated method stub
        musicseekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub

                if (fromUser) {

                    if (mPlayer != null && mPlayer.isPlaying()) {
                        MusicSeekto(progress,Globals.PlayerDuration);
                        seekBar.setProgress(progress);
                    }
                }
            }
        });
    }

    public void setSeekBarpositionDetail(int position,int currenttime1,int totaltime1) {
        // TODO Auto-generated method stub
        if (musicseekbar != null) {
            musicseekbar.setProgress(currenttime1);
        }
    }


    public void setPlayerInfo(int duration) {

        musicseekbar.setMax(duration); // set maximum here
    }


    public void MusicSeekto(int i, int playerDuration) {
        // TODO Auto-generated method stub

        if (mPlayer != null) {
            mPlayer.seekTo(i);  //problem is here , I get here exact "i", but  seekto(pos) does't start from that position.

        }
    }

        public void primarySeekBarProgressUpdater() {

        if (mPlayer != null) {
            stop = (int) (((float) mPlayer.getCurrentPosition() / Globals.PlayerDuration) * 100);

            setSeekBarpositionDetail(stop, mPlayer.getCurrentPosition(), mPlayer.getDuration());
        }

        if (mPlayer != null) {
            if (mPlayer.isPlaying()) {

                notification = new Runnable() {
                    public void run() {
                        primarySeekBarProgressUpdater();
                    }
                };
                handler.postDelayed(notification, 100);
            }
        }
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        // TODO Auto-generated method stub
        playerstarting();
        primarySeekBarProgressUpdater();
    }

    private void playerstarting() {
        // TODO Auto-generated method stub

        if (mPlayer != null) {
            mPlayer.start();
        }
        Globals.PlayerDuration = mPlayer.getDuration();
        changingImage(false);
        setPlayerInfo(mPlayer.getDuration());// seekbar value passing from here
    }

1 个答案:

答案 0 :(得分:0)

MediaPlayer.seekto不占百分比。这需要几毫秒。然而,你的百分比在传递。那不行。相反,尝试传入

((float)(i*mPlayer.getDuration()))/100.0