How to Use the Media Player in Android
Introduction
This introduction is about how to use the Media Player in Android.
When should you apply this? (Optional)
This app is applied when you want to listen to music. There are two modes which are online mode and the offline mode in this app. When there is no Internet connection, user can listen to music locally. If the network connection is available, user can listen to music online on nhaccuatui.com or nhacvui.com website.
Advantages & disadvantages
Advantage:
It’s easy to use.
When users use the online mode, users can listen to the latest songs.
When users use the offline mode, users can listen to their songs from the sdcard.
Disadvantage:
If the path for sdcard is incorrect, the method will not run correctly.
If the sdcard is not push into the emulator, the emulator cannot read the file from the sdcard.
Requirements
The Android emulator needs to add the sdcard first.
Users need to push the music into the sdcard.
If user’s network needs authentication to access the Internet, then user needs to configure the proxy setting.
The minimum requirements for Media Player must have songs, play/pause button, previous button, next button, shuffle song, repeat song button, playlist button and a seek bar.
Steps & Screen shots
First, declare all the buttons and seek bar that mentioned above:
private SeekBar seekBar;
private Button buttonPlayPause, buttonUpdate, buttonNext, buttonPrev;
private CheckBox check_shuffle, check_repeat;
Second, down cast and add listener for each button:
buttonPlayPause = (Button) findViewById(R.id.btn_play_pause);
buttonUpdate = (Button) findViewById(R.id.btn_update); buttonNext = (Button) findViewById(R.id.btn_next);
buttonPrev = (Button) findViewById(R.id.btn_prev);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
check_shuffle = (CheckBox) findViewById(R.id.check_shuffle);
check_repeat = (CheckBox) findViewById(R.id.check_repeat);
Adding listener:
buttonPlayPause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
buttonClick();
updateSongList();
}
});
buttonNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nextSong();
setCount(getCount() + 1);
updateSongList();
}
});
buttonUpdate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// highlightListElement(currentPosition);
updateSongList();
}
});
buttonPrev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
prevSong();
setCount(getCount() + 1);
updateSongList();
}
});
seekBar.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
seekChange(v);
return false;
}
});
To change the seek bar, user needs to add this method:
private void seekChange(View v) {
if (mp.isPlaying()) {
SeekBar sb = (SeekBar) v;
mp.seekTo(sb.getProgress());
}
}
To update the song list, user needs to implement this method:
public void updateSongList() {
File home = new File(MEDIA_PATH);
if (getState() == PLAY_LIST) {
songs.clear();
// check file type
if (home.listFiles(new Mp3Filter()).length > 0) {
for (File file : home.listFiles(new Mp3Filter())) {
songs.add(file.getName());
addSong(file.getName());
}
temp_songs.clear();
for (int i = 0; i < songs.size(); i++) {
temp_songs.add(i);
}
// sort in alphabetical order
Collections.sort(songs, new Comparator() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
songList = new ArrayAdapter(this, R.layout.song_item,
songs);
songList.notifyDataSetChanged();
setListAdapter(songList);
}
} else if (getState() == NOW_PLAYING) {
ArrayAdapter nowPlaying_list = new ArrayAdapter(
this, R.layout.song_item, nowPlaying);
nowPlaying_list.notifyDataSetChanged();
setListAdapter(nowPlaying_list);
} else if (getState() == RECENTLY_ADDED) {
recentlyAdded.clear();
for (File file : home.listFiles(new Mp3Filter())) {
recentlyAdded.add(file.getName());
}
temp_songs.clear();
for (int i = 0; i < songs.size(); i++) {
temp_songs.add(i);
}
ArrayAdapter recentlyAdded_list = new ArrayAdapter(
this, R.layout.song_item, recentlyAdded);
recentlyAdded_list.notifyDataSetChanged();
setListAdapter(recentlyAdded_list);
}
}
Next is the code for the next and previous buttons:
private void nextSong() {
if (check_shuffle.isChecked()) {
if (check_repeat.isChecked()) {
if (temp_songs.size() == swap_temp_songs.size()) {
swap_temp_songs.clear();
int pos = rand.nextInt(temp_songs.size());
if (swap_temp_songs.isEmpty()) {
pos = rand.nextInt(temp_songs.size());
swap_temp_songs.add(pos);
} else {
while (checkExist(swap_temp_songs, pos)) {
pos = rand.nextInt(temp_songs.size());
}
swap_temp_songs.add(pos);
}
setCurrentPosition(pos);
if (getState() == PLAY_LIST || getState() == NOW_PLAYING) {
playSong(MEDIA_PATH + songs.get(pos));
} else if (getState() == RECENTLY_ADDED) {
playSong(MEDIA_PATH + recentlyAdded.get(pos));
}
} else {
int pos = rand.nextInt(temp_songs.size());
if (swap_temp_songs.isEmpty()) {
pos = rand.nextInt(temp_songs.size());
swap_temp_songs.add(pos);
} else {
while (checkExist(swap_temp_songs, pos)) {
pos = rand.nextInt(temp_songs.size());
}
swap_temp_songs.add(pos);
}
setCurrentPosition(pos);
if (getState() == PLAY_LIST || getState() == NOW_PLAYING) {
playSong(MEDIA_PATH + songs.get(pos));
} else if (getState() == RECENTLY_ADDED) {
playSong(MEDIA_PATH + recentlyAdded.get(pos));
}
}
} else {
if (temp_songs.size() == swap_temp_songs.size()) {
Toast.makeText(getBaseContext(), “End of Playlist”,
Toast.LENGTH_LONG).show();
} else {
int pos = rand.nextInt(temp_songs.size());
if (swap_temp_songs.isEmpty()) {
pos = rand.nextInt(temp_songs.size());
swap_temp_songs.add(pos);
} else {
while (checkExist(swap_temp_songs, pos)) {
pos = rand.nextInt(temp_songs.size());
}
swap_temp_songs.add(pos);
}
setCurrentPosition(pos);
if (getState() == PLAY_LIST || getState() == NOW_PLAYING) {
playSong(MEDIA_PATH + songs.get(pos));
} else if (getState() == RECENTLY_ADDED) {
playSong(MEDIA_PATH + recentlyAdded.get(pos));
}
}
}
} else {
if (getCurrentPosition() == (songs.size() – 1)) {
if (check_repeat.isChecked()) {
setCurrentPosition(0);
if (getState() == PLAY_LIST || getState() == NOW_PLAYING) {
playSong(MEDIA_PATH + songs.get(getCurrentPosition()));
} else if (getState() == RECENTLY_ADDED) {
playSong(MEDIA_PATH
+ recentlyAdded.get(getCurrentPosition()));
}
} else {
Toast.makeText(getBaseContext(), “End of Playlist”,
Toast.LENGTH_LONG).show();
}
} else {
setCurrentPosition(getCurrentPosition() + 1);
if (getState() == PLAY_LIST || getState() == NOW_PLAYING) {
playSong(MEDIA_PATH + songs.get(getCurrentPosition()));
} else if (getState() == RECENTLY_ADDED) {
playSong(MEDIA_PATH
+ recentlyAdded.get(getCurrentPosition()));
}
}
}
if (getState() == PLAY_LIST || getState() == NOW_PLAYING) {
nowPlaying.clear();
nowPlaying.add(songs.get(getCurrentPosition()));
} else if (getState() == RECENTLY_ADDED) {
nowPlaying.clear();
nowPlaying.add(recentlyAdded.get(getCurrentPosition()));
}
}
// back to previous song
private void prevSong() {
if (getCurrentPosition() == 0) {
if (check_repeat.isChecked()) {
if (getState() == PLAY_LIST || getState() == NOW_PLAYING) {
setCurrentPosition(songs.size() – 1);
playSong(MEDIA_PATH + songs.get(getCurrentPosition()));
} else if (getState() == RECENTLY_ADDED) {
setCurrentPosition(recentlyAdded.size() – 1);
playSong(MEDIA_PATH
+ recentlyAdded.get(getCurrentPosition()));
}
} else {
Toast.makeText(getBaseContext(), “Top of Playlist”,
Toast.LENGTH_LONG).show();
}
} else {
setCurrentPosition(getCurrentPosition() – 1);
}
if (getState() == PLAY_LIST || getState() == NOW_PLAYING) {
nowPlaying.clear();
nowPlaying.add(songs.get(getCurrentPosition()));
} else if (getState() == RECENTLY_ADDED) {
nowPlaying.clear();
nowPlaying.add(recentlyAdded.get(getCurrentPosition()));
}
}
The play/pause button:
private void buttonClick() {
if (buttonPlayPause.getText() == getString(R.string.play_str)) {
buttonPlayPause.setText(getString(R.string.pause_str));
try {
if (getCount() == 0) {
if (getState() == PLAY_LIST || getState() == NOW_PLAYING) {
nowPlaying.clear();
nowPlaying.add(songs.get(getCurrentPosition()));
} else if (getState() == RECENTLY_ADDED) {
nowPlaying.clear();
nowPlaying.add(recentlyAdded.get(getCurrentPosition()));
}
playSong(MEDIA_PATH + songs.get(getCurrentPosition()));
}
mp.start();
startPlayProgressUpdater();
} catch (IllegalStateException e) {
mp.pause();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
} else {
buttonPlayPause.setText(getString(R.string.play_str));
mp.pause();
}
}
Override the onBackPressed method in order to stop the music when user clicks the back button.
@Override
public void onBackPressed() {
mp.stop();
mp.release();
super.onBackPressed();
}