devstory

Le Tutoriel de Android MediaPlayer

  1. MediaPlayer
  2. Exemple avec MediaPlayer
  3. Phụ lục: Thiết kế giao diện

1. MediaPlayer

Android fournit un composant pour jouer de la musique, c'est MediaPlayer, MediaPlayer qui peuvent exécuter des fichiers audio et vidéo, le fichier source se trouve sur votre appareil ou à partir d'une URL.

Comme avec d'autres lecteurs de musique que vous connaissez, MediaPlayer fournit des méthodes pour contrôler la lecture audio/ vidéo, y compris le démarrage, l'arrêt, le rembobinage .....
Vous pouvez également appeler MediaPlayer à partir d'un service.

MediaPlayer est un composant qui n'a pas d'interface, il vous aide à lire un fichier de musique facilement, mais pour lire un fichier vidéo, vous devez le combiner avec SuffaceView pour afficher l'image.

2. Exemple avec MediaPlayer

L'exemple simple suivant utilise MediaPlayer pour lire un fichier musical et certains contrôles de la lecture tels que la lecture, la pause, le rembobinage.
Créez un nouveau projet nommé MediaPlayerTutorial:
  • File > New > New Project > Empty Activity
    • Name: MediaPlayerTutorial
    • Package name: org.o7planning.mediaplayertutorial
    • Language: Java
Créez le dossier raw afin de contenir les fichiers musicals.
Préparez un fichier musical, et le copiez, ensuite le collez dans le dossier raw.
If you want to play a song from a URL you must allow the application to access the network, adding the following XML code to AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET" />
Allows the app to access external storage (Such as SD Card) if you want to play media here.
<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>
La conception de l'interface:
If you are interested in the steps to design the interface of this application, please see the appendix at the end of the article.
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="&lt;&lt;" />

        <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="&gt;&gt;" />

        <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;
    }
}

3. Phụ lục: Thiết kế giao diện

Set ID, Text for components on the interface:

Tutoriels de programmation Android

Show More