Android Mediaplayer實(shí)現(xiàn)邊下邊播功能

本文介紹的是mediaplayer實(shí)現(xiàn)邊下邊播的一種方法。

原理:創(chuàng)建兩個(gè)socket服務(wù),遠(yuǎn)程socket和本地socket,遠(yuǎn)程socket用于請(qǐng)求播放資源真實(shí)的數(shù)據(jù),本地socket用于監(jiān)聽mediaplayer請(qǐng)求,并且將遠(yuǎn)程socket獲取到的數(shù)據(jù),寫到mediaplyer中進(jìn)行播放。

為什么需要采用兩個(gè)socket?
如果只采用一個(gè)socket代理,歌曲可以播放正常,但是mediaplayer的seekTo方法失效,原因是mediaplayer在請(qǐng)求數(shù)據(jù)的時(shí)候缺少了一些請(qǐng)求數(shù)據(jù),導(dǎo)致mediaPlayer的duration一直為0,所以無法進(jìn)行seekTo操作。

詳細(xì)步驟:
一,初始化本地socket代理

   public MediaPlayerProxy(String writeFileName, boolean writeFile) throws Exception {
        proxyIdle = false;
        this.writeFile = writeFile;
        this.writeFileName = writeFileName;
        try {
            if (localServer == null || localServer.isClosed()) {
                //創(chuàng)建本地socket服務(wù)器,用來監(jiān)聽mediaplayer請(qǐng)求和給mediaplayer提供數(shù)據(jù)
                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();
            }
        }
    }

二、根據(jù)真實(shí)的請(qǐng)求音源地址,得到本地的音源地址,將本地音源地址通過setDataSource的方式傳遞給mediaplayer. 前面創(chuàng)建的本地socket對(duì)象監(jiān)聽這個(gè)地址,用于獲取mediaplayer的請(qǐng)求數(shù)據(jù)。

public String getLocalURLAndSetRemotSocketAddr(String url) {
        try {
            //真實(shí)的播放地址
            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();
                   //將真實(shí)的播放地址替換成本地的代理地址
                    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();
                        //將真實(shí)的播放地址替換成本地的代理地址
                        localProxyUrl = url.replace(remoteHost, LOCAL_IP_ADDRESS + ":" + local_ip_port);
                        remoteHostAndPort = remoteHost;
                    }
                }
            }
            return localProxyUrl;
        } catch (Exception e) {
            LogTool.ex(e);
            return "";
        }
    }

三、本地socket監(jiān)聽mediaplayer,通過getInputStream方法可以獲取到mediaplayer傳遞過來的請(qǐng)求信息數(shù)據(jù),由于我們是通過本地代理地址的方式獲取到的,所以我們需要根據(jù)這個(gè)本地的請(qǐng)求信息替換成真實(shí)的遠(yuǎn)程socket請(qǐng)求信息,向服務(wù)器獲取真實(shí)請(qǐng)求數(shù)據(jù)。

   public void getTrueSocketRequestInfo(Socket localSocket) throws Exception {
        InputStream in_localSocket = localSocket.getInputStream();
        String trueSocketRequestInfoStr = "";//保存MediaPlayer的真實(shí)HTTP請(qǐng)求

        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改為遠(yuǎn)程ip
                trueSocketRequestInfoStr = trueSocketRequestInfoStr.replace(LOCAL_IP_ADDRESS + ":" + local_ip_port, remoteHostAndPort);
                this.trueSocketRequestInfoStr = trueSocketRequestInfoStr;
                //如果用戶拖動(dòng)了進(jìn)度條,因?yàn)橥蟿?dòng)了滾動(dòng)條還有Range則表示本地歌曲還未緩存完,不再保存
                if (trueSocketRequestInfoStr.contains("Range")) {
                    LogTool.s("=Range=");
                    writeFile = false;
                }
                break;
            }
        }
    }

四、 上一步我們獲取到了真實(shí)的請(qǐng)求數(shù)據(jù)信息,此時(shí)通過遠(yuǎn)程socket連接遠(yuǎn)程請(qǐng)求。

    public Socket sendRemoteRequest() throws Exception {
        //創(chuàng)建遠(yuǎn)程socket用來請(qǐng)求網(wǎng)絡(luò)數(shù)據(jù)
        Socket remoteSocket = new Socket();
        remoteSocket.connect(remoteAddress, socketTimeoutTime);
        remoteSocket.getOutputStream().write(trueSocketRequestInfoStr.getBytes());
        remoteSocket.getOutputStream().flush();
        return remoteSocket;
    }

五、將遠(yuǎn)程socket的數(shù)據(jù),通過本地socket寫入mediaplayer進(jìn)行播放。

    public void processTrueRequestInfo(Socket remoteSocket, Socket localSocket) {
        //如果要寫入本地文件的實(shí)例聲明
        FileOutputStream fileOutputStream = null;
        File theFile = null;

        try {
            //獲取音樂網(wǎng)絡(luò)數(shù)據(jù)
            InputStream in_remoteSocket = remoteSocket.getInputStream();
            if (in_remoteSocket == null) return;

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

            //如果要寫入文件,配置相關(guān)實(shí)例
            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;//是否循環(huán)中第一次獲得數(shù)據(jù)

                //當(dāng)從遠(yuǎn)程還能取到數(shù)據(jù)且播放器還沒切換另一首網(wǎng)絡(luò)音樂
                while ((readLenth = in_remoteSocket.read(remote_reply, 0, remote_reply.length)) != -1 && currProxyId == lastProxyId) {

                    //首先從數(shù)據(jù)中獲得文件總長度
                    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()) {
                                //獲取數(shù)據(jù)的大小
                                fileTotalLength = Long.parseLong(matcher.group(1));
                            }
                        }
                    } catch (Exception e) {
                        LogTool.ex(e);
                    }

                    //把遠(yuǎn)程sokcet拿到的數(shù)據(jù)用本地socket寫到mediaplayer中播放
                    try {
                        out_localSocket.write(remote_reply, 0, readLenth);
                        out_localSocket.flush();
                    } catch (Exception e) {
                        LogTool.ex(e);
                    }

                    //計(jì)算當(dāng)前播放時(shí),其在seekbar上的緩沖值,并刷新進(jìn)度條
                    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);
                    }

                    //如果需要緩存數(shù)據(jù)到本地,就緩存到本地
                    if (writeFile) {
                        try {
                            if (fileOutputStream != null) {
                                fileOutputStream.write(remote_reply, 0, readLenth);
                                fileOutputStream.flush();
                            }
                        } catch (Exception e) {
                            LogTool.ex(e);
                        }
                    }
                }

                //如果是因?yàn)榍袚Q音樂跳出循環(huán)的,當(dāng)前音樂播放進(jìn)度,小于 seekbar最大值的1/4,就把當(dāng)前音樂緩存在本地的數(shù)據(jù)清除了
                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便實(shí)現(xiàn)了邊下邊播的功能。

詳細(xì)代碼已上傳至github,地址:https://github.com/Clearlee/PlayWhileDownloadMusic

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容