扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
你要做的就是根据指定的URL来创建一个Player,然后启动这个Player。URL的格式如下:rtsp://MyServer/myVideo.3gp。下面的代码例子演示了如何使用:
private void startStreaming(){
try{
myPlayer = Manager.createPlayer("rtsp://MyServer/MyFile.3gp");
myPlayer.addPlayerListener(this);
myPlayer.realize();
// Grab the video control and set
vc = (VideoControl)myPlayer.getControl("VideoControl");
if (vc != null) {
myForm.append((Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null));
// sets the display size of the video.
vc.setDisplaySize(120,160);
}
myPlayer.start();
}catch(Exception e){
log("Exception: " + e.toString());
}
}
我们需要注意的是进行连接服务器的时候必须在单独线程中处理,而不能在主线程。因为这样会堵塞系统。接下来你要做的就是构建一个支持RTSP的
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import
import javax.microedition.lcdui.game.*;
/**
* A simple example of the MMAPI (JSR 135) support for Streaming Video
* with the Sony Ericsson V800.
*
* This code is part of the Tips & Tricks section at
* www.SonyEricsson.com/developer
*
* COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2005.
* The software is the copyrighted work of Sony Ericsson Mobile Communications AB.
* The use of the software is subject to the terms of the end-user license
* agreement which accompanies or is included with the software. The software is
* provided "as is" and Sony Ericsson specifically disclaim any warranty or
* condition whatsoever regarding merchantability or fitness for a specific
* purpose, title or non-infringement. No warranty of any kind is made in
* relation to the condition, suitability, availability, accuracy, reliability,
* merchantability and/or non-infringement of the software provided herein.
*
* Written by Jöns Weimarck, January 2005
*/
public class StreamingVideo extends MIDlet implements CommandListener, PlayerListener, Runnable{
private Display myDisplay;
private Form myForm;
private Thread streamingThread;
private Player myPlayer;
private VideoControl vc;
private boolean running=false;
public StreamingVideo() {
myDisplay = Display.getDisplay(this);
myForm=new Form ("Streaming Test");
myForm.addCommand(new Command("Exit", Command.EXIT,0));
myForm.addCommand(new Command("Start", Command.OK,0));
myForm.setCommandListener(this);
}
protected void startApp() throws MIDletStateChangeException {
myDisplay.setCurrent(myForm);
streamingThread = new Thread(this);
}
protected void pauseApp() {}
protected void destroyApp(boolean unconditional) {
try {
myPlayer.stop();
myPlayer.close();
}
catch( Exception e ) {
log("Exception: " + e.toString());
}
}
/**
* Inits and starts the Player for Video Streaming
*/
private void startStreaming(){
try{
myPlayer = Manager.createPlayer("rtsp://MyServer/MyFile.3gp");
myPlayer.addPlayerListener(this);
myPlayer.realize();
// Grab the video control and set it to the current display.
vc = (VideoControl)myPlayer.getControl("VideoControl");
if (vc != null) {
myForm.append((Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null));
// sets the display size of the video.
vc.setDisplaySize(120,160);
}
myPlayer.start();
}catch(Exception e){
log("Exception: " + e.toString());
}
}
public void commandAction(Command c, Displayable s){
if(c.getCommandType()==Command.EXIT){
running=false;
notifyDestroyed();
}else{
streamingThread.start();
}
}
/**
* PlayerListener Interface method, logs all player event.
*/
public void playerUpdate(Player player, String event, Object eventData){
log(" ** playerUpdate: " + event + " **");
}
public void log(String msg){
System.out.println(msg);
}
public void run() {
running=true;
startStreaming();
while(running){
Thread.yield();
}
}
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者