Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve a Bonjour domain name on Android?

I need to get my app to play a video file located on my network. I know the url of the file is:

http://something.local/abc.mp4

Now, when I manually substitute "something.local" with its true ip address, the MediaPlayer has no problem playing it. Nonetheless, when I have the above address, the MediaPlayer errors out with error (1, -1007).

So I'm assuming this is because Android doesn't understand "something.local" as being correct.

My question is: How can I "translate" something.local into an ip myself, so that I can then pass it into MediaPlayer?

A small caveat: I believe that MediaPlayer does not work with IPv6 addresses, so please keep that in mind...


Just a side note, in case it makes my situation clearer: When I run ping something.local -4 in the Windows command prompt, it returns:

Pinging something.local [192.168.1.126] with 32 bytes of data:
Reply from 192.168.1.126: bytes=32 time=145ms TTL=64
Reply from 192.168.1.126: bytes=32 time=112ms TTL=64
Reply from 192.168.1.126: bytes=32 time=32ms TTL=64
Reply from 192.168.1.126: bytes=32 time=169ms TTL=64

That translation where windows went from something.local -> 192.168.1.126 is what I want to do in my Android app.

like image 336
yydl Avatar asked Dec 12 '11 06:12

yydl


People also ask

What is Bonjour in Android?

Bonjour, also known as zero-configuration networking, enables automatic discovery of devices and services on a local network using industry standard IP protocols.

How does Bonjour work?

Bonjour uses multicast DNS (mDNS) and link-local addressing to allow systems to assign and recognize IP addresses without automated dynamic host configuration protocols (DHCP). It can work with both IPv6 and IPv4 addressing and uses the native local-link addressing support on Windows.


2 Answers

Firstly, you need read document about Bonjour (iOS term) or Zero Config (Linux term).

To understand what's something.local:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NetServices/Articles/about.html#//apple_ref/doc/uid/TP40002458-SW1

For example, if a user types steve.local. into a Web browser, this tells the system to multicast the request for steve on the local network instead of sending it to the conventional DNS server. If a Bonjour-enabled computer named steve is on the local network, the user’s browser is sent the correct IP address for it. This allows users to access local hosts and services without a conventional DNS server.

For how to resolve it:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NetServices/Articles/NetServicesArchitecture.html#//apple_ref/doc/uid/20001074-SW1

For java library, previous answers provided good enough example.

like image 96
pinxue Avatar answered Nov 09 '22 03:11

pinxue


You should try this snippet with jmDNS library api.. may need some changes.

JmDNS jmdns =  JmDNS.create();

DNSEntry addressEntry = jmdns.getCache().getDNSEntry(name, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_ANY);
 if (addressEntry instanceof DNSRecord) {
      ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(true);
      if (cachedAddressInfo != null) {
      for (Inet4Address address : cachedAddressInfo.getInet4Addresses()) {
          //  use the `address`
      }
 }
like image 3
Ronnie Avatar answered Nov 09 '22 02:11

Ronnie