在 Android 中封装优雅的 MediaPlayer 音频播放器,可以遵循以下几个原则: 采用单例模式,确保整个应用中只有一个播放器实例,避免资源浪费和冲突。封装音频播放器的基本
在 Android 中封装优雅的 MediaPlayer 音频播放器,可以遵循以下几个原则:
下面是一个简单的示例代码:
public class SimpleAudioPlayer {
private static SimpleAudioPlayer sInstance;
private MediaPlayer mMediaPlayer;
private Context mContext;
private boolean mIsPrepared = false;
private int mDuration; // 音频总时长
private int mCurPosition; // 当前播放位置
private OnStateChangeListener mOnStateChangeListener;
private OnErrorListener mOnErrorListener;
// 单例模式
public static SimpleAudioPlayer getInstance(Context context) {
if (sInstance == null) {
synchronized (SimpleAudioPlayer.class) {
if (sInstance == null) {
sInstance = new SimpleAudioPlayer(context.getApplicationContext());
}
}
}
return sInstance;
}
private SimpleAudioPlayer(Context context) {
mContext = context;
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mIsPrepared = true;
mDuration = mp.getDuration();
if (mOnStateChangeListener != null) {
mOnStateChangeListener.onPrepared();
}
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if (mOnStateChangeListener != null) {
mOnStateChangeListener.onComplete();
}
}
});
mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
mIsPrepared = false;
if (mOnErrorListener != null) {
mOnErrorListener.onError(what, extra);
}
return false;
}
});
mMediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
@Override
public void onSeekComplete(MediaPlayer mp) {
if (mOnStateChangeListener != null) {
mOnStateChangeListener.onSeekComplete();
}
}
});
}
public void setOnStateChangeListener(OnStateChangeListener listener) {
mOnStateChangeListener = listener;
}
public void setOnErrorListener(OnErrorListener listener) {
mOnErrorListener = listener;
}
public void play(String url) {
if (TextUtils.isEmpty(url)) {
return;
}
if (!NetworkUtil.isConnected(mContext)) { // 检查网络状态
if (mOnErrorListener != null) {
mOnErrorListener.onError(ERROR_NETWORK_DISCONNECTED, "Network disconnected");
}
return;
}
try {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(url);
mMediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
mIsPrepared = false;
}
}
public void pause() {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
if (mOnStateChangeListener != null) {
mOnStateChangeListener.onPause();
}
}
}
public void resume() {
if (mIsPrepared && !mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
if (mOnStateChangeListener != null) {
mOnStateChangeListener.onResume();
}
}
}
public void stop() {
if (mMediaPlayer.isPlaying() || mIsPrepared) {
mMediaPlayer.stop();
mIsPrepared = false;
if (mOnStateChangeListener != null) {
mOnStateChangeListener.onStop();
}
}
}
使用封装好的 SimpleAudioPlayer,可以按照以下步骤:
获取 SimpleAudioPlayer 实例
SimpleAudioPlayer player = SimpleAudioPlayer.getInstance(context);
设置监听器(可选)
可以通过 setOnStateChangeListener
方法和 setOnErrorListener
方法设置状态变化监听器和错误监听器。
到此这篇关于Android MediaPlayer音频播放器封装示例浅析的文章就介绍到这了,更多相关Android MediaPlayer内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: AndroidMediaPlayer音频播放器封装示例浅析
本文链接: https://www.lsjlt.com/news/208394.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0