Android流媒体播放器

转载 2012年03月29日 19:35:20
Android流媒体播放器
2011-09-22 13:39:31
今天比较开心,看到论坛上很多人都在询问如何写网络流媒体播放器?是否需要两个播放器轮流交替?因为Android没有提供像J2me那样的流创建的形式,总之还是不太好写,最近也在研究android流媒体播放,这里提供一个很早之前写的demo,以供大家相互学习和参考。 
/**
 * Android提供的接口仍然是不能直接通过流创建Player,但是Android提供了很全面的文件系统接口,现在用一种新的方法来解决: <br>
 * <1>,把下载下来的数据先写到临时的文件中 <br>
 * <2>,用临时文件创建Player<br>
 * todo: <br>
 * <1>,清理没有用的临时文件。<br>
 * <2>,暂停逻辑的优化。---抛出异常。。。。<br>
 * <3>,分段下载的逻辑处理,这和<2>有关系。<br>
 * <4>,整体优化
 *
 * @author shenrenkui
 *
 */
public class AudioPlayer implements OnErrorListener, OnBufferingUpdateListener,
                   MediaPlayer.OnCompletionListener {
         private static final String TAG = "AudioPlayer";
         private MediaPlayer mPlayer;
         private String current;
         private static final int MIN_BUFF = 100 * 1024;
         private int totalKbRead = 0;
         private Handler handler = new Handler();
         private File DLTempFile;
         private File BUFFTempFile;
         private final String TEMP_DOWNLOAD_FILE_NAME = "tempMediaData";
         private final String TEMP_BUFF_FILE_NAME = "tempBufferData";
         private final String FILE_POSTFIX = ".dat";
         private final int PER_READ = 1024;
         private boolean pause;
         private boolean stop;
         private final int UNKNOWN_LENGTH = -1;
         private Handler mHandler = null;
 
         public void setHandler(Handler handler) {
                   mHandler = handler;
         }
 
         public void play(final String path) {
                   downloadOver = false;
                   totalKbRead = 0;
                   try {
                            Log.v(TAG, "playing: " + path);
                            if (path.equals(current) && mPlayer != null) {
                                     mPlayer.start();
                                     return;
                            }
                            current = path;
                            mPlayer = null;
                            new PlayThread(current).start();
                   } catch (Exception e) {
 
                   }
         }
 
         private void setListener() {
                   if (mPlayer != null) {
                            mPlayer.setOnErrorListener(this);
                            mPlayer.setOnBufferingUpdateListener(this);
                            mPlayer.setOnCompletionListener(this);
                   }
         }
 
         /**
          * 下载数据,分段下载
          * todo:联网方式和分段
          * @param mediaUrl
          * @param start
          * @param end
          */
         private void playFromNet(String mediaUrl, int start, int end) {
                   URLConnection cn = null;
                   FileOutputStream out = null;
                   InputStream is = null;
                   try {
                            cn = new URL(mediaUrl).openConnection();
                            cn.connect();
                            is = cn.getInputStream();
                            int mediaLength = cn.getContentLength();
                            if (is == null) {
                                     return;
                            }
                            deleteTempFile(true);
                            DLTempFile = File.createTempFile(TEMP_DOWNLOAD_FILE_NAME,
                                               FILE_POSTFIX);
                            out = new FileOutputStream(DLTempFile);
                            byte buf[] = new byte[PER_READ];
                            int readLength = 0;
                            while (readLength != -1 && !stop) {
                                     if (pause) {
                                               try {
                                                        Thread.sleep(1000);
                                               } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                               }
                                               continue;
                                     }
                                     readLength = is.read(buf);
                                     if (readLength > 0) {
                                               try {
                                                        out.write(buf, 0, readLength);
                                                        totalKbRead += readLength;
                                               } catch (Exception e) {
                                                        Log.e(TAG, e.toString());
                                               }
                                     }
                                     dealWithBufferData();
                            }
                            if (totalKbRead == mediaLength) {
                                     downloadOver = true;
                                     dealWithLastData();
                                     // 删除临时文件
                                     if (DLTempFile != null && DLTempFile.exists()) {
                                               DLTempFile.delete();
                                     }
                            }
                   } catch (MalformedURLException e) {
                            Log.e(TAG, e.toString());
                   } catch (IOException e) {
                            Log.e(TAG, e.toString());
                   } finally {
                            if (out != null) {
                                     try {
                                               out.close();
                                     } catch (IOException e) {
                                               e.printStackTrace();
                                     }
                            }
                            if (is != null) {
                                     try {
                                               is.close();
                                     } catch (IOException e) {
                                               e.printStackTrace();
                                     }
                            }
                   }
         }
 
         private boolean downloadOver = false;
         private boolean wasPlayed = false;
 
         private void dealWithBufferData() {
                   if (mPlayer == null || !wasPlayed) {
                            if (totalKbRead >= MIN_BUFF) {
                                     try {
                                               startMediaPlayer();
                                     } catch (Exception e) {
                                     }
                            }
                   } else if (mPlayer.getDuration() - mPlayer.getCurrentPosition() <= 1000) {
                            deleteTempFile(true);
                            transferBufferToMediaPlayer();
                   }
         }
 
         private void startMediaPlayer() {
                   try {
                            deleteTempFile(true);
                            BUFFTempFile = File.createTempFile(TEMP_BUFF_FILE_NAME,
                                               FILE_POSTFIX);
                            FileSystemUtil.copyFile(DLTempFile, BUFFTempFile);
                            mPlayer = new MediaPlayer();
                            setListener();
                            mPlayer.setDataSource(BUFFTempFile.getAbsolutePath());
                            mPlayer.prepare();
                            mPlayer.start();
                            wasPlayed = true;
                   } catch (IOException e) {
 
                   }
         }
 
         private void transferBufferToMediaPlayer() {
                   try {
                            boolean wasPlaying = mPlayer.isPlaying();
                            int curPosition = mPlayer.getCurrentPosition();
                            mPlayer.pause();
                            BUFFTempFile = File.createTempFile(TEMP_BUFF_FILE_NAME,
                                               FILE_POSTFIX);
                            FileSystemUtil.copyFile(DLTempFile, BUFFTempFile);
                            mPlayer = new MediaPlayer();
                            mPlayer.setDataSource(BUFFTempFile.getAbsolutePath());
                            mPlayer.prepare();
                            mPlayer.seekTo(curPosition);
                            boolean atEndOfFile = mPlayer.getDuration()
                                               - mPlayer.getCurrentPosition() <= 1000;
                            if (wasPlaying || atEndOfFile) {
                                     mPlayer.start();
                            }
                   } catch (Exception e) {
                   }
         }
 
         private void dealWithLastData() {
                   Runnable updater = new Runnable() {
                            public void run() {
                                     transferBufferToMediaPlayer();
                            }
                   };
                   handler.post(updater);
         }
 
         public void onCompletion(MediaPlayer mp) {
                   if (mHandler != null) {
                            mHandler.sendEmptyMessage(Preferences.MEDIA_ENDED);
                   }
         }
 
         public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
                   if (mediaPlayer != null) {
                            mediaPlayer.stop();
                            mediaPlayer.release();
                   }
                   if (mHandler != null) {
                            mHandler.sendEmptyMessage(Preferences.MEDIA_ERROR);
                   }
                   return true;
         }
 
         public void onBufferingUpdate(MediaPlayer arg0, int percent) {
                   Log.d(TAG, "onBufferingUpdate called --->   percent:" + percent);
                   if (mHandler != null) {
                            mHandler.sendEmptyMessage(Preferences.EMDIA_BUFF_CHANGE);
                   }
         }
 
         private class PlayThread extends Thread {
                   private String url;
 
                   PlayThread(String url) {
                            this.url = url;
                   }
 
                   public void run() {
                            if (!URLUtil.isNetworkUrl(url)) {
                                     mPlayer = new MediaPlayer();
                                     setListener();
                                     try {
                                               // if (url.startsWith("content://")) {
                                               // mPlayer.setDataSource(MediaPlayService.this, Uri
                                               // .parse(url));
                                               // } else {
                                               mPlayer.setDataSource(url);
                                               // }
                                               mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                                               setVolume(0f);
                                               mPlayer.prepare();
                                               mPlayer.start();
                                     } catch (IllegalArgumentException e) {
                                               Log.e(TAG, e.toString());
                                     } catch (IllegalStateException e) {
                                               Log.e(TAG, e.toString());
                                     } catch (IOException e) {
                                               Log.e(TAG, e.toString());
                                     }
                            } else {
                                     playFromNet(url, 0, UNKNOWN_LENGTH);
                            }
                   }
         }
 
}
 

Android流媒体播放器介绍

Android原生流媒体播放Android原生SDK当中有提供流媒体播放API,ViewoView,播放流媒体也挺简单的,但是它支持的流媒体协议有限,例如先前博客里面提到的RTMP,就是不支持的,关于...
  • HACK__ZSMJ
  • HACK__ZSMJ
  • 2016年02月23日 22:08
  • 5043

Android使用MediaPlayer播放流媒体,支持远程以及本地流媒体,一行代码实现

HDMediaPlayer 该类包依托于谷歌Mediaplayer二次封装。 谷歌中国API链接:https://developer.android.google.cn/guide/top...
  • yinhaide
  • yinhaide
  • 2017年05月09日 17:40
  • 4774

Android 播放器之流媒体,边下边播如此简单。

闲来无事,想搞一个流媒体
  • gaoshouxiaodi
  • gaoshouxiaodi
  • 2014年11月18日 23:11
  • 18677

基于ffmpeg的android流媒体播放器

  • 2014年03月19日 09:30
  • 2.5MB
  • 下载

android 流媒体播放

  • 2013年10月22日 14:46
  • 57KB
  • 下载

Android流媒体播放器

 今天比较开心,看到论坛上很多人都在询问如何写网络流媒体播放器?是否需要两个播放器轮流交替?因为Android没有提供像J2me那样的流创建的形式,总之还是不太好写,最近也在研究android流媒体播...
  • feifei454498130
  • feifei454498130
  • 2011年05月31日 09:45
  • 767

EasyPlayer安卓Android流媒体播放器实现直播过程中客户端快照功能

对于一个裸的RTSP URL,存放在播放列表上略显单调与枯燥。大家可以看到EasyPlayer在播放完视频后会保存一帧图片到列表上。那么这个功能是如何做到的呢? 如果自己实现解码的话,比如使用ffm...
  • xiejiashu
  • xiejiashu
  • 2017年02月27日 12:50
  • 1659

EasyPlayer开源流媒体移动端播放器推出RTSP-RTMP-HTTP-HLS全功能Pro版

EasyPlayerPro介绍Android EasyPlayerPro专业版全功能播放器,是由EasyDarwin开源团队维护的一款支持RTSP、RTMP、HTTP、HLS多种流媒体协议的播放器版本...
  • xiejiashu
  • xiejiashu
  • 2017年01月12日 14:04
  • 8291

EasyPlayerPro安卓流媒体播放器实现Android H.265硬解码流程

本文转自EasyDarwin团队成员John的博客:http://blog.csdn.net/jyt0551/article/details/74502627H.265编码算法作为新一代视频编码标准,...
  • xiejiashu
  • xiejiashu
  • 2017年07月06日 08:58
  • 963

android 流媒体 播放器 项目 源码

项目正在进展中。 我现在已经有一些资料。有兴趣的同志加入我们。留下脚步。  此文章会不断更新。   参考资料: 流媒体: http://blog.csdn.net/jingwen369...
  • jingwen3699
  • jingwen3699
  • 2012年07月06日 01:10
  • 15056
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android流媒体播放器
举报原因:
原因补充:

(最多只允许输入30个字)