Android媒体播放工具类 Android开发之媒体播放工具类完整示例
沉水之木 人气:0想了解Android开发之媒体播放工具类完整示例的相关内容吗,沉水之木在本文为您仔细讲解Android媒体播放工具类的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,媒体播放,工具类,下面大家一起来学习吧。
本文实例讲述了Android开发之媒体播放工具类。分享给大家供大家参考,具体如下:
package com.maobang.imsdk.util; import android.media.MediaPlayer; import android.net.Uri; import android.util.Log; import java.io.FileInputStream; import java.io.IOException; import com.maobang.imsdk.app.IMApplication; /** * 媒体播放工具 */ public class MediaUtil { private static final String TAG = "MediaUtil"; private MediaPlayer player; private EventListener eventListener; private MediaUtil(){ player = new MediaPlayer(); } private static MediaUtil instance = new MediaUtil(); public static MediaUtil getInstance(){ return instance; } public MediaPlayer getPlayer() { return player; } public void setEventListener(final EventListener eventListener) { if (player != null){ player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { eventListener.onStop(); } }); } this.eventListener = eventListener; } public void play(FileInputStream inputStream){ try{ if (eventListener != null){ eventListener.onStop(); } player.reset(); player.setDataSource(inputStream.getFD()); player.prepare(); player.start(); }catch (IOException e){ Log.e(TAG, "play error:" + e); } } public void stop(){ if (player != null && player.isPlaying()){ player.stop(); } } public long getDuration(String path){ player = MediaPlayer.create(IMApplication.getContext(), Uri.parse(path)); return player.getDuration(); } /** * 播放器事件监听 */ public interface EventListener{ void onStop(); } }
希望本文所述对大家Android程序设计有所帮助。
加载全部内容