我有本地多播流。视频格式为 MPEG4。我有主机 (HOST) 的 IP 地址和端口号,我可以在其上获取多播流 (PORT)。为了获取内容,我应该连接并发送多播加入请求以获取内容。
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();
}
}
}
在其他帖子的帖子等的一些代码库之前,我遇到了一些问题。我没有从 Vitamio 收到任何错误,但我也没有得到任何图片。我的 Android 设备有以太网套接字,这就是我选择 eth0 设备的原因(同样,设备的选择基于其他一些帖子,没有它我无法连接)。也许有人尝试为此使用 Vitamio?在许多线程上,我发现一个人回答说 Vitamio 可以播放 udp 流,但从未提及如何播放,我使用了来自 Vitamio 库 src 代码的示例代码。没有运气。