gpt4 book ai didi

android - 媒体播放器同时播放超过 1 首歌曲

转载 作者:行者123 更新时间:2023-11-29 19:42:19 26 4
gpt4 key购买 nike

我是 Android 编程的新手,我正在尝试制作一个简单的媒体播放器应用程序,该应用程序可以从用户的 SD 卡中提取歌曲并使用户能够播放歌曲。我面临的问题是,当一首歌正在播放时,如果用户点击其他歌曲,那首歌也会开始播放。

我搜索了解决方案并使用了 release(),但问题仍然存在。

如有任何帮助,我将不胜感激!

相关代码:

public class Player extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, MediaPlayer.OnCompletionListener{

NotificationManager nm;
Notification n;

private Button play, forward, backward, next, prev, repeat, shuffle;
private ImageButton playlist;
private SeekBar sb;
private TextView songTitle, currDur, totDur;

private MediaPlayer mp;
private SongManager sManager;
private Utilities utils;
private Handler mHandler = new Handler();
private int seekForwardTime = 5000;
private int seekBackwardTime = 5000;
private boolean isShuffle = false;
private boolean isRepeat = false;
private ArrayList<HashMap<String,String>> songList = new ArrayList<HashMap<String, String>>();

int songIndex;

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

if(mp!=null)
{
mp.stop();
mp.release();
mp=null;
}
init_player();
songIndex = getIntent().getExtras().getInt("songIndex");

nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
n = new Notification(R.drawable.notif,"Alert",System.currentTimeMillis());
Intent j = new Intent(this, MainActivity.class);
j.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pp= PendingIntent.getActivity(Player.this, 0, j, PendingIntent.FLAG_UPDATE_CURRENT);

Notification.Builder builder = new Notification.Builder(this);
builder.setAutoCancel(true);
builder.setContentIntent(pp);
builder.setTicker(songList.get(songIndex).get("songTitle"));
builder.setContentTitle("Playing");
builder.setContentText(songList.get(songIndex).get("songTitle"));
builder.setSmallIcon(R.drawable.notif);
builder.setOngoing(true);
builder.setNumber(100);
builder.build();

n = builder.getNotification();
nm.notify(0, n);

playSong(songIndex);
//Various Button Click Listeners
}
protected void init_player() {

play = (Button)findViewById(R.id.btnPlay);
forward = (Button) findViewById(R.id.btnForward);
backward = (Button) findViewById(R.id.btnBackward);
next = (Button) findViewById(R.id.btnNext);
prev = (Button) findViewById(R.id.btnPrevious);
repeat = (Button) findViewById(R.id.btnRepeat);
shuffle = (Button) findViewById(R.id.btnShuffle);
playlist = (ImageButton) findViewById(R.id.btn_playlist);

sb = (SeekBar) findViewById(R.id.songProgressBar);
songTitle = (TextView) findViewById(R.id.songName);
currDur = (TextView) findViewById(R.id.songCurrentDurationLabel);
totDur = (TextView) findViewById(R.id.songTotalDurationLabel);

mp = new MediaPlayer();
mp.setWakeMode(getApplicationContext(),
PowerManager.PARTIAL_WAKE_LOCK);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
sManager = new SongManager(Player.this);
utils = new Utilities();

sb.setOnSeekBarChangeListener(this);
mp.setOnCompletionListener(this);

songList = sManager.getPlaylist(0);
}
protected void playSong(int songIndex)
{
try {

mp.reset();
mp.setDataSource(songList.get(songIndex).get("songPath"));
mp.prepare();
mp.start();

songTitle.setText(songList.get(songIndex).get("songTitle"));
play.setText("PAUSE");
sb.setProgress(0);
sb.setMax(100);
updateProgressBar();
} catch (IllegalArgumentException e){
e.printStackTrace();
} catch(IllegalStateException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}

注意:Player 类被 MainActivity 调用并接收整数 songIndex(重新表示 ArrayList 中歌曲的索引)

最佳答案

在 playSong() 方法中试试这个

if (mp.isPlaying()) {
mp.stop();
mp.relase();
mp.prepare();
mp.start();
}

关于android - 媒体播放器同时播放超过 1 首歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38460317/

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