iOS Airplay--Airtunes音乐播放在Android盒子和手机上的实现 (第一篇)

一、前言

在局域网中实现流媒体的播放有2种主要方式,Airplay和DLNA。对于iOS系统,天生带了Airplay,但可惜是苹果秉承一贯的作风,Airplay是一个闭源协议。万幸有大神逆向了协议内容,使得我们可以去搭建服务完成此项工作。这个协议的复杂度还是略高,使得我也不可能一口气写出完整的文章,咱们就一篇篇的道来。

二、让iOS通过AirTurns发现Android设备

Airplay是基于局域网的服务,在相同wifi的内网下,苹果设备会去搜寻支持Airplay服务的设备。
我们可以通过mDNS服务向局域网中发送一个MultiCast广播,这样iOS设备在内网中就可以发现你(Android设备)了。Android可以使用JmDNS这个库来构建相关代码。

- 代码实现

  • 1.获取满足条件的本地网络设备接口。去除没有运行的设备,过滤掉回送 、点对点和虚拟接口的,并且去掉不支持MultiCast的设备接口。
for (final NetworkInterface networkInterface : workInterfaces) {
            //如果网络设备接口是 回送接口 & 点对点接口 & 没有运行 & 虚拟端口,则跳过执行
            if (networkInterface.isLoopback() || networkInterface.isPointToPoint() || !networkInterface.isUp()
                    || networkInterface.isVirtual()) {
                continue;
            }
            // 不支持组播  跳过
            if (!networkInterface.supportsMulticast()) {
                continue;
            }
  • 2.对于满足条件的设备,选取ipv4和ipv6的端口。
for (final InetAddress address : Collections.list(networkInterface.getInetAddresses())) {
                        //端口是是ipv4 或者  ipv6 端口
                        if (address instanceof Inet4Address || address instanceof Inet6Address) {
                            try {
                                final JmDNS jmDNS = JmDNS.create(address, hostName);
                                jmDNSList.add(jmDNS);
  • 3.对满足条件的端口号开启 AirTunes/RAOP (远程音频传输协议)服务。

协议常量定义

/**
     * The AirTunes/RAOP service type
     */
    static final String AIR_TUNES_SERVICE_TYPE = "_raop._tcp.local.";

    /**
     * The AirTunes/RAOP M-DNS service properties (TXT record)
     */
    static final Map AIRTUNES_SERVICE_PROPERTIES = NetworkUtils.map(
            "txtvers", "1",
            "tp", "UDP",
            "ch", "2",
            "ss", "16",
            "sr", "44100",
            "pw", "false",
            "sm", "false",
            "sv", "false",
            "ek", "1",
            "et", "0,1",
            "cn", "0,1",
            "vn", "3");

    /**
     * The AirTunes/RAOP RTSP port
     */
    private int rtspPort = 5000; //default value

逻辑代码


final ServiceInfo airTunesServiceInfo = ServiceInfo.create(
                                        AIR_TUNES_SERVICE_TYPE,
                                        hardwareAddressString + "@" + hostName,
                                        getRstpPort(),
                                        0,
                                        0,
                                        AIRTUNES_SERVICE_PROPERTIES
                                );
                                jmDNS.registerService(airTunesServiceInfo);
  • 4 完整工作线程代码如下
package ss.serven.rduwan.airtunesandroid;

import android.util.Log;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;

/**
 * Created by rduwan on 17/6/29.
 */

public class AirTunesRunnable implements Runnable {

    /**
     * The AirTunes/RAOP service type
     */
    static final String AIR_TUNES_SERVICE_TYPE = "_raop._tcp.local.";

    /**
     * The AirTunes/RAOP M-DNS service properties (TXT record)
     */
    static final Map AIRTUNES_SERVICE_PROPERTIES = NetworkUtils.map(
            "txtvers", "1",
            "tp", "UDP",
            "ch", "2",
            "ss", "16",
            "sr", "44100",
            "pw", "false",
            "sm", "false",
            "sv", "false",
            "ek", "1",
            "et", "0,1",
            "cn", "0,1",
            "vn", "3");

    /**
     * The AirTunes/RAOP RTSP port
     */
    private int rtspPort = 5000; //default value

    protected List jmDNSList;

    private static AirTunesRunnable instance = null;

    private final static String TAG = "AirTunesRunnable";

    private AirTunesRunnable() {
        jmDNSList = new java.util.LinkedList();
    }

    public synchronized static AirTunesRunnable getInstance() {
        if (instance == null) {
            instance = new AirTunesRunnable();
        }
        return instance;
    }


    @Override
    public void run() {
        startAirTunesService();
    }

    private void startAirTunesService() {
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                onAppShutDown();
            }
        }));
        sendMulitCastToiOS();
    }

    /**
     * 通过DNS服务给局域网里面的iOS发送组播,使得iOS设备能发现你
     * java 使用JmDNS库
     */
    private void sendMulitCastToiOS() {
        //get Network details
        NetworkUtils networkUtils = NetworkUtils.getInstance();
        String hostName = "Rduwan-AirTunes";
        networkUtils.setHostName(hostName);
        String hardwareAddressString = networkUtils.getHardwareAddressString();
        try {
            synchronized (jmDNSList) {
                List workInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
                for (final NetworkInterface networkInterface : workInterfaces) {
                    //如果网络设备几口是 回送接口 & 点对点接口 & 没有运行 & 虚拟端口,则跳过执行
                    if (networkInterface.isLoopback() || networkInterface.isPointToPoint() || !networkInterface.isUp()
                            || networkInterface.isVirtual()) {
                        continue;
                    }
                    // 不支持组播  跳过
                    if (!networkInterface.supportsMulticast()) {
                        continue;
                    }

                    for (final InetAddress address : Collections.list(networkInterface.getInetAddresses())) {
                        //端口是是ipv4 或者  ipv6 端口
                        if (address instanceof Inet4Address || address instanceof Inet6Address) {
                            try {
                                final JmDNS jmDNS = JmDNS.create(address, hostName);
                                jmDNSList.add(jmDNS);

                                //构建AirTunes/RAOP (远程音频传输协议)服务
                                final ServiceInfo airTunesServiceInfo = ServiceInfo.create(
                                        AIR_TUNES_SERVICE_TYPE,
                                        hardwareAddressString + "@" + hostName,
                                        getRstpPort(),
                                        0,
                                        0,
                                        AIRTUNES_SERVICE_PROPERTIES
                                );
                                jmDNS.registerService(airTunesServiceInfo);
                                Log.d(TAG, "Success to publish service on " + address + ", port: " + getRstpPort());
                            } catch (final Throwable e) {
                                Log.e(TAG, "Failed to publish service on " + address, e);

                            }

                        }
                    }
                }
            }
        } catch (Exception e) {

        }
    }

    public int getRstpPort() {
        return rtspPort;
    }

    private void onAppShutDown() {
        /* Stop all mDNS responders */
        synchronized(jmDNSList) {
            for(final JmDNS jmDNS: jmDNSList) {
                try {
                    jmDNS.unregisterAllServices();
                    Log.i(TAG, "Unregistered all services ");
                }
                catch (final Exception e) {
                    Log.i(TAG, "Failed to unregister some services");

                }
            }
        }
    }
}

  • 5 定义一个Service,启动该线程。在Android设备上运行此程序。在同样wifi下的iOS设备,即可以看到名字叫"RDuwan-AirTunes"的AirTurns服务。
    如下图

    iOS Airplay--Airtunes音乐播放在Android盒子和手机上的实现 (第一篇)_第1张图片

  • 6 第一部分工程代码见Github链接

你可能感兴趣的:(iOS Airplay--Airtunes音乐播放在Android盒子和手机上的实现 (第一篇))