媒体播放器中同时播放超过 1 首歌曲 [英] More than 1 song playing in Media Player at same time

查看:23
本文介绍了媒体播放器中同时播放超过 1 首歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to Android programming and I am trying to make a simple Media Player app which extracts songs from the user's SD Card and enables the user to play the songs. The problem I face is that while a song is playing, if the user clicks on some other song, that song also starts playing.

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

I searched for the solution and used release() but the problem persists.

我将不胜感激!

相关代码:

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();
    }
}

注意:播放器类被 MainActivity 调用并接收整数 SongIndex(重新发送 ArrayList 中的歌曲索引)

Note : Player class gets called by the MainActivity and receives the integer songIndex (reresenting the index of song in the ArrayList)

推荐答案

在 playSong() 方法中试试这个

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

这篇关于媒体播放器中同时播放超过 1 首歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆