如何在Android(MediaPlayer)中播放下一首歌?

时间:2016-08-09 10:55:05

标签: java android android-mediaplayer goto

首先,这是我的代码。我在下面解释了我的问题和想法:

 listView.setOnItemClickListener
            (new AdapterView.OnItemClickListener() {
                 @Override
                 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {



                         int Current_Song;
                         Songs song = Song.get(i);

                         //If mediaPlayer is not used before, this will make oldsong as present song.
                         if (Old_Song == -326523) {
                             Old_Song = song.getSong();
                         }

                         Current_Song = song.getSong();

                         ImageView IVP_P = (ImageView) findViewById(R.id.P_PImage);


                         //If mediaPlayer is paused.
                         if (IsPaused) {
                             P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);
                             //If the song paused is same as the new song.
                             if (Current_Song == Old_Song) {

                                 mediaplayer.start();
                             }

                             //If the song Paused is not the new song.
                             else {
                                 if (mediaplayer != null) {
                                     mediaplayer.release();
                                     mediaplayer = null;
                                 }

                                 int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

                                 if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                                     mediaplayer = mediaplayer.create(SongsListActivity.this, song.getSong());
                                     Old_Song = song.getSong();
                                     NameD.setText(song.getNameOfSong());
                                     RateD.setText(song.getDeveloperRate());
                                     ImageD.setImageResource(song.getImage());
                                     mediaplayer.start();
                                     mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                         @Override
                                         public void onCompletion(MediaPlayer mediaPlayer) {
                                             P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
                                             IsPaused = true;
                                         }
                                     });
                                 }
                             }
                             IsPaused = false;

                         } else if (mediaplayer != null) {
                             //If mediaPlayer is already Playing a song.
                             if (mediaplayer.isPlaying()) {
                                 P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
                                 mediaplayer.pause();
                                 IVP_P.setImageResource(R.drawable.ic_play_arrow_black_24dp);
                                 IsPaused = true;
                             }
                         }

                         //If mediaPlayer is used for first time and if mediaPlayer is neither paused
                         else {

                             if (mediaplayer != null) {
                                 mediaplayer.release();
                                 mediaplayer = null;
                             }

                             int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

                             if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                                 mediaplayer = mediaplayer.create(SongsListActivity.this, song.getSong());
                                 Old_Song = song.getSong();
                                 NameD.setText(song.getNameOfSong());
                                 RateD.setText(song.getDeveloperRate());
                                 ImageD.setImageResource(song.getImage());
                                 mediaplayer.start();
                                 P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);

                                 mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                     @Override
                                     public void onCompletion(MediaPlayer mediaPlayer) {
                                         P_and_P.setImageResource(R.drawable.ic_play_arrow_white_48dp);
                                         IsPaused = true;
                                         P_and_P.setImageResource(R.drawable.ic_pause_white_48dp);

                                     }
                                 });
                             }
                         }

                     }

             }
            );

这里我的应用程序使用ListView和Adapter显示歌曲列表。歌曲的细节存储在一个arrayList中(如你所能看到的)。当一个项目被点击时,它获取该项目的位置,然后引用arrayList的相应元素,获取相应歌曲的位置然后播放那首歌。我想要的是当一首歌结束,并调用onCompletion()方法时,我想增加i的值(在我的代码的第4行看一下),然后开始 相应的歌曲。

我想使用goto和label来做这件事,但它不起作用。(我把标签放在onItemClick的最开始,到最后并转到onCompletion。)

如何使用goto和label?

或者还有其他有效的方法来执行我的任务吗?

请帮助我!!!!!!!!!!!!!!!!!!!!!!!!

1 个答案:

答案 0 :(得分:0)

因此,您必须为播放列表中的所有歌曲创建索引,然后选中

if(currentSongIndex < (songsList.size() - 1)){
                playSong(currentSongIndex + 1);
                currentSongIndex = currentSongIndex + 1;
            }else{
                // play first song
                playSong(0);
                currentSongIndex = 0;
            }

有关详情,请查看此链接http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/