📄 threadgetvideo.java
字号:
/**
//MobileTrafficCamViewer文档生成日期:2005.10.24
//
//(1)概述:
//类名称:ThreadGetVideo
//类说明:
// 提供告知服务器生成交通实时录像功能,并下载该录像并播放
*
//所在子系统:MobileTrafficCamViewer
//
//系统总描述:
我们提供的Mobile TrafficCam Viewer J2ME版本 就是这么一种概念:
一个可以下载到手机(例如Nokia7610已经确实可以下载安装并运行)的Java应用程序。
每次选择它的“Download&Play”命令,它就会向我们的服务器发起请求(通过GPRS),
然后下载服务器准备好的3gp格式数据,并播放它。你还可以选择Replay刚才下载的交通路况录像。
子系统描述:
下面介绍Mobile TrafficCam Viewer的功能列表:
TrafficView
Download&Play
StopDownload&Play
Settings
About
Exit
//(2)历史记录:
//创建人: 郑昀(2005.10.24)
//联系我: Google Talk >> zhengyun@gmail.com
//Blogs: http://blog.csdn.net/zhengyun_ustc/以及http://www.cnblogs.com/zhengyun_ustc
//(3)版权声明:
//我这个版本的Mobile TrafficCam Viewer,代码您可以借鉴,但不得用于商业用途,除非得到本人的授权。
//(4)相关资源:
1:《[J2ME]手机看交通监视器实时录像 实现说明》
3:下载源代码:
////////////////////////////////////////////////////////////////////*/
package com.ultrapower.model;
import java.io.InputStream;
import java.util.Hashtable;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Item;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import org.kobjects.serialization.KvmSerializable;
import org.kobjects.serialization.PropertyInfo;
import org.ksoap.ClassMap;
import org.ksoap.SoapEnvelope;
import org.ksoap.SoapObject;
import org.ksoap.transport.HttpTransport;
import org.kxml.PrefixMap;
import com.ultrapower.common.CommandResources;
import com.ultrapower.control.GUIController;
/**********************************************************
//ThreadGetVideo
//
//Class Description:
// 提供告知服务器生成交通实时录像功能,并下载该录像并播放
//
//Author:
// zhengyun@ultrapower 2005.10。24
//
**********************************************************/
public class ThreadGetVideo
implements Runnable {
private GUIController m_controller = null;
private VideoControl m_video = null;
private Player m_player = null;
private boolean m_bDownloadVideo;
private VideoSettings m_Settings = VideoSettings.getInstance();
private InputStream m_isInputMedia;
private Item m_itemVideoWindow;
// 保留当前VideoControl的提示资源的ID号:
private int m_nItemVideoControlId = -1;
public ThreadGetVideo(GUIController controll)
{
System.out.println("/** Enter ThreadPostVideo Constructor!");
m_controller = controll;
m_bDownloadVideo = false;
}
public synchronized void setDownloadVideo(boolean value)
{
m_bDownloadVideo = value;
}
public final void run()
{
/* Use networking if necessary */
long lngStart;
long lngTimeTaken;
String sProgressWAITTITLE = String.valueOf(
CommandResources.getChars(CommandResources.TXT_WAITTITLE));
while(true)
{
try
{
// 我们是依靠m_bDownloadVideo来判断是否去得到3gp并下载的.
// 如果当前m_bDownloadVideo是false,那么本线程就只能先睡眠一段时间了.
if( m_bDownloadVideo )
{
System.out.println("/* StartWebcamDownload::开始下载实况录像!");
/*
* Change to run locally on developer machine
*/
String serviceNamespace =
String.valueOf(m_controller.getSettings().getMyConvertServerURL());
//String.valueOf(
// CommandResources.getChars(
// CommandResources.TXT_MY_3GP_SERVER_URL));
String methodName =
String.valueOf(
CommandResources.getChars(
CommandResources.TXT_MY_3GP_SERVER_METHOD));
String methodReturnName =
String.valueOf(
CommandResources.getChars(
CommandResources.TXT_MY_3GP_SERVER_METHOD_RETURN));
String methodResponseName =
String.valueOf(
CommandResources.getChars(
CommandResources.TXT_MY_3GP_SERVER_METHOD_RESPONSE));
String downloadSiteURL =
String.valueOf(m_controller.getSettings().getDownload3gpFileSiteURL());
//String.valueOf(
// CommandResources.getChars(
// CommandResources.TXT_DOWNLOAD3GPFILE_SERVER_URL));
String serviceUrl =
serviceNamespace + "?wsdl";
System.out.println("Try add ClassMap.....");
ClassMap classMap = new ClassMap(true);
classMap.prefixMap = new PrefixMap( classMap.prefixMap, "ns",
serviceNamespace );
//classMap.prefixMap = Soap.prefixMap[2];
System.out.println("/* Try new SoapObject */\n" +
" serviceNamespace:" + serviceNamespace);
SoapObject request = new SoapObject(serviceNamespace, methodName );
//SoapEnvelope envelope = new SoapEnvelope (classMap);
//envelope.setBody (request);
//SoapObject request = new SoapObject("urn:xmethods-Streaming", methodName );
//////////////////////////////////////
// ksoap2!
//SoapSerializationEnvelope envelope =
// new SoapSerializationEnvelope(SoapEnvelope.VER11);
//envelope.bodyOut = request;
////////////////////////////////////////
/*envelope.addMapping(serviceNamespace,
methodResponseName,
PropertyInfo.VECTOR_CLASS);*/
//(new MarshalBase64()).register(envelope);
/*envelope.addMapping(
"http://xml.apache.org/xml-soap",
"Vector",
PropertyInfo.VECTOR_CLASS);
envelope.addMapping(
"http://schemas.xmlsoap.org/soap/encoding/",
"string",
PropertyInfo.STRING_CLASS);
envelope.addMapping(
"http://schemas.xmlsoap.org/soap/encoding/",
"int",
PropertyInfo.INTEGER_CLASS);*/
/*
* 添加 web service的输入参数
*/
String strMMSURL =
String.valueOf(m_controller.getSettings().getMMSServerURL());
//String.valueOf(
// CommandResources.getChars(
// CommandResources.TXT_URL_MMS_SERVER_1));
request.addProperty("url",
strMMSURL);
System.out.println("/* 流媒体服务器:" + strMMSURL);
String strTimeLimit =
String.valueOf(m_controller.getSettings().getLiveMediaTimeLimit());
//String.valueOf(VideoSettings.getInstance().getLiveMediaTimeLimit());
request.addProperty("timeVideo",
strTimeLimit);
System.out.println("/* 时间秒数:" + strTimeLimit);
String str3gpFileName =
String.valueOf(
BuildLongFromString(
GetCID() + "thisisasalt1234")) + ".3gp";
request.addProperty("FileName",
str3gpFileName);
System.out.println("/* 文件名:" + str3gpFileName);
System.out.println("/* Try new HttpTransport \n" +
" serviceUrl:" + serviceUrl + " ;method:" + methodName);
HttpTransport tx = new HttpTransport(serviceUrl, methodName);
tx.setClassMap( classMap );
tx.debug = true;
//////////////////////////////////////
// ksoap2!
/*HttpTransport tx =
new HttpTransport(serviceUrl);
tx.debug = true;*/
////////////////////////////////////////////
// 告诉控制器,开始生成数据了
m_controller.handleEventNoThrows(GUIController.EventID.EVENT_BUILDDATAING,
null);
////////////////////////////////////////////
System.out.println("Try call Streaming web service.request dump>>"
+ tx.requestDump);
//tx.call(serviceNamespace + "#" + methodName,
// envelope);
Object Response = (Object)tx.call(request);
System.out.println("End call Streaming web service.response dump>>"
+ tx.responseDump);
//KvmSerializable o3gpResponse = (KvmSerializable)envelope.bodyIn;
SoapObject o3gpResponse = (SoapObject)Response;
if(o3gpResponse != null)
{
PropertyInfo info = new PropertyInfo();
Hashtable properties = new Hashtable();
//String str3gpResponse;
int cnt = o3gpResponse.getPropertyCount();
for (int i = 0; i < cnt; i++) {
//o3gpResponse.getPropertyInfo(i, properties, info);
o3gpResponse.getPropertyInfo(i, info);
System.out.println("info name:" + info.name);
if(methodReturnName.equalsIgnoreCase(info.name))
{
System.out.println("这正是我们要找的返回值!" );
//str3gpResponse = (String)o3gpResponse.getProperty(i);
//System.out.println("服务器告诉的URL为:" + str3gpResponse.getStringAt(0));
}
}
}
////////////////////////////////////////////
// 告诉控制器,开始下载数据了
m_controller.handleEventNoThrows(GUIController.EventID.EVENT_DOWNLOADING,
null);
////////////////////////////////////////////
downloadSiteURL = downloadSiteURL + str3gpFileName;
System.out.println("服务器告诉的URL为:" + downloadSiteURL);
HttpConnection conn =
(HttpConnection)Connector.open(downloadSiteURL);
conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("Content-Length", "0");
conn.setRequestProperty("Accept-Language", "zh-cn");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
conn.setRequestProperty("Connection", "close");
System.out.println("1");
m_isInputMedia =
(InputStream)conn.openInputStream();
if(m_isInputMedia != null)
{
////////////////////////////////////////////
// 告诉控制器,成功下载数据了
m_controller.handleEventNoThrows(GUIController.EventID.EVENT_DOWNLOADDATA_SUCC,
null);
////////////////////////////////////////////
System.out.println("2");
m_player = (Player)Manager.createPlayer(m_isInputMedia, "video/3gpp");
m_player.realize();
System.out.println("3");
m_video = (VideoControl)m_player.getControl("VideoControl");
System.out.println("4");
if(m_video != null)
{
// the player can start with the smallest latency
m_player.prefetch();
System.out.println("/** got VideoControl!");
// Display Video as a Form item
//
// 在USE_GUI_PRIMITIVE模式下,默认是可见的,而在USE_DIRECT_VIDEO模式下,
// 默认是不可见的,需要通过方法setVisible(boolean visible)来设置
(m_itemVideoWindow = (Item)m_video.initDisplayMode
(VideoControl.USE_GUI_PRIMITIVE, null)).setLayout(3);
System.out.println("/** initDisplayMode!");
//if(m_nItemVideoControlId < 0)
{
m_nItemVideoControlId = m_controller.m_camForm.append(m_itemVideoWindow);
}
//else
//{
// 这种set()的做法会导致java.lang.IndexOutOfBoundsException异常!!
// System.out.println("/** item video control already exist!");
// m_controller.m_camForm.delete(m_nItemVideoControlId);
// m_nItemVideoControlId = m_controller.m_camForm.append(m_itemVideoWindow);
//}
// 一定要在播放之前将屏幕上方的提示信息删除,要不然会导致播放的视频不能全屏,要
// 滚动才能看。但是一滚动,由于没有刷新功能,导致视频消失!
System.out.println("/** deleteTips!");
m_controller.m_camForm.deleteTips();
m_player.start();
System.out.println("/** start Player!");
}
}
else
{
////////////////////////////////////////////
// 告诉控制器,无法下载数据了
m_controller.setPlayerException("远端服务器没有正常响应!");
m_controller.handleEventNoThrows(GUIController.EventID.EVENT_DOWNLOADDATA_ERROR,
null);
////////////////////////////////////////////
}
m_bDownloadVideo = false;
}
lngStart = System.currentTimeMillis();
lngTimeTaken = System.currentTimeMillis() - lngStart;
if(lngTimeTaken < 100)
Thread.sleep(75 - lngTimeTaken);
}
catch(Exception exc)
{
System.out.println("/** 下载实况录像时发生异常!");
m_bDownloadVideo = false;
exc.printStackTrace();
////////////////////////////////////////////
// 告诉控制器,不能够下载录像并播放
m_controller.setPlayerException(exc.getMessage()
+ "/" + exc.getClass());
m_controller.handleEventNoThrows(GUIController.EventID.EVENT_DOWNLOADDATA_ERROR,
null);
////////////////////////////////////////////
}
}
}
private String GetCID()
{
return "cid" + System.currentTimeMillis();
}
private long BuildLongFromString(String strMagic)
{
int j = 0;
for(int k = 0; k < strMagic.length(); k++)
if((k & 0x1) == 0)
j ^= j << 7 ^ strMagic.charAt(k) ^ j >> 3;
else
j ^= ~(j << 11 ^ strMagic.charAt(k) ^ j >> 5);
return (long)(j & 0x7fffffff);
}
public synchronized void PlayAgain()
{
System.out.println("/** PlayAgain!");
try
{
if(m_player != null)
{
//if(m_player.getState() == m_player.CLOSED)
{
m_player.start();
}
}
}
catch(Exception exc)
{
}
finally
{
}
}
public synchronized void StopPlayer()
{
System.out.println("/** StopPlayer!");
try
{
if(m_player != null)
{
//if(m_player.getState() == m_player.STARTED)
{
m_player.stop();
}
}
}
catch(Exception exc)
{
}
finally
{
}
}
public synchronized void DeleteVideoControl()
{
System.out.println("/** DeleteVideoControl!");
try
{
if(m_player != null)
{
//if(m_player.getState() == m_player.STARTED)
{
m_player.stop();
}
m_player.deallocate();
m_player = null;
}
// 将VideoControlItem删除
if(m_nItemVideoControlId > 0)
m_controller.m_camForm.delete(m_nItemVideoControlId);
}
catch(Exception exc)
{
}
finally
{
if(m_video != null)
{
m_video.setVisible(false);
m_video = null;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -