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

推荐阅读更多精彩内容