服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

使用索爱v800开发流媒体应用程序

        索尼爱立信的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();
         }
     }    
}

原文请参考:go streaming with v800

扫描关注微信公众号