Android Mediaplayer实现边下边播功能

本文介绍的是mediaplayer实现边下边播的一种方法。

原理:创建两个socket服务,远程socket和本地socket,远程socket用于请求播放资源真实的数据,本地socket用于监听mediaplayer请求,并且将远程socket获取到的数据,写到mediaplyer中进行播放。

为什么需要采用两个socket?
如果只采用一个socket代理,歌曲可以播放正常,但是mediaplayer的seekTo方法失效,原因是mediaplayer在请求数据的时候缺少了一些请求数据,导致mediaPlayer的duration一直为0,所以无法进行seekTo操作。

详细步骤:
一,初始化本地socket代理

   public MediaPlayerProxy(String writeFileName, boolean writeFile) throws Exception {
        proxyIdle = false;
        this.writeFile = writeFile;
        this.writeFileName = writeFileName;
        try {
            if (localServer == null || localServer.isClosed()) {
                //创建本地socket服务器,用来监听mediaplayer请求和给mediaplayer提供数据
                localServer = new ServerSocket();
                localServer.setReuseAddress(true);
                InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName(LOCAL_IP_ADDRESS), local_ip_port);
                localServer.bind(socketAddress);
            }
        } catch (Exception e) {
            LogTool.ex(e);
            try {
                local_ip_port--;
                localServer = new ServerSocket(local_ip_port, 0, InetAddress.getByName(LOCAL_IP_ADDRESS));
                localServer.setReuseAddress(true);
            } catch (Exception e2) {
                LogTool.ex(e2);
                throw new Exception();
            }
        }
    }

二、根据真实的请求音源地址,得到本地的音源地址,将本地音源地址通过setDataSource的方式传递给mediaplayer. 前面创建的本地socket对象监听这个地址,用于获取mediaplayer的请求数据。

public String getLocalURLAndSetRemotSocketAddr(String url) {
        try {
            //真实的播放地址
            remotUrl = url;

            if (writeFile) {
                bufferingMusicUrlList.add(remotUrl);
            }

            String localProxyUrl = "";

            final URI originalURI = URI.create(url);
            final String remoteHost = originalURI.getHost();
            if (!TextUtils.isEmpty(remoteHost)) {
                if (originalURI.getPort() != -1) {//URL带Port
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            remoteAddress = new InetSocketAddress(remoteHost, originalURI.getPort());
                        }
                    }).start();
                   //将真实的播放地址替换成本地的代理地址
                    localProxyUrl = url.replace(remoteHost + ":" + originalURI.getPort(), LOCAL_IP_ADDRESS + ":" + local_ip_port);
                    remoteHostAndPort = remoteHost + ":" + originalURI.getPort();
                } else {//URL不带Port
                    if (!TextUtils.isEmpty(remoteHost)) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                remoteAddress = new InetSocketAddress(remoteHost, HTTP_PORT);//使用80端口
                            }
                        }).start();
                        //将真实的播放地址替换成本地的代理地址
                        localProxyUrl = url.replace(remoteHost, LOCAL_IP_ADDRESS + ":" + local_ip_port);
                        remoteHostAndPort = remoteHost;
                    }
                }
            }
            return localProxyUrl;
        } catch (Exception e) {
            LogTool.ex(e);
            return "";
        }
    }

三、本地socket监听mediaplayer,通过getInputStream方法可以获取到mediaplayer传递过来的请求信息数据,由于我们是通过本地代理地址的方式获取到的,所以我们需要根据这个本地的请求信息替换成真实的远程socket请求信息,向服务器获取真实请求数据。

   public void getTrueSocketRequestInfo(Socket localSocket) throws Exception {
        InputStream in_localSocket = localSocket.getInputStream();
        String trueSocketRequestInfoStr = "";//保存MediaPlayer的真实HTTP请求

        byte[] local_request = new byte[1024];
        while (in_localSocket.read(local_request) != -1) {
            String str = new String(local_request);
            trueSocketRequestInfoStr = trueSocketRequestInfoStr + str;

            if (trueSocketRequestInfoStr.contains("GET") && trueSocketRequestInfoStr.contains("\r\n\r\n")) {
                //把request中的本地ip改为远程ip
                trueSocketRequestInfoStr = trueSocketRequestInfoStr.replace(LOCAL_IP_ADDRESS + ":" + local_ip_port, remoteHostAndPort);
                this.trueSocketRequestInfoStr = trueSocketRequestInfoStr;
                //如果用户拖动了进度条,因为拖动了滚动条还有Range则表示本地歌曲还未缓存完,不再保存
                if (trueSocketRequestInfoStr.contains("Range")) {
                    LogTool.s("=Range=");
                    writeFile = false;
                }
                break;
            }
        }
    }

四、 上一步我们获取到了真实的请求数据信息,此时通过远程socket连接远程请求。

    public Socket sendRemoteRequest() throws Exception {
        //创建远程socket用来请求网络数据
        Socket remoteSocket = new Socket();
        remoteSocket.connect(remoteAddress, socketTimeoutTime);
        remoteSocket.getOutputStream().write(trueSocketRequestInfoStr.getBytes());
        remoteSocket.getOutputStream().flush();
        return remoteSocket;
    }

五、将远程socket的数据,通过本地socket写入mediaplayer进行播放。

    public void processTrueRequestInfo(Socket remoteSocket, Socket localSocket) {
        //如果要写入本地文件的实例声明
        FileOutputStream fileOutputStream = null;
        File theFile = null;

        try {
            //获取音乐网络数据
            InputStream in_remoteSocket = remoteSocket.getInputStream();
            if (in_remoteSocket == null) return;

            OutputStream out_localSocket = localSocket.getOutputStream();
            if (out_localSocket == null) return;

            //如果要写入文件,配置相关实例
            if (writeFile) {
                File dirs = new File(Environment.getExternalStorageDirectory() + File.separator + "clearlee_music");
                dirs.mkdirs();
                theFile = new File(dirs + File.separator + writeFileName + ".m4a");
                fileOutputStream = new FileOutputStream(theFile);
            }

            try {
                int readLenth;
                byte[] remote_reply = new byte[4096];
                boolean firstData = true;//是否循环中第一次获得数据

                //当从远程还能取到数据且播放器还没切换另一首网络音乐
                while ((readLenth = in_remoteSocket.read(remote_reply, 0, remote_reply.length)) != -1 && currProxyId == lastProxyId) {

                    //首先从数据中获得文件总长度
                    try {
                        if (firstData) {
                            firstData = false;
                            String str = new String(remote_reply, "utf-8");
                            Pattern pattern = Pattern.compile("Content-Length:\\s*(\\d+)");
                            Matcher matcher = pattern.matcher(str);
                            if (matcher.find()) {
                                //获取数据的大小
                                fileTotalLength = Long.parseLong(matcher.group(1));
                            }
                        }
                    } catch (Exception e) {
                        LogTool.ex(e);
                    }

                    //把远程sokcet拿到的数据用本地socket写到mediaplayer中播放
                    try {
                        out_localSocket.write(remote_reply, 0, readLenth);
                        out_localSocket.flush();
                    } catch (Exception e) {
                        LogTool.ex(e);
                    }

                    //计算当前播放时,其在seekbar上的缓冲值,并刷新进度条
                    try {
                        cachedFileLength += readLenth;
                        if (fileTotalLength > 0 && currProxyId == lastProxyId) {
                            currMusicCachedProgress = (int) (Common.div(cachedFileLength, fileTotalLength, 5) * 100);
                            if (mOnCaChedProgressUpdateListener != null && currMusicCachedProgress <= 100) {
                                mOnCaChedProgressUpdateListener.updateCachedProgress(currMusicCachedProgress);
                            }
                        }
                    } catch (Exception e) {
                        LogTool.ex(e);
                    }

                    //如果需要缓存数据到本地,就缓存到本地
                    if (writeFile) {
                        try {
                            if (fileOutputStream != null) {
                                fileOutputStream.write(remote_reply, 0, readLenth);
                                fileOutputStream.flush();
                            }
                        } catch (Exception e) {
                            LogTool.ex(e);
                        }
                    }
                }

                //如果是因为切换音乐跳出循环的,当前音乐播放进度,小于 seekbar最大值的1/4,就把当前音乐缓存在本地的数据清除了
                if (currProxyId != lastProxyId && currPlayDegree < 25) {
                    bufferingMusicUrlList.remove(remotUrl);
                    if (theFile != null) {
                        Common.deleteFile(theFile.getPath());
                    }
                }

            } catch (Exception e) {
                LogTool.ex(e);
                if (theFile != null) {
                    Common.deleteFile(theFile.getPath());
                }
                bufferingMusicUrlList.remove(remotUrl);

            } finally {
                in_remoteSocket.close();
                out_localSocket.close();
                if (fileOutputStream != null) {
                    fileOutputStream.close();

                    //音频文件缓存完后处理
                    if (theFile != null && Common.checkFileExist(theFile.getPath())) {
                        conver2RightAudioFile(theFile);
                        if (musicControlInterface != null) {
                            musicControlInterface.updateBufferFinishMusicPath(musicKey, theFile.getPath());
                            bufferingMusicUrlList.remove(remotUrl);
                        }
                    }

                }
                localSocket.close();
                remoteSocket.close();
            }

        } catch (Exception e) {
            LogTool.ex(e);
            if (theFile != null) {
                Common.deleteFile(theFile.getPath());
            }
            bufferingMusicUrlList.remove(remotUrl);
        }
    }

至此,mediaplayer便实现了边下边播的功能。

详细代码已上传至github,地址:https://github.com/Clearlee/PlayWhileDownloadMusic

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容