Android之解决Mediaplayer播放网络音频缓冲太慢的问题

  相信有不少人在使用Mediaplayer播放网络音频的时候,prepare与prepareAsync的方法缓冲太慢,如何解决问题?现在就给出一个亲身测试并且用到实际项目中的方案。
  该方案中,用到了框架Vitamio(http://vitamio.org/),在下载页面中有比较详细的下载项目与介绍。主要是Demo下载地址:https://github.com/yixia/VitamioDemo 与 依赖库:https://github.com/yixia/VitamioBundle/tree/v3.0

  不过如果仅仅使用依赖库而不使用自己的重新编译的FFmpeg,那个res下的动态库so有5m大,将会造成apk过大的问题。以下就是自己编译FFmpeg的步骤与注意的地方。这是官方提供的:如何在Vitamio中使用自己编译的FFmpeghttp://vitamio.org/pages/how-to-use-vitamio-with-your-own-ffmpeg-build?locale=zh-CN

  我是在MAC的系统上编译的,其他系统也差不多,主要是注意自己的环境那些,例如mac下需要gcc,window下需要cygwin等
  1. 确定自己的mac上已经安装了gcc的编译环境,如果没有请到xcode下载
  2. 下载ndk,vitamio推荐我们使用的是ndk-r8b的版本
  3. 在终端下输入:export ANDROID_NDK=/path/to/your/android-ndk  (export是关键字;ANDROID_NDK照着打吧;后面的地址就是你解压的ndk地址)

  4. 在终端定位到http://github.com/yixia/FFmpeg-Android网址下载的文件夹的目录,然后执行  ./FFmpeg-Android.sh命令。
  4.1 执行git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg下载ffmpeg的代码,然后才能使用configure --help 命令。

  5. 补充4。 根据官方的解释,我们可以在FFmpeg-Android.sh 进行适当修改,我们主要是关注FFMPEG_FLAGS下面的配置。
  如果需要配置多个,可以用英文的','分开
  --enable-decoder=aac,mp3,mpeg4 \ 实践了,这样写ok
    5.1 附上一个FFMPEG_FLAGS配置例子:

FFMPEG_FLAGS="--target-os=linux \
  --arch=arm \
  --enable-cross-compile \
  --cross-prefix=arm-linux-androideabi- \
  --enable-shared \
  --disable-symver \
  --disable-doc \
  --disable-ffplay \
  --disable-ffmpeg \
  --disable-ffprobe \
  --disable-ffserver \
  --disable-avdevice \
  --disable-avfilter \
  --disable-encoders \
  --disable-muxers \
  --disable-filters \
  --disable-devices \
  --disable-everything \
  --enable-protocol=http,md5,https,cache,file,httpproxy  \
  --enable-parser=aac,aac_latm,mpegaudio \
  --enable-demuxer=aac,ogg \
  --enable-decoder=aac_latm,aac \
  --enable-bsf=aac_adtstoasc,noise \
  --enable-network \
  --enable-swscale  \
  --enable-asm \
  --enable-version3"

  6. 执行是漫长的等待,不过这时不要松懈。你有可能会出现类似ccache的错误。这个错误会引起不能生成libffmpeg.so文件的错误

    6.1 解决办法:到网上http://mxcl.github.com/homebrew/ 把类似下面的地址复制到终端,回车执行 
     6.2 ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
   
6.3 
安装完成之后吧,执行 brew install ccache 
    6.4 重新再编译执行sh文件,这时候就可以了
  7. 官方文档也提到VitamioBundle中的res/raw/libarm.so这个文件是7-zip压缩文件。得到这个信息后,我们可以在终端下使用命令 brew search 7zip搜索需要的软件,找到之后就可以使用brew install X    X是你需要的那个软件名字。
    然后根据7z --help命令得到解压so文件的指令,把libffmpeg.so替换解压出来的libffmpeg.so,官网http://vitamio.org/pages/how-to-use-vitamio-with-your-own-ffmpeg-build?locale=zh-CN有提示,替换完之后再用指令打包回so文件。
  8. 把这个自己编译的文件覆盖 res/raw/libarm.so文件,到此,所有的操作已经完成。稍后一些时间,我会完善这一文档及附上部分图片。
  9. 附上p7zip的解压指令:
    9.1 pz x 需要解压的文件 -o指定的解压到的目录,例:7z x /Users/Ari/Downloads/VitamioBundle-3.0/res/raw/libarm.so -o/Users/Ari/Downloads/VitamioBundle-3.0/res/raw/lib

    
9.2 pz a 指定压缩到的目录 需要压缩的文件或目录,使用空格隔开多个,例:7z a /Users/Ari/Downloads/VitamioBundle-3.0/res/raw/libarm.so 60 61 70 71

 

 

 

原文地址:https://www.cnblogs.com/lee0oo0/p/2998921.html