从 SD 卡搜索 mp3 栏

Seekbar for mp3 from sd card

这段代码是从原始文件夹中读取一个mp3,但我想将我的mp3文件从sd卡中更改为另一个文件

它的新代码,音乐播放但搜索栏不能正常工作,当我触摸它时搜索栏回到第一个位置

public class MainActivity extends Activity implements OnClickListener {

SeekBar seek_bar;
Button play_button, pause_button;
MediaPlayer mediaPlayer;
TextView text_shown;
Handler seekHandler = new Handler();

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

    getInit();
    seekUpdation();
}


public void getInit() {  
    seek_bar = (SeekBar) findViewById(R.id.seek_bar);
    play_button = (Button) findViewById(R.id.play_button);
    pause_button = (Button) findViewById(R.id.pause_button);
    text_shown = (TextView) findViewById(R.id.text_shown);
    play_button.setOnClickListener(this);
    pause_button.setOnClickListener(this);
    String filePath = Environment.getExternalStorageDirectory() + "/Android/music.mp3";
    mediaPlayer = new MediaPlayer();
    try {
        mediaPlayer.setDataSource(filePath);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {

        }
    });
    mediaPlayer.prepareAsync();
    seek_bar.setMax(mediaPlayer.getDuration());
}

Runnable run = new Runnable() {

    @Override
    public void run() {
        seekUpdation();
    }
};

public void seekUpdation() {

    seek_bar.setProgress(mediaPlayer.getCurrentPosition());
    seekHandler.postDelayed(run, 1000);
    seek_bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

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

        }

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

        }

        @Override
        public void onProgressChanged(SeekBar seek_bar, int progress, boolean fromUser) {
            if(fromUser){ 
                mediaPlayer.seekTo(progress); 
                seek_bar.setProgress(progress);
            }

        }
    });
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.play_button:
        text_shown.setText("Playing...");
        mediaPlayer.start();
        break;
    case R.id.pause_button:
        mediaPlayer.pause();
        text_shown.setText("Paused...");
    }
}

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
    if(fromUser){ 
        mediaPlayer.seekTo(progress); 
        seekBar.setProgress(progress);
    }
}


@Override
public void onBackPressed() {
    mediaPlayer.stop();
    finish();
}

}

替换

mediaPlayer = MediaPlayer.create(this, R.raw.my_file);

与:

String filePath = Environment.getExternalStorageDirectory() + "/randomDirectory/yourFile.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mediaPlayer.start();
    }
});
mediaPlayer.prepareAsync();

移动这条线

seek_bar.setMax(mediaPlayer.getDuration());

之后

seekUpdation()

seekHandler.postDelayed(run, 1000);