媒体播放器问题:所有歌曲同时播放

huangapple go评论72阅读模式
英文:

MediaPlayer problem: all songs playing at the same time

问题

所以我正在尝试播放用户选择的某些歌曲混合。我正在创建一个字符串数组,其中包含应该在MediaPlayer中播放的文件名。问题是所有的歌曲都同时播放。我已经尝试了各种方法,但仍然不知道如何确保播放器在开始另一首歌之前等待一首歌结束。

我尝试过以下方法:

public void play(View v){

        if(player==null)
        {
            while(true) {
                if(i==brojac) break; //检查数组列表是否已结束
                if(player.isPlaying()==false) {
                    i++;
                    String s = lista.get(i).toString();
                    player = MediaPlayer.create(this, (this.getResources().getIdentifier(s, "raw", this.getPackageName())));
                    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mediaPlayer) {
                            stopPlayer();
                        }
                    });
                    player.start();
                }
            }
        }

    }
private void stopPlayer()
    {
        if(player!=null)
        {
            player.release();
            player=null;
        }
    }

编辑

根据下面的答案,一切正常,但是播放器有明显的间隔。我使用了setNextPlayer()函数解决了这个问题,但由于在播放时重新创建播放器,应用程序崩溃了。

void playSong(int currentSong) {
        player = MediaPlayer.create(this, currentSong);
        player1 = MediaPlayer.create(this,this.getResources().getIdentifier(lista.get(i+1).toString(),"raw",getPackageName()));
        player.setNextMediaPlayer(player1);
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                player.release();
                i++;
                if(i==brojac) return;
                player1.start();
                playSong(MainActivity.this.getResources().getIdentifier(lista.get(i).toString(),"raw",getPackageName()));
            }
        });
        if(i==0) {
            player.start();
        }
    }
英文:

So I'm trying to play a certain mix of songs that user chooses. I'm making an array of strings that are filenames that should be played in MediaPlayer. The problem is that all of the songs play at the same time. I've tried everything but still don't know how to make sure player waits for a song to end before it starts another one.

Here's what I've tried:

public void play(View v){

        if(player==null)
        {
            while(true) {
                if(i==brojac) break; //checking if the arraylist has ended
                if(player.isPlaying()==false) {
                    i++;
                    String s = lista.get(i).toString();
                    player = MediaPlayer.create(this, (this.getResources().getIdentifier(s, "raw", this.getPackageName())));
                    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mediaPlayer) {
                            stopPlayer();
                        }
                    });
                    player.start();
                }
            }
        }

    }
private void stopPlayer()
    {
        if(player!=null)
        {
            player.release();
            player=null;
        }
    }

EDIT

With the answer below everything works,but the player is having noticable gap. I've solved it with setNextPlayer() function, but the app crashes due to recreating the player while it is playing.

void playSong(int currentSong) {
        player = MediaPlayer.create(this, currentSong);
        player1 = MediaPlayer.create(this,this.getResources().getIdentifier(lista.get(i+1).toString(),"raw",getPackageName()));
        player.setNextMediaPlayer(player1);
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                player.release();
                i++;
                if(i==brojac) return;
                player1.start();
                playSong(MainActivity.this.getResources().getIdentifier(lista.get(i).toString(),"raw",getPackageName()));
            }
        });
        if(i==0) {
            player.start();
        }
    }

答案1

得分: 0

我相信您想要这个:

ArrayList<Integer> songs = new ArrayList<>();
private MediaPlayer mediaPlayer;
private int currentIndex = 0;

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

    Field[] fields = R.raw.class.getFields();
    for (Field field : fields)
        songs.add(getResources().getIdentifier(field.getName(), "raw", getPackageName()));
    playSong(songs.get(currentIndex));
}

void playSong(int currentSong) {
    mediaPlayer = MediaPlayer.create(this, currentSong);
    mediaPlayer.setOnCompletionListener(completeListener);
    mediaPlayer.start();
}

MediaPlayer.OnCompletionListener completeListener = mp -> {
    mp.release();
    currentIndex++;
    playSong(songs.get(currentIndex));
};

解释:

以下代码将从“raw”文件夹获取所有音频文件,并将它们存储在名为“songs”的ArrayList中:

Field[] fields = R.raw.class.getFields();
for (Field field : fields)
    songs.add(getResources().getIdentifier(field.getName(), "raw", getPackageName()));

要播放歌曲:

void playSong(int currentSong) {
    mediaPlayer = MediaPlayer.create(this, currentSong);
    mediaPlayer.setOnCompletionListener(completeListener);
    mediaPlayer.start();
}

当歌曲播放完毕时,OnCompletionListener侦听器会释放当前的MediaPlayer,然后将currentIndex递增,以便songs.get(currentIndex)将提供下一首歌曲:

MediaPlayer.OnCompletionListener completeListener = mp -> {
    mp.release();
    currentIndex++;
    playSong(songs.get(currentIndex));
};
英文:

I believe you want this :

ArrayList&lt;Integer&gt; songs = new ArrayList&lt;&gt;();
    private MediaPlayer mediaPlayer;
    private int currentIndex = 0;

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

        Field[] fields = R.raw.class.getFields();
        for (Field field : fields)
            songs.add(getResources().getIdentifier(field.getName(), &quot;raw&quot;, getPackageName()));
        playSong(songs.get(currentIndex));

    }


    void playSong(int currentSong) {
        mediaPlayer = MediaPlayer.create(this, currentSong);
        mediaPlayer.setOnCompletionListener(completeListener);
        mediaPlayer.start();
    }

    MediaPlayer.OnCompletionListener completeListener = mp -&gt; {
        mp.release();
        currentIndex++;
        playSong(songs.get(currentIndex));
    };

Explication :

The below code, will get all the audio files from raw folder and store them in the ArrayList called songs :

 Field[] fields = R.raw.class.getFields();
 for (Field field : fields)
            songs.add(getResources().getIdentifier(field.getName(), &quot;raw&quot;, getPackageName()));

To play song :

void playSong(int currentSong) {
        mediaPlayer = MediaPlayer.create(this, currentSong);
        mediaPlayer.setOnCompletionListener(completeListener);
        mediaPlayer.start();
    }

the OnCompletionListener listener, when the song ends, you need to increment the currentIndex so the songs.get(currentIndex) will give you the next song

MediaPlayer.OnCompletionListener completeListener = mp -&gt; {
        mp.release();
        currentIndex++;
        playSong(songs.get(currentIndex));
    };

huangapple
  • 本文由 发表于 2020年9月30日 06:35:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64128581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定