当前位置: 首页 > >

智能电视TV开发---手机直播视频客户端与智能电视通信

发布时间:

在智能电视TV开发---直播视频客户端结构设计和实现中,我们设计出了简单的客户端,这里这个客户端与智能电视上面的TV服务端进行通信,实现手机控制电视来播放节目。


一、手机端UDP的client实现

? 在智能电视TV开发---客户端和服务器通信中我是使用PC端来做客户端与智能电视TV通信的,现在需要把PC端的代码移植到手机端就可以了,这一步很容易。代码如下:



package com.jwzhangjie.smarttv_client.server.service;

import java.io.IOException;
import java.io.Serializable;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import android.os.AsyncTask;

public class UDPClient implements Serializable{

private static final long serialVersionUID = 1L;
private DatagramSocket udpSocket;
private DatagramPacket dataPacket;
private static final int DEFAULT_PORT = 43701;
private static final int MAX_DATA_PACKET_LENGTH = 40;
private byte[] buffer = new byte[MAX_DATA_PACKET_LENGTH];
private String scret = "jwzhangjie";//验证密钥
private boolean connect = false;

private static class SingleHolder{
private static UDPClient instance = new UDPClient();
}

public static UDPClient getInstance(){
return SingleHolder.instance;
}

/**
* 在串行化和反串行化中
* 阻止产生新的实例,总是返回当前对象
* @return
*/
private Object readResolve(){
return SingleHolder.instance;
}

/**
* 建立通信连接
*/
public boolean startUDP(){
new startBack().execute();
return connect;
}

private class startBack extends AsyncTask{
@Override
protected Void doInBackground(Void... params) {
try {
udpSocket = new DatagramSocket(DEFAULT_PORT );
dataPacket = new DatagramPacket(buffer, MAX_DATA_PACKET_LENGTH);
dataPacket.setPort( DEFAULT_PORT );
InetAddress broadcastAddr;
broadcastAddr = InetAddress.getByName("255.255.255.255");
dataPacket.setAddress(broadcastAddr);
connect = true;
} catch (Exception e) {
e.printStackTrace();
}
if (connect) {
showInfo("初始化成功");
}else {
showInfo("初始化失败");
}
return null;
}

}
/**
* 发送数据
* @param data
*/
public void sendMSG(String params){
new sendBack().execute(params);
}

/**
* 使用线程运行的目的是因为4.0之后不允许网络操作在主线程中
*/
private class sendBack extends AsyncTask{
@Override
protected Void doInBackground(String... params) {
if (connect) {
showInfo(params[0]);
byte[] data = params[0].getBytes();
dataPacket.setData(data);
dataPacket.setLength(data.length);
try {
udpSocket.send(dataPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

}

/**
* 关闭客户端广播
*/
public void closeUDP(){
if (connect) {
if (udpSocket != null && !udpSocket.isClosed() && udpSocket.isConnected()) {
udpSocket.close();
udpSocket = null;
dataPacket = null;
}
showInfo("关闭客户端");
}
}

public void showInfo(String params){
System.out.println(params);
}
}


上面的代码需要主要的地方就是Android系统功能是与权限挂钩,不同版本也是有差异的,就如上面的网络通信在4.0之后就不允许在主线程中运行。


二、手机客户端主界面代码

我们添加了发送广播的代码,相应的在主界面代码进行修改,代码如下:

package com.jwzhangjie.smarttv_client;

import java.util.List;

import com.jwzhangjie.smarttv_client.adapter.LiveVideoAdapter;
import com.jwzhangjie.smarttv_client.model.LiveVideoModel;
import com.jwzhangjie.smarttv_client.server.service.UDPClient;
import com.jwzhangjie.smarttv_client.utils.DBUtils;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Smart_TV_Client extends Activity {

private ListView listView;
private LiveVideoAdapter adapter;
private DBUtils dbUtils;
private List listDatas;
public static Smart_TV_Client instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smart__tv__client);
instance = this;
dbUtils = new DBUtils(this);
initView();
initListener();
UDPClient.getInstance().startUDP();
loadData();
}

@Override
protected void onDestroy() {
UDPClient.getInstance().closeUDP();
super.onDestroy();
}


public void initView(){
adapter = new LiveVideoAdapter(this);
listView = (ListView)findViewById(R.id.listview);
listView.setAdapter(adapter);
}

public void initListener(){
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
LiveVideoModel video = listDatas.get(position);
UDPClient.getInstance().sendMSG(video.getUrl());
}
});
}

public void loadData(){
new loadData().execute();
}

private class loadData extends AsyncTask{
@Override
protected Void doInBackground(Void... params) {
listDatas = dbUtils.getLiveVideoModels();
return null;
}

@Override
protected void onPostExecute(Void result) {
adapter.updateListDatas(listDatas);
super.onPostExecute(result);
}

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.smart__tv__client, menu);
return true;
}
}


到现在实现了手机端能够操作智能电视上TV程序的视频播放,接下来就是抽取里面协议部分,最后会以jar和so的形式提供。



友情链接: