I have local multicast stream. Video is in MPEG4. I have Ip address of host (HOST) and port number on which I can get multicast stream (PORT). In order to get content I should connect and send multicast join request to get content.
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
public class MainActivity extends Activity {
private static final String HOST = "192.168.1.1";
private static final int PORT = 1234;
int port;
InetAddress address;
DatagramSocket socket = null;
DatagramPacket packet;
byte[] sendBuf = new byte[256];
private VideoView mVideoView;
private MediaController mMediaController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifi != null) {
WifiManager.MulticastLock lock = wifi.createMulticastLock("mylock");
lock.acquire();
}
mVideoView = (VideoView) findViewById(R.id.video);
mMediaController = new MediaController(this);
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// optional need Vitamio 4.0
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
}
@Override
protected void onResume() {
super.onResume();
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
NetworkInterface eth0 = null;
while (enumeration.hasMoreElements()) {
eth0 = enumeration.nextElement();
if (eth0.getName().equals("eth0")) {
// there is probably a better way to find ethernet
// interface
break;
}
}
InetAddress group = InetAddress.getByName(HOST);
MulticastSocket s = new MulticastSocket(PORT);
s.setReuseAddress(true);
s.setTimeToLive(1);
s.setSoTimeout(10000);
s.joinGroup(new InetSocketAddress(group, PORT), eth0);
Log.log("JOINED GROUP");
byte[] msg = {
'H', 'e', 'l', 'l', 'o'
};
DatagramPacket hi = new DatagramPacket(msg, msg.length, group, TVP_HD_PORT);
s.send(hi);
Log.log("SENT HI TO GROUP")
mVideoView.setVideoURI(Uri.parse("udp://" + HOST + ":" + PORT));
mVideoView.setMediaController(mMediaController);
mVideoView.requestFocus();
} catch (SocketException e) {
Log.log("FAIL");
e.printStackTrace();
} catch (UnknownHostException e) {
Log.log("FAIL");
e.printStackTrace();
} catch (IOException e) {
Log.log("FAIL");
e.printStackTrace();
}
return null;
}
}.execute();
}
}
}
I had some issues before s some of the code base on posts from other posts etc. I don't get any error from Vitamio, but I don't get any picture either. My Android device has ethernet socket thats why I selectd eth0 device (again, selection of device is based on some other post, I couldn't connect without it). Maybe someone tried using Vitamio for this? On many threads I found answer of one person that Vitamio can play udp streams, but never menion how, I used sample code from Vitamio library src code. Without luck.
Ok, so I figured it out.
First of all I don't need to join the group before setVideoUri
in VideoView
. When using Vitamio whole communication needed by udp multicast (join group, leave group and confirm that you are still listening) is implemented.
Second thing is a link. I used udp://HOST:PORT
but it should be udp://@HOST:PORT
(empty user). If you will try to test your udp stream in VLC then you should use link with @
too in order to play it.