Android 媒体播放器搜索栏从停止位置开始播放

Android Media player seekbar play from stopped position

我正在尝试使用搜索栏侦听器播放歌曲。但是当我尝试拖动进度条时,它会从歌曲的开头播放。它需要从那个拖动的位置开始播放。

这是我的代码:

public class MainActivity extends Activity {

    private TextView prairText;
    private AssetFileDescriptor descriptor;
    private SeekBar seekBar;
    private MediaPlayer mp = new MediaPlayer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Keep screen active
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        findViewById(R.id.play_button).setVisibility(View.INVISIBLE);
        findViewById(R.id.pauseButton).setVisibility(View.VISIBLE);

        init();

        // Play prayer
        /*if (mp != null) {
            mp.start();
        }*/

        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (mp != null && fromUser) {
                    mp.seekTo(progress * 1000);
                }

            }
        });
        findViewById(R.id.play_button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mp != null) {
                    mp.start();
                    final Handler mHandler = new Handler();
                    //Make sure you update Seekbar on UI thread
                    MainActivity.this.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            if(mp != null){
                                int mCurrentPosition = mp.getCurrentPosition() / 1000;
                                seekBar.setProgress(mCurrentPosition);
                            }
                            mHandler.postDelayed(this, 1000);
                        }
                    });
                    findViewById(R.id.play_button).setVisibility(View.INVISIBLE);
                    findViewById(R.id.pauseButton).setVisibility(View.VISIBLE);
                }
            }
        });

        findViewById(R.id.pauseButton).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mp != null) {
                    mp.pause();
                    findViewById(R.id.pauseButton).setVisibility(View.INVISIBLE);
                    findViewById(R.id.play_button).setVisibility(View.VISIBLE);
                }
                mp.pause();
            }
        });
        findViewById(R.id.restart_button).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mp != null) {
                    mp.seekTo(0);
                    mp.start();
                    findViewById(R.id.play_button).setVisibility(View.INVISIBLE);
                    findViewById(R.id.pauseButton).setVisibility(View.VISIBLE);
                }
            }
        });

private void init() {
        prairText = (TextView) findViewById(R.id.prairText);
        seekBar = (SeekBar) findViewById(R.id.seekBar1);
        try {
            descriptor = getAssets().openFd("mysong.mp3");
            mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
            descriptor.close();
            mp.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
        seekBar.setMax(mp.getDuration());
    }

如何解决这个问题。提前致谢..

这里我做了个小动作:

  • 已将 MediaPlayer 实例移至 init() 方法

  • 更改了这一行

int mCurrentPosition = mMediaPlayer.getCurrentPosition() / 1000;

至:

int mCurrentPosition = mp.getCurrentPosition();

  • 更改了这一行

mp.seekTo(progress * 1000);

至:

mp.seekTo(progress);

最终的工作代码将是:

public class MainActivity extends Activity {

    private TextView prairText;
    private AssetFileDescriptor descriptor;
    private SeekBar seekBar;
    private MediaPlayer mp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Keep screen active
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        findViewById(R.id.play_button).setVisibility(View.INVISIBLE);
        findViewById(R.id.pauseButton).setVisibility(View.VISIBLE);

        init();

        // Play song
        if (mp != null) {
            mp.start();
        }

        final Handler mHandler = new Handler();
        //Make sure you update Seek bar on UI thread
        MainActivity.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if(mp != null){
                    int mCurrentPosition = mp.getCurrentPosition();
                    seekBar.setProgress(mCurrentPosition);
                }
                mHandler.postDelayed(this, 1000);
            }
        });

        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (mp != null && fromUser) {
                    mp.seekTo(progress);
                    mp.start();
                    findViewById(R.id.play_button).setVisibility(View.INVISIBLE);
                    findViewById(R.id.pauseButton).setVisibility(View.VISIBLE);
                }

            }
        });
        findViewById(R.id.play_button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mp != null) {
                    mp.start();
                    findViewById(R.id.play_button).setVisibility(View.INVISIBLE);
                    findViewById(R.id.pauseButton).setVisibility(View.VISIBLE);
                }
            }
        });

        findViewById(R.id.pauseButton).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mp != null) {
                    mp.pause();
                    findViewById(R.id.pauseButton).setVisibility(View.INVISIBLE);
                    findViewById(R.id.play_button).setVisibility(View.VISIBLE);
                }
                mp.pause();
            }
        });
        findViewById(R.id.restart_button).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mp != null) {
                    mp.seekTo(0);
                    mp.start();
                    findViewById(R.id.play_button).setVisibility(View.INVISIBLE);
                    findViewById(R.id.pauseButton).setVisibility(View.VISIBLE);
                }
            }
        });

private void init() {
        mp = new MediaPlayer();
        mp.reset();
        prairText = (TextView) findViewById(R.id.prairText);
        seekBar = (SeekBar) findViewById(R.id.seekBar1);
        try {
            descriptor = getAssets().openFd("mysong.mp3");
            mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
            descriptor.close();
            mp.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int duration = mp.getDuration();
        seekBar.setMax(duration);
    }
}

注意:不要忘记将 mysong.mp3 放入 assets 文件夹