Android 在媒体播放器中同时播放多首歌曲

Android 在媒体播放器中同时播放多首歌曲,android,audio-player,android-music-player,Android,Audio Player,Android Music Player,我是Android编程新手,我正在尝试制作一个简单的媒体播放器应用程序,从用户的SD卡中提取歌曲,让用户能够播放歌曲。我面临的问题是,当播放歌曲时,如果用户单击其他歌曲,该歌曲也会开始播放 我搜索了解决方案并使用了release(),但问题仍然存在 我将感谢任何帮助 相关代码: public class Player extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, MediaPlayer.OnComple

我是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();
    }
}
公共类播放器扩展AppCompatActivity实现SeekBar.onseekbarchanegListener、MediaPlayer.OnCompletionListener{
通知经理nm;
通知n;
私人按钮播放,向前,向后,下一步,上一步,重复,洗牌;
私有图像按钮播放列表;
私人SeekBar sb;
私有TextView songTitle、currDur、totDur;
私人媒体播放器;
私人音乐经理;
私人公用事业和公用事业;
私有处理程序mHandler=新处理程序();
私有int seekForwardTime=5000;
私有int seekBackwardTime=5000;
私有布尔值isShuffle=false;
私有布尔值isRepeat=false;
private ArrayList songList=new ArrayList();
国际歌曲索引;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
如果(mp!=null)
{
mp.stop();
mp.release();
mp=null;
}
init_player();
songIndex=getIntent().getExtras().getInt(“songIndex”);
nm=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
n=新通知(R.drawable.notif,“警报”,System.currentTimeMillis());
Intent j=新Intent(这个,MainActivity.class);
j、 添加标志(意图、标志、活动、清除、顶部);
PendingEvent pp=PendingEvent.getActivity(Player.this,0,j,PendingEvent.FLAG_UPDATE_CURRENT);
Notification.Builder=new Notification.Builder(此);
builder.setAutoCancel(true);
builder.setContentIntent(pp);
builder.setTicker(songList.get(songinindex.get)(“songtile”);
builder.setContentTitle(“播放”);
setContentText(songList.get(songIndex.get(“songTitle”));
生成器.设置小图标(R.可绘制.不适用);
builder.setcongressing(true);
建造商设置编号(100);
builder.build();
n=builder.getNotification();
nm.notify(0,n);
歌曲索引;
//各种按钮单击侦听器
}
受保护的void init_player(){
play=(按钮)findViewById(R.id.btnPlay);
前进=(按钮)findViewById(R.id.btnForward);
向后=(按钮)findViewById(R.id.btnBackward);
下一步=(按钮)findViewById(R.id.btnNext);
prev=(按钮)findViewById(R.id.btnPrevious);
重复=(按钮)findViewById(R.id.btnRepeat);
shuffle=(按钮)findviewbyd(R.id.btnShuffle);
playlist=(ImageButton)findviewbyd(R.id.btn\u播放列表);
sb=(SeekBar)findviewbyd(R.id.songProgressBar);
songTitle=(TextView)findViewById(R.id.songName);
currDur=(TextView)findViewById(R.id.songCurrentDurationLabel);
totDur=(TextView)findViewById(R.id.songTotalDurationLabel);
mp=新媒体播放器();
mp.setWakeMode(getApplicationContext(),
PowerManager.PARTIAL_WAKE_LOCK);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
sManager=newsongmanager(Player.this);
utils=新的实用程序();
sb.setonseekbarchaneglistener(这个);
mp.setOnCompletionListener(此);
songList=sManager.getPlaylist(0);
}
受保护的void播放歌曲(int-songIndex)
{
试一试{
mp.reset();
setDataSource(songList.get(songinindex.get(“songPath”));
mp.prepare();
mp.start();
songTitle.setText(songList.get(songIndex.get)(“songTitle”);
play.setText(“暂停”);
某人取得进步(0);
sb.setMax(100);
updateProgressBar();
}捕获(IllegalArgumentException e){
e、 printStackTrace();
}捕获(非法状态){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}

注意:MainActivity调用Player类并接收整数songIndex(在ArrayList中重新呈现歌曲索引)

在playSong()方法中尝试此操作

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

您是否同时拥有多个媒体播放器实例?您应该检查一下

您必须在活动的onStop()方法中释放媒体播放器,如下所示:

@Override
protected void onStop() {
super.onStop();  // Always call the superclass method first

mediaPlayer.release();
mediaPlayer = null;
}
从官方文件:

As an example, consider the problems that could happen if you forgot to release the MediaPlayer when your activity is stopped, but create a new one when the activity starts again. 
As you may know, when the user changes the screen orientation (or changes the device configuration in another way), the system handles that by restarting the activity (by default), 
so you might quickly consume all of the system resources as the user rotates the device back and forth between portrait and landscape, 
because at each orientation change, you create a new MediaPlayer that you never release. 
同时有太多的播放器实例,会出现诸如OutOfMemory或太多打开文件之类的错误

顺便说一句,您不应该直接使用构造函数,而应该使用create()方法实例化媒体播放器,如下所述:


当你点击你想播放的歌曲时,你会使用什么代码?这个代码似乎没问题,你一定是在上一个屏幕上犯了这样的错误。你可能正在创建多个即时的播放器活动。并且使用intent
songIndex传递URL
所以每次你点击上一个屏幕上的行时,你的应用程序都会创建一个新的实例of Player activity。在
androidmanifeat.xml
@MAC中尝试这一点,我在清单文件中的Player activity中插入了这一行…它解决了同时播放多首歌曲的问题。但是,现在的问题是,当用户在某首歌曲已经播放时单击某首歌曲,该歌曲将不会开始播放,并且之前的ious one将继续播放。因此,现在用户没有自由了。您能否建议一些建议,以便当用户单击其他歌曲时,该歌曲将开始播放,而前一首歌曲将停止播放?谢谢!@MichaelSpitsin,当用户单击MainActivity(ListView)中的歌曲时,使用意图调用播放器活动,并将songIndex传递给播放器活动。请说明是否使用覆盖onStop(),如以下所示:)我使用onStop()释放MediaPlayer资源并将其设为null。当我单击歌曲播放时,它导致应用程序崩溃。