gpt4 book ai didi

java - Android Media Player 同时播放多首歌曲

转载 作者:行者123 更新时间:2023-11-30 02:53:18 29 4
gpt4 key购买 nike

我正在编写一个 Church 应用程序,我在其中提供了 Music Player 功能,几乎完成了我的要求,但遇到了一些小问题。

我是如何实现我的音乐播放器的:

  1. 列出 SD 卡中可用的音频歌曲(路径如:Environment.getExternalStorageDirectory() + "/Church Application/Prayers")
  2. 一旦用户点击任何音频,然后启动媒体播放器
  3. 在媒体播放器屏幕中,我为用户提供导航以返回到音频列表
  4. 用户回来了,现在选择了其他歌曲来收听(这里我遇到了问题,用户将能够播放新选择的歌曲,但旧歌曲也在播放,而一次只能播放最后选择的歌曲,但是在我的例子中,两首歌都在播放)

PlayListActivity.java:-

    // selecting single ListView item
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listitem index
int songIndex = position;

// Starting new intent
Intent in = new Intent(getApplicationContext(),
MediaPlayerActivity.class);
// Sending songIndex to PlayerActivity
in.putExtra("songIndex", songIndex);
setResult(100, in);
startActivity(in);
// Closing PlayListView
finish();
}
});

MediaPlayerActivity.java:-

    int value ;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);


mp = new MediaPlayer();
songManager = new SongsManager();
utils = new Utilities();

songProgressBar.setOnSeekBarChangeListener(this); // Important
mp.setOnCompletionListener(this); // Important

songsList = songManager.getPlayList();

Bundle extras = getIntent().getExtras();
if (extras != null) {
value = extras.getInt("songIndex");
}

playSong(value);


btnPlay.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// check for already playing
if (mp.isPlaying()) {
if (mp != null) {
mp.pause();
// Changing button image to play button
btnPlay.setImageResource(R.drawable.btn_play);
}
} else {
// Resume song
if (mp != null) {
mp.start();
// Changing button image to pause button
btnPlay.setImageResource(R.drawable.btn_pause);
}
}

}
});

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
currentSongIndex = data.getExtras().getInt("songIndex");
// play selected song
playSong(currentSongIndex);
}

}

public void playSong(int songIndex) {
// Play song
try {

mp.reset();
mp.setDataSource(songsList.get(songIndex).get("songPath"));
mp.prepare();
mp.start();
// Displaying Song title
String songTitle = songsList.get(songIndex).get("songTitle");
songTitleLabel.setText(songTitle);

// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);

// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);

// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}

private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();

// Displaying time completed playing
songCurrentDurationLabel.setText(""
+ utils.milliSecondsToTimer(currentDuration));

// Updating progress bar
int progress = (int) (utils.getProgressPercentage(currentDuration,
totalDuration));
// Log.d("Progress", ""+progress);
songProgressBar.setProgress(progress);

// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromTouch) {

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// remove message Handler from updating progress bar
mHandler.removeCallbacks(mUpdateTimeTask);
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mHandler.removeCallbacks(mUpdateTimeTask);
int totalDuration = mp.getDuration();
int currentPosition = utils.progressToTimer(seekBar.getProgress(),
totalDuration);

// forward or backward to certain seconds
mp.seekTo(currentPosition);

// update timer progress again
updateProgressBar();
}

@Override
public void onCompletion(MediaPlayer arg0) {

// check for repeat is ON or OFF
if (isRepeat) {
// repeat is on play same song again
playSong(currentSongIndex);
} else if (isShuffle) {
// shuffle is on - play a random song
Random rand = new Random();
currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
playSong(currentSongIndex);
} else {
// no repeat or shuffle ON - play next song
if (currentSongIndex < (songsList.size() - 1)) {
playSong(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
} else {
// play first song
playSong(0);
currentSongIndex = 0;
}
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// if one of the volume keys were pressed
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
}
// propagate the key event
return super.onKeyDown(keyCode, event);
}

@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}

}

最佳答案

        songManager = new SongsManager();
utils = new Utilities();

songProgressBar.setOnSeekBarChangeListener(this);


songsList = songManager.getPlayList();

Bundle extras = getIntent().getExtras();
if (extras != null) {
value = extras.getInt("songIndex");
}

//playSong(value);

if (mp != null){
try {
mp.relase();
mp = null;
} catch (Exception e) {

}

mp = new MediaPlayer();
playSong(value);
}else{
mp = new MediaPlayer();
playSong(value);
}

mp.setOnCompletionListener(this); // Important

关于java - Android Media Player 同时播放多首歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23752450/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com