---
title: "J2ME实现RTSP（只有在支持的手机才能用）"
author: "fonter"
date: "2009-07-12T21:09:39Z"
tags: []
slug: "j2me-rtsp"
draft: false
lastmod: "2009-07-26T22:03:36Z"
id: 424415
url: "https://fonter.iteye.com/blog/424415"
---

最近在研究J2ME实现RTSP协议，在索爱开发网站中看到一个类，但只能用于支持RTSP协议的手机，大部分手机需利用J2ME MMAPI实现，而对于自己实现的RTSP，虽然做了一些测试，调通了一点，但还没能真正运行，不知播放的效果如何，会不会像HTTP连接那么烂！

 

```
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;


/**
 * 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.
 *
 */
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://yourServer/mobile/realmp3.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());
                myForm.append("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();
         }
     }     
}
```

 

开发RTSP的相关资料：

Experiments in Streaming Content in Java ME(1) [http://fonter.iteye.com/blog/425372](/blog/425372)  
Experiments in Streaming Content in Java ME(2) [http://fonter.iteye.com/blog/425392](/blog/425392)  
Experiments in Streaming Content in Java ME(3) [http://fonter.iteye.com/blog/425427](/blog/425427)

 

 

keyRepeated和keyPressed处理 [http://fonter.iteye.com/blog/433408](http://fonter.iteye.com/blog/433408) 

少用System.out.println()吗？[http://fonter.iteye.com/blog/423871](/blog/423871)  
读取流最快方式 [http://fonter.iteye.com/blog/422412](/blog/422412)  
java简单解析docx、pptx、xlsx文档 [http://fonter.iteye.com/blog/420319](/blog/420319)  
Log4j在Java WebApp的配置 [http://fonter.iteye.com/blog/418570](/blog/418570)  
J2ME to android之学习笔记 [http://fonter.iteye.com/blog/416112](/blog/416112)  
安装Jar提示“jar文件无效”的另一个奇怪原因 [http://fonter.iteye.com/blog/414188](/blog/414188)  
J2ME代码认证证书的支持情况 [http://fonter.iteye.com/blog/413357](/blog/413357)  
S40平台播放多媒体时内存优化 [http://fonter.iteye.com/blog/413022](/blog/413022)  
SUN的J2ME源代码下载 [http://fonter.iteye.com/blog/412094](/blog/412094)