SoFunction
Updated on 2025-03-04

A brief analysis of Android MediaPlayer audio player packaging example

To encapsulate an elegant MediaPlayer audio player in Android, you can follow the following principles:

  • Adopt singleton mode to ensure that there is only one player instance in the entire application, avoiding resource waste and conflict.
  • The basic operations of encapsulating the audio player include play, pause, stop, fast forward, fast reversal, jump to a specified time, etc.
  • Provides a callback interface to monitor player status changes events, such as playback start, playback pause, playback completion, etc.
  • Provides an error callback interface, listens for player error events, and provides corresponding error codes and error information.
  • If you need to support multiple player instances, you can use a message processing method similar to Messenger. Each instance corresponds to a Handler object, and the corresponding operations are processed by receiving messages.
  • Check the network status before playing, if the network is not available, playback fails and an error message is returned.

Here is a simple example code:

public class SimpleAudioPlayer {
    private static SimpleAudioPlayer sInstance;
    private MediaPlayer mMediaPlayer;
    private Context mContext;
    private boolean mIsPrepared = false;
    private int mDuration; // Total audio duration    private int mCurPosition; // Current playback location    private OnStateChangeListener mOnStateChangeListener;
    private OnErrorListener mOnErrorListener;
    // Singleton mode    public static SimpleAudioPlayer getInstance(Context context) {
        if (sInstance == null) {
            synchronized () {
                if (sInstance == null) {
                    sInstance = new SimpleAudioPlayer(());
                }
            }
        }
        return sInstance;
    }
    private SimpleAudioPlayer(Context context) {
        mContext = context;
        mMediaPlayer = new MediaPlayer();
        (new () {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mIsPrepared = true;
                mDuration = ();
                if (mOnStateChangeListener != null) {
                    ();
                }
            }
        });
        (new () {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (mOnStateChangeListener != null) {
                    ();
                }
            }
        });
        (new () {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mIsPrepared = false;
                if (mOnErrorListener != null) {
                    (what, extra);
                }
                return false;
            }
        });
        (new () {
            @Override
            public void onSeekComplete(MediaPlayer mp) {
                if (mOnStateChangeListener != null) {
                    ();
                }
            }
        });
    }
    public void setOnStateChangeListener(OnStateChangeListener listener) {
        mOnStateChangeListener = listener;
    }
    public void setOnErrorListener(OnErrorListener listener) {
        mOnErrorListener = listener;
    }
    public void play(String url) {
        if ((url)) {
            return;
        }
        if (!(mContext)) { // Check network status            if (mOnErrorListener != null) {
                (ERROR_NETWORK_DISCONNECTED, "Network disconnected");
            }
            return;
        }
        try {
            ();
            (url);
            ();
        } catch (IOException e) {
            ();
            mIsPrepared = false;
        }
    }
    public void pause() {
        if (()) {
            ();
            if (mOnStateChangeListener != null) {
                ();
            }
        }
    }
    public void resume() {
        if (mIsPrepared && !()) {
            ();
            if (mOnStateChangeListener != null) {
                ();
            }
        }
    }
    public void stop() {
        if (() || mIsPrepared) {
            ();
            mIsPrepared = false;
            if (mOnStateChangeListener != null) {
                ();
            }
        }
    }

Using the encapsulated SimpleAudioPlayer, you can follow these steps:

Get SimpleAudioPlayer instance

SimpleAudioPlayer player = (context);

Set up the listener (optional)

Can be passedsetOnStateChangeListenerMethods andsetOnErrorListenerMethods set state change listeners and error listeners.

This is the end of this article about the packaging example of Android MediaPlayer audio player. For more related Android MediaPlayer content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!