玩转 Android MediaPlayer之视频预加载(优化)
MediaPlayer由厂家定制,不同终端的MediaPlayer略有差异,例如:有些MediaPlayer首次播放从头buffer,有些MdiaPlayer首次播放会多次Request,Range到网络媒体文件的头部、中间和文件尾,再从指定位置buffer...本文所做的优化就是适应播放前多次Request的MediaPlayer。
决定预加载效果好坏由三因素决定:
网速
缓冲文件大小
视频码率
码率低、网速快的情况没必要使用预加载,码率中等、网速一般的情况合适使用。另外,缓冲文件也不能设置太大:过大的缓冲区会刷爆MediaPlayer内置的缓冲区,影响正常播放;再者,读取缓冲文件也耗时。
先看看本文程序的运行结果,以下是不使用预加载的运行LOG:
08-27 10:34:55.222: E//mnt/sdcard/ProxyBuffer/files(12949): --------共有0个缓存文件
08-27 10:34:55.327: E/MediaPlayer(12949): MediaPlayer::setVideoRect(0,25, 1280, 720)
08-27 10:34:55.327: E/(12949): IMediaPlayer::setVideoRect(0,25, 1280, 720)
08-27 10:34:55.367: E/MediaPlayer(12949): MediaPlayer::setVideoRect(0,25, 1280, 720)
08-27 10:34:55.367: E/(12949): IMediaPlayer::setVideoRect(0,25, 1280, 720)
08-27 10:35:01.152: E/MediaPlayer(12949): MediaPlayer::setVideoRect(0,25, 1280, 720)
08-27 10:35:01.152: E/(12949): IMediaPlayer::setVideoRect(0,25, 1280, 720)
08-27 10:35:01.402: E/MediaPlayer(12949): MediaPlayer::setVideoRect(0,25, 1280, 720)
08-27 10:35:01.402: E/(12949): IMediaPlayer::setVideoRect(0,25, 1280, 720)
08-27 10:35:02.382: E/testVideoPlayer(12949): 预加载开关:false,等待缓冲时间:8000,首次缓冲时间:7152
以下是使用预加载的运行LOG,内容有点多,这个MediaPlayer就是首次播放前多次Request:
08-27 10:40:02.627: E//mnt/sdcard/ProxyBuffer/files(13769): --------共有0个缓存文件
08-27 10:40:02.777: E/testVideoPlayer(13769): 预加载文件:/mnt/sdcard/ProxyBuffer/files/videorzx201208151345010952759.mp4
08-27 10:40:02.972: E/DownloadThread(13769): /mnt/sdcard/ProxyBuffer/files/videorzx201208151345010952759.mp4
08-27 10:40:10.782: E/MediaPlayer(13769): MediaPlayer::setVideoRect(0,25, 1280, 720)
08-27 10:40:10.782: E/(13769): IMediaPlayer::setVideoRect(0,25, 1280, 720)
08-27 10:40:10.787: E/HttpGetProxy(13769): ------------------------------------------------------------------
08-27 10:40:10.787: E/HttpGetProxy(13769): java.lang.NullPointerException
08-27 10:40:10.787: E/HttpGetProxy(13769): com.proxy.HttpGetProxy.startProxy 171line
08-27 10:40:10.787: E/HttpGetProxy(13769): com.proxy.HttpGetProxy.access$0 134line
08-27 10:40:10.787: E/HttpGetProxy(13769): com.proxy.HttpGetProxy$1.run 129line
08-27 10:40:10.787: E/HttpGetProxy(13769): ------------------------------------------------------------------
08-27 10:40:10.792: E/HttpParser(13769): GET /video/rzx/201208/15/1345010952759.mp4 HTTP/1.1
08-27 10:40:10.792: E/HttpParser(13769): Range: bytes=0-
08-27 10:40:10.792: E/HttpParser(13769): Host: video.cztv.com
08-27 10:40:10.792: E/HttpParser(13769): Accept: */*
08-27 10:40:10.792: E/HttpParser(13769): Pragma: no-cache
08-27 10:40:10.792: E/HttpParser(13769):
08-27 10:40:10.792: E/HttpParser(13769): _prebufferFilePath:/mnt/sdcard/ProxyBuffer/files/videorzx201208151345010952759.mp4
08-27 10:40:10.792: E/HttpParser(13769): ------->rangePosition:0
08-27 10:40:10.792: E/HttpGetProxy(13769): prebuffer size:999296
08-27 10:40:10.797: E/DownloadThread(13769): mTotalSize:8311866,mTargetSize:3145728
08-27 10:40:11.762: E/HttpGetProxy<---(13769): HTTP/1.1 206 Partial Content
08-27 10:40:11.762: E/HttpGetProxy<---(13769): Date: Mon, 27 Aug 2012 02:33:24 GMT
08-27 10:40:11.762: E/HttpGetProxy<---(13769): Server: Apache
08-27 10:40:11.762: E/HttpGetProxy<---(13769): X-Mod-H264-Streaming: version=2.2.7
08-27 10:40:11.762: E/HttpGetProxy<---(13769): Last-Modified: Wed, 15 Aug 2012 06:24:38 GMT
08-27 10:40:11.762: E/HttpGetProxy<---(13769): Accept-Ranges: bytes
08-27 10:40:11.762: E/HttpGetProxy<---(13769): Cache-Control: max-age=315360000
08-27 10:40:11.762: E/HttpGetProxy<---(13769): Expires: Thu, 25 Aug 2022 02:33:24 GMT
08-27 10:40:11.762: E/HttpGetProxy<---(13769): Content-Type: video/mp4
08-27 10:40:11.762: E/HttpGetProxy<---(13769): Powered-By-ChinaCache: HIT from 01006613Y4
08-27 10:40:11.762: E/HttpGetProxy<---(13769): Content-Range: bytes 0-8311865/8311866
08-27 10:40:11.762: E/HttpGetProxy<---(13769): Content-Length: 8311866
08-27 10:40:11.762: E/HttpGetProxy<---(13769): Age: 407
08-27 10:40:11.762: E/HttpGetProxy<---(13769): Powered-By-ChinaCache: HIT from 01075913r4
08-27 10:40:11.762: E/HttpGetProxy<---(13769):
08-27 10:40:11.777: E/HttpGetProxy(13769): .........over..........
08-27 10:40:11.777: E/HttpGetProxy(13769): ------------------------------------------------------------------
08-27 10:40:11.782: E/HttpParser(13769): GET /video/rzx/201208/15/1345010952759.mp4 HTTP/1.1
08-27 10:40:11.782: E/HttpParser(13769): Range: bytes=101901-
08-27 10:40:11.782: E/HttpParser(13769): Host: video.cztv.com
08-27 10:40:11.782: E/HttpParser(13769): Accept: */*
08-27 10:40:11.782: E/HttpParser(13769): Pragma: no-cache
08-27 10:40:11.782: E/HttpParser(13769):
08-27 10:40:11.782: E/HttpParser(13769): _prebufferFilePath:/mnt/sdcard/ProxyBuffer/files/videorzx201208151345010952759.mp4
08-27 10:40:11.782: E/HttpParser(13769): ------->rangePosition:101901
08-27 10:40:11.782: E/HttpGetProxy(13769): prebuffer size:1000320
08-27 10:40:12.167: E/HttpGetProxy<---(13769): HTTP/1.1 206 Partial Content
08-27 10:40:12.167: E/HttpGetProxy<---(13769): Date: Mon, 27 Aug 2012 02:33:24 GMT
08-27 10:40:12.167: E/HttpGetProxy<---(13769): Server: Apache
08-27 10:40:12.167: E/HttpGetProxy<---(13769): X-Mod-H264-Streaming: version=2.2.7
08-27 10:40:12.167: E/HttpGetProxy<---(13769): Last-Modified: Wed, 15 Aug 2012 06:24:38 GMT
08-27 10:40:12.167: E/HttpGetProxy<---(13769): Accept-Ranges: bytes
08-27 10:40:12.167: E/HttpGetProxy<---(13769): Cache-Control: max-age=315360000
08-27 10:40:12.167: E/HttpGetProxy<---(13769): Expires: Thu, 25 Aug 2022 02:33:24 GMT
08-27 10:40:12.167: E/HttpGetProxy<---(13769): Content-Type: video/mp4
08-27 10:40:12.167: E/HttpGetProxy<---(13769): Powered-By-ChinaCache: HIT from 01006613Y4
08-27 10:40:12.167: E/HttpGetProxy<---(13769): Content-Range: bytes 101901-8311865/8311866
08-27 10:40:12.167: E/HttpGetProxy<---(13769): Content-Length: 8209965
08-27 10:40:12.167: E/HttpGetProxy<---(13769): Age: 408
08-27 10:40:12.167: E/HttpGetProxy<---(13769): Powered-By-ChinaCache: HIT from 01075913r4
08-27 10:40:12.167: E/HttpGetProxy<---(13769):
08-27 10:40:12.172: E/HttpGetProxy(13769): >>>skip:101901
08-27 10:40:12.182: E/HttpGetProxy(13769): .........over..........
08-27 10:40:12.182: E/HttpGetProxy(13769): ------------------------------------------------------------------
08-27 10:40:12.187: E/HttpParser(13769): GET /video/rzx/201208/15/1345010952759.mp4 HTTP/1.1
08-27 10:40:12.187: E/HttpParser(13769): Range: bytes=74-
08-27 10:40:12.187: E/HttpParser(13769): Host: video.cztv.com
08-27 10:40:12.187: E/HttpParser(13769): Accept: */*
08-27 10:40:12.187: E/HttpParser(13769): Pragma: no-cache
08-27 10:40:12.187: E/HttpParser(13769):
08-27 10:40:12.187: E/HttpParser(13769): _prebufferFilePath:/mnt/sdcard/ProxyBuffer/files/videorzx201208151345010952759.mp4
08-27 10:40:12.187: E/HttpParser(13769): ------->rangePosition:74
08-27 10:40:12.187: E/HttpGetProxy(13769): prebuffer size:1000320
08-27 10:40:12.372: E/HttpGetProxy<---(1
补充:移动开发 , Android ,