索尼爱立信的v800手机可以支持流媒体的播放,使用非常简单。如果你曾经使用mmapi播放过声音或者做过照相应用的话,那么上手非常快。
你要做的就是根据指定的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 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());
}
}
我们需要注意的是进行连接服务器的时候必须在单独线程中处理,而不能在主线程。因为这样会堵塞系统。接下来你要做的就是构建一个支持rtsp的流媒体服务器,放置一个3gp格式的文件在服务器上。下面的代码演示了如何使用v800开发流媒体应用。
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;
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();
}
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器