美文网首页
缓存音频到本地

缓存音频到本地

作者: 啤酒小龙虾 | 来源:发表于2018-06-12 16:22 被阅读0次

private MediaPlayermediaPlayer =new MediaPlayer();

private Stringpath; //网络音频

private String mp3String; //本地路径

private SharedPreferencesUtilspreferencesUtils;

private long videoTotalSize =0;

private StringlocalUrl;

private long videoCacheSize =0;

//再要播放的位置调用下面的逻辑

path = “网络音乐路径”;

    mp3String = (String)preferencesUtils.get(Contexts.MUSIC_PATH, "");

    if (!mp3String.equals(path)) {

        new Thread(new Runnable() {

@Override

            public void run() {

try {

mp3String =path;

                    preferencesUtils.put(Contexts.MUSIC_PATH, path);

                    prepareVideo(path);

                }catch (IOException e) {

e.printStackTrace();

                }

}

}).start();

    }else {

        localUrl = (String)preferencesUtils.get(Contexts.MUSIC_URL, "");

        try {

playMusicBySDCar(localUrl);

            Log.d("PATH", localUrl);

        }catch (IOException e) {

e.printStackTrace();

        }

}

}

/**

* 下载缓存音频

*

* @param remoteUrl

* @throws IOException

*/

private void prepareVideo(String remoteUrl)throws IOException {

URL url =new URL(remoteUrl);

    HttpURLConnection httpConnection = (HttpURLConnection) url

.openConnection();

    httpConnection.setConnectTimeout(3000);

    httpConnection.setRequestProperty("RANGE", "bytes=" +0 +"-");

    InputStream is = httpConnection.getInputStream();

    videoTotalSize = httpConnection.getContentLength();

    if (videoTotalSize == -1) {

return;

    }

String name = remoteUrl.substring(remoteUrl.lastIndexOf("/") +1);

    localUrl = Environment.getExternalStorageDirectory().getAbsolutePath()

+"/VideoCache/" + File.separator + name;

    //保存路径

    preferencesUtils.put(Contexts.MUSIC_URL, localUrl);

    File cacheFile =new File(localUrl);

    if (!cacheFile.exists()) {

cacheFile.getParentFile().mkdirs();

        cacheFile.createNewFile();

    }

RandomAccessFile raf =new RandomAccessFile(cacheFile, "rws");

    raf.setLength(videoTotalSize);

    raf.seek(0);

    byte buf[] =new byte[10 *1024];

    int size =0;

    videoCacheSize =0;

    int buffercnt =0;

    while ((size = is.read(buf)) != -1) {

try {

raf.write(buf, 0, size);

            videoCacheSize += size;

        }catch (Exception e) {

e.printStackTrace();

        }

}

is.close();

    raf.close();

    playMusicBySDCar(localUrl);

}

private void playMusicBySDCar(String localUrl)throws IOException {

mediaPlayer.setDataSource(localUrl);

    mediaPlayer.prepare();

    mediaPlayer.start();

    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

@Override

        public void onCompletion(MediaPlayer mp) {

mp.reset();

        }

});

}

相关文章

  • 缓存音频到本地

    private MediaPlayermediaPlayer =new MediaPlayer();private...

  • 微信小程序播放缓存的音频文件

    很多时候我们都想把数据预先缓存到本地,节省带宽。但是最近在处理微信小程序播放缓存到本地的音频文件的时候,遇到一些小...

  • Android 音视频缓存框架-AndroidVideoCach

    AndroidVideoCache是一个视频/音频缓存库,利用本地代理实现了边下边播。 HttpProxyCach...

  • git基础操作记录

    基本操作 1. 克隆远程仓库到本地 2. 将修改代码放入本地缓存 3. 查看本地缓存 4.将本地修改提交 输入修改...

  • iOS 封装控件之音视频播放

    音频播放 视频播放 本地播放、网络播放、边下边播 缓存进度 播放进度 自动播放 音频后台播放 全屏播放(demo中...

  • Guava cache使用总结

    缓存分为本地缓存和远端缓存。常见的远端缓存有Redis,MongoDB;本地缓存一般使用map的方式保存在本地内存...

  • js 数组对象去重问题

    1、问题阐述 在做列表数据的时候,本地缓存数据数组,在请求到新数据以后需要添加到本地缓存中并显示到列表,其中比...

  • Redis入门--缓存介绍

    学习笔记 缓存的类型: 1. 本地缓存 本地缓存就是在进程的内存中进行缓存,比如JVM中的堆。 本地缓存是...

  • 数据缓存

    缓存的四种方式?各自应用的场景? 缓存本质将请求到的数据存储到本地,将数据显示到UI界面前先询问本地数据库是否已经...

  • git篇之add

    添加本地工作区改动到本地仓库缓存区 git add [file1] [file2] -->添加多个文件到缓存区gi...

网友评论

      本文标题:缓存音频到本地

      本文链接:https://www.haomeiwen.com/subject/hgcreftx.html