Hướng dẫn và ví dụ Android MediaPlayer
Xem thêm các chuyên mục:

Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi.


Android cung cấp sẵn một thành phần dùng để chơi nhạc, đó là MediaPlayer. MediaPlayer có thể chạy các file audio và video, với file nguồn nằm trên thiết bị của bạn hoặc từ một đường dẫn URL.
Cũng giống với các phần mềm chơi nhạc khác mà bạn biết, MediaPlayer cung cấp các phương thức để bản kiểm soát phát lại âm thanh (control playback of audio/video) bao gồm chạy, dừng, tua đi, tua lại,..
Cũng giống với các phần mềm chơi nhạc khác mà bạn biết, MediaPlayer cung cấp các phương thức để bản kiểm soát phát lại âm thanh (control playback of audio/video) bao gồm chạy, dừng, tua đi, tua lại,..
Bạn cũng có thể gọi tới MediaPlayer từ một dịch vụ.
MediaPlayer là một thành phần không có giao diện, nó dễ dàng giúp bạn chơi một file nhạc, tuy nhiên để chơi một file video bạn cần phải kết hợp nó với SuffaceView để hiển thị hình ảnh.
MediaPlayer là một thành phần không có giao diện, nó dễ dàng giúp bạn chơi một file nhạc, tuy nhiên để chơi một file video bạn cần phải kết hợp nó với SuffaceView để hiển thị hình ảnh.
Ví dụ đơn giản sau đây, sử dụng MediaPlayer để chơi một file nhạc và một vài nút điều khiển việc chơi nhạc như chạy, tạm dừng, tua đi tua lại.

Tạo mới một project có tên MediaPlayerTutorial:
- File > New > New Project > Empty Activity
- Name: MediaPlayerTutorial
- Package name: org.o7planning.mediaplayertutorial
- Language: Java
Tạo thư mục raw để chứa các file nhạc.


Chuẩn bị một file nhạc, copy và paste vào thư mục raw.


Nếu bạn muốn chơi một bài hát từ một URL bạn phải cho phép ứng dụng truy cập vào mạng, thêm đoạn mã XML sau vào AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET" />
Cho phép ứng dụng truy cập vào bộ nhớ ngoài (external storage) (Chẳng hạn như SD Card) nếu bạn muốn chơi các bài nhạc tại đây.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.o7planning.mediaplayertutorial">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thiết kế giao diện:

Nếu bạn quan tâm đến các bước để thiết kế giao diện của ứng dụng này xin hãy xem phần phụ lục phía cuối bài viết.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:layout_height="37dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView_maxTime"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="39dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:gravity="center"
android:text="Max Time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/seekBar" />
<TextView
android:id="@+id/textView_currentPosition"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:gravity="center"
android:text="Current Possion"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_maxTime" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_currentPosition">
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/button_rewind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="<<" />
<Button
android:id="@+id/button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Start" />
<Button
android:id="@+id/button_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Pause" />
<Button
android:id="@+id/button_fastForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text=">>" />
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<Button
android:id="@+id/button_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="51dp"
android:text="Select a Song"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package org.o7planning.mediaplayertutorial;
import androidx.appcompat.app.AppCompatActivity;
import android.media.AudioManager;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
private TextView textMaxTime;
private TextView textCurrentPosition;
private Button buttonPause;
private Button buttonStart;
private Button buttonRewind;
private Button buttonFastForward;
private Button buttonSelect;
private SeekBar seekBar;
private Handler threadHandler = new Handler();
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.textCurrentPosition = (TextView)this.findViewById(R.id.textView_currentPosition);
this.textMaxTime=(TextView) this.findViewById(R.id.textView_maxTime);
this.buttonSelect = (Button) this.findViewById(R.id.button_select);
this.buttonStart= (Button) this.findViewById(R.id.button_start);
this.buttonPause= (Button) this.findViewById(R.id.button_pause);
this.buttonRewind= (Button) this.findViewById(R.id.button_rewind);
this.buttonFastForward= (Button) this.findViewById(R.id.button_fastForward);
this.buttonStart.setEnabled(false);
this.buttonPause.setEnabled(false);
this.buttonRewind.setEnabled(false);
this.buttonFastForward.setEnabled(false);
this.seekBar= (SeekBar) this.findViewById(R.id.seekBar);
this.seekBar.setClickable(false);
this.buttonSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Select new media source.
selectMediaResource();
}
});
this.buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doStart( );
}
});
this.buttonPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doPause( );
}
});
this.buttonRewind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doRewind( );
}
});
this.buttonFastForward .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doFastForward( );
}
});
// Create MediaPlayer.
this.mediaPlayer= new MediaPlayer();
this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
this.mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
doStop(); // Stop current media.
}
});
this.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
doComplete();
}
});
}
// When user click "Select Media Source" button.
private void selectMediaResource() {
// this.selectRawMediaSource();
// this.selectURLMediaSource();
// this.selectLocalMediaSource();
this.selectRawMediaSource();
}
private void selectRawMediaSource() {
// "mysong.mp3" ==> resName = "mysong".
String resName = MediaPlayerUtils.RAW_MEDIA_SAMPLE;
MediaPlayerUtils.playRawMedia(this, this.mediaPlayer, resName);
}
private void selectURLMediaSource() {
// http://example.coom/mysong.mp3
String mediaURL = MediaPlayerUtils.URL_MEDIA_SAMPLE;
MediaPlayerUtils.playURLMedia(this, this.mediaPlayer, mediaURL);
}
private void selectLocalMediaSource() {
// @localPath = "/storage/emulated/0/DCIM/Music/mysong.mp3"; (For example).
String localPath = MediaPlayerUtils.LOCAL_MEDIA_SAMPLE;
MediaPlayerUtils.playLocalMedia(this, this.mediaPlayer, localPath);
}
// Convert millisecond to string.
private String millisecondsToString(int milliseconds) {
long minutes = TimeUnit.MILLISECONDS.toMinutes((long) milliseconds);
long seconds = TimeUnit.MILLISECONDS.toSeconds((long) milliseconds) ;
return minutes + ":"+ seconds;
}
private void doStart( ) {
if(this.mediaPlayer.isPlaying()) {
return;
}
// The duration in milliseconds
int duration = this.mediaPlayer.getDuration();
int currentPosition = this.mediaPlayer.getCurrentPosition();
if(currentPosition== 0) {
this.seekBar.setMax(duration);
String maxTimeString = this.millisecondsToString(duration);
this.textMaxTime.setText(maxTimeString);
} else if(currentPosition== duration) {
// Resets the MediaPlayer to its uninitialized state.
this.mediaPlayer.reset();
}
this.mediaPlayer.start();
// Create a thread to update position of SeekBar.
UpdateSeekBarThread updateSeekBarThread= new UpdateSeekBarThread();
threadHandler.postDelayed(updateSeekBarThread,50);
this.buttonPause.setEnabled(true);
this.buttonStart.setEnabled(false);
this.buttonRewind.setEnabled(true);
this.buttonFastForward.setEnabled(true);
}
// Called by MediaPlayer.OnCompletionListener
// When Player cocmplete
private void doComplete() {
buttonStart.setEnabled(true);
buttonPause.setEnabled(false);
buttonRewind.setEnabled(true);
buttonFastForward.setEnabled(false);
}
// Called by MediaPlayer.OnPreparedListener.
// When user select a new media source, then stop current.
private void doStop() {
if(this.mediaPlayer.isPlaying()) {
this.mediaPlayer.stop();
}
buttonStart.setEnabled(true);
buttonPause.setEnabled(false);
buttonRewind.setEnabled(false);
buttonFastForward.setEnabled(false);
}
// When user click to "Pause".
private void doPause( ) {
this.mediaPlayer.pause();
this.buttonPause.setEnabled(false);
this.buttonStart.setEnabled(true);
}
// When user click to "Rewind".
private void doRewind( ) {
int currentPosition = this.mediaPlayer.getCurrentPosition();
int duration = this.mediaPlayer.getDuration();
// 5 seconds.
int SUBTRACT_TIME = 5000;
if(currentPosition - SUBTRACT_TIME > 0 ) {
this.mediaPlayer.seekTo(currentPosition - SUBTRACT_TIME);
}
this.buttonFastForward.setEnabled(true);
}
// When user click to "Fast-Forward".
private void doFastForward( ) {
int currentPosition = this.mediaPlayer.getCurrentPosition();
int duration = this.mediaPlayer.getDuration();
// 5 seconds.
int ADD_TIME = 5000;
if(currentPosition + ADD_TIME < duration) {
this.mediaPlayer.seekTo(currentPosition + ADD_TIME);
}
}
// Thread to Update position for SeekBar.
class UpdateSeekBarThread implements Runnable {
public void run() {
int currentPosition = mediaPlayer.getCurrentPosition();
String currentPositionStr = millisecondsToString(currentPosition);
textCurrentPosition.setText(currentPositionStr);
seekBar.setProgress(currentPosition);
// Delay thread 50 milisecond.
threadHandler.postDelayed(this, 50);
}
}
}
MediaPlayerUtils.java
package org.o7planning.mediaplayertutorial;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;
public class MediaPlayerUtils {
// "mysong.mp3" in directory "raw".
public static final String RAW_MEDIA_SAMPLE = "mysong";
// Example Path: /sdcard/Music/mysong.mp3
// Example Path: /storage/emulated/0/DCIM/Music/mysong.mp3
public static final String LOCAL_MEDIA_SAMPLE ="/sdcard/Music/mysong.mp3";
public static final String URL_MEDIA_SAMPLE = "https://ex1.o7planning.com/_testdatas_/yodel.mp3";
public static final String LOG_TAG= "MediaPlayerTutorial";
// Play a media in directory RAW.
// Media name = "mysong.mp3" ==> resName = "mysong".
public static void playRawMedia(Context context, MediaPlayer mediaPlayer, String resName) {
try {
// ID of video file.
int id = MediaPlayerUtils.getRawResIdByName( context,resName);
Uri uri = Uri.parse("android.resource://" + context.getPackageName() + "/" + id);
Log.i(LOG_TAG, "Media URI: "+ uri);
Toast.makeText(context,"Select source: "+ uri,Toast.LENGTH_SHORT).show();
mediaPlayer.setDataSource(context, uri);
mediaPlayer.prepareAsync();
} catch (Exception e) {
Log.e(LOG_TAG, "Error Play Raw Media: "+e.getMessage());
Toast.makeText(context,"Error Play Raw Media: "+ e.getMessage(),Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
// Example Path: /sdcard/Music/mysong.mp3
// Example Path: /storage/emulated/0/DCIM/Music/mysong.mp3
public static void playLocalMedia(Context context, MediaPlayer mediaPlayer, String localPath) {
try {
Uri uri = Uri.parse("android.resource://" + localPath);
Log.i(LOG_TAG, "Media URI: "+ uri);
Toast.makeText(context,"Select source: "+ uri,Toast.LENGTH_SHORT).show();
mediaPlayer.setDataSource(context, uri);
mediaPlayer.prepareAsync();
} catch(Exception e) {
Log.e(LOG_TAG, "Error Play Local Media: "+ e.getMessage());
Toast.makeText(context,"Error Play Local Media: "+ e.getMessage(),Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
// String videoURL = "https://ex1.o7planning.com/_testdatas_/yodel.mp3";
public static void playURLMedia(Context context, MediaPlayer mediaPlayer, String videoURL) {
try {
Log.i(LOG_TAG, "Media URL: "+ videoURL);
Uri uri= Uri.parse( videoURL );
Toast.makeText(context,"Select source: "+ uri,Toast.LENGTH_SHORT).show();
mediaPlayer.setDataSource(context, uri);
mediaPlayer.prepareAsync();
} catch(Exception e) {
Log.e(LOG_TAG, "Error Play URL Media: "+ e.getMessage());
Toast.makeText(context,"Error Play URL Media: "+ e.getMessage(),Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
// Find ID corresponding to the name of the resource (in the directory RAW).
public static int getRawResIdByName(Context context, String resName) {
String pkgName = context.getPackageName();
// Return 0 if not found.
int resID = context.getResources().getIdentifier(resName, "raw", pkgName);
Log.i(LOG_TAG, "Res Name: " + resName + "==> Res ID = " + resID);
return resID;
}
}


Sét đặt ID, Text cho các thành phần trên giao diện:
