In a music player, the playback mode is generally automatically recognized after the music is played. The following logic code is provided. You can add the appropriate code as needed.
Public static final int ALL = 0; // ALL Loops
Public static final int SINGLE = 1; // SINGLE Loop
Public static final int RANDOM = 2; // RANDOM
// After playing a song (set the playing mode here)
Public class Completed implements OnCompletionListener
{
@ Override
Public void onCompletion (MediaPlayer mp)
{
If (playMode = ALL) // ALL Loops
{
If (position = listMusic. size ()-1) // default loop playback
{
Position = 0; // The first line
}
Else
{
Position ++;
}
}
Else if (playMode = SINGLE) // SINGLE Loop
{
// Position does not need to be changed
}
Else if (playMode = RANDOM) // RANDOM
{
Position = (int) (Math. random () * listMusic. size (); // random playback
}
ElseSet (); // other playback Processing
}
}
When making a single loop, it is best not to design it like the code above. Because no actual position is given, the program may jump out of the BUG or even crash, the single loop I implemented is to listen to the user's mode selection. After the music is played, restart MediaPlayer.