Media Player in android Using MediaStore

跟風遠走 提交于 2020-01-06 18:09:27

问题


public class PlayerScreen extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

private MediaPlayer mp;
private Button buttonplaypause;
private int count;
private SeekBar seekbar;
private int curpos, prevpos;
private Bundle b;
private String title;
private final Handler handler = new Handler();
private Runnable updatePositionRunnable = new Runnable() {
    public void run() {

        seekbar.setProgress(mp.getCurrentPosition());
        handler.postDelayed(this, 6000);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player_screen);
    title = b.getString("title");
    mp = new MediaPlayer(); 
    try {
        Log.d("message", "message");
        mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Music/"+title);
        mp.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //mp = MediaPlayer.create(this, R.raw.audio);

    buttonplaypause = (Button) findViewById(R.id.buttonPlayPause);
    seekbar = (SeekBar) findViewById(R.id.seekBar);
    seekbar.setMax(mp.getDuration());
    buttonplaypause.setBackgroundResource(R.drawable.play);

    seekbar.setOnSeekBarChangeListener(this);
    handler.postDelayed(updatePositionRunnable, 6000);
}

public void onProgressChanged(SeekBar mySeekBar, int progress, boolean fromUser) {
    if (mp.isPlaying()) {
        mp.seekTo(progress);
    }
}

public void onStartTrackingTouch(SeekBar mySeekBar) {
}

public void onStopTrackingTouch(SeekBar mySeekBar) {
}

public void toPlayPause(View v) {
    if (count % 2 == 0) {
        buttonplaypause.setBackgroundResource(R.drawable.pause);
        curpos = mp.getCurrentPosition();
        prevpos = curpos;
        mp.seekTo(curpos);
        mp.start();
        count++;
        buttonplaypause.setBackgroundResource(R.drawable.play);
        mp.pause();
        count++;
    }
}
}

What i'm trying to do is build a music player which displays the list of song in one activity in a List View and when the user taps on an item that particular song should be played in another intent (which is provided by play and pause button). The code provided above is the code for the second activity (the first part i.e. listing the songs is done). The first Activity passes the song title in a bundle which is received by this activity. I am new to android and don't know a lot about it. I don't know how to pass the song from one intent to another so that it is played. The problem I think is in the line setDataSource().

When the second Intent (i.e the code provided above) is opened on tapping item in the List View, the seek bar reaches it's end and no song is played

If there is any other, better way to pass the song from one intent to another feel free to describe it in detail.

来源:https://stackoverflow.com/questions/38880528/media-player-in-android-using-mediastore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!