I want a simple code to play video from rtmp stream for my android application project

18,228

Solution 1

rtmp live stream can be play in Android webview embedding flash player. Give below is full source code to play rtmp.

Though you must go through this url http://www.adobe.com/devnet-apps/flashruntimes/certified-devices.html here the flash supported device is listed. So test the code in such a device, which supports the flash.

public class WebViewPlayer extends Activity {

    WebView webView;

    Utils utils;
    String bodyHtml;
    String rtmpUrl;
    String fileName;
    String htmlVideoEmbedCode ;

    String htmlPost = "</body></html>";
    String htmlPre = "<!DOCTYPE html>" 
            + "<html lang=\"en\">"
            + "<head><meta charset=\"utf-8\">" 
            + "</head>"
            + "<body style='margin:0; pading:0;"
            + " background-color: #71D5CA;'>";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.flash_player);
        RegisterActivities.registerActivity(this);

        utils = new Utils(this);

        if(! utils.isConnectionPossible()){

            utils.getMessageDialogBox("Alert", "The Device cannot connect to the internet."
                    + "Please make sure internet is available.", "OK",
            true)
            .show();

        }

        rtmpUrl = YOUR_RTMP_URL);
        fileName = YOUR_FILE_NAME);

        htmlVideoEmbedCode = getVideoEmbedCode();

        webView = (WebView) findViewById(R.id.webView1);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setPluginsEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setAppCacheEnabled(true);

        webView.setWebChromeClient(new WebChromeClient(){

             public void onProgressChanged(WebView view, int progress) {
                 if(progress == 100)
                     ((ProgressBar)findViewById(R.id.progressBarWebView))
                     .setVisibility(View.INVISIBLE);
             }
        });

        refreshFileName();
    }

    @Override
    protected void onResume() {
        super.onResume();
        webView.refreshPlugins(true);
    }

    private String getVideoEmbedCode() {
        return "<embed " 
                + "type=\"application/x-shockwave-flash\""
                + "id=\"player1\" " + "name=\"player1\" "
                + "src=\"http://www.c-span.org/"
                + "cspanVideoHD.swf\""
                + "width=\""+utils.getDisplayWidth()+"\""
                + "height=\""+(utils.getDisplayHeight()+40)+"\"" + " flashvars=@FILESRC@"
                + "allowfullscreen=\"true\"" 
                + "allowscripaccess=\"always\""
                + "/>";
    }

    private void refreshFileName() {

        if (fileName.endsWith(".flv")) {
            fileName = "flv:" + fileName;
        }

        bodyHtml = htmlVideoEmbedCode ;
        bodyHtml = bodyHtml.replaceAll("@FILESRC@", 
                "\"file=" + fileName
                + "&streamer=" + rtmpUrl + "\"");
        webView.loadDataWithBaseURL("http://127.0.0.1",
                htmlPre + bodyHtml
                + htmlPost, "text/html", "UTF-8", null);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        webView.stopLoading();
        webView.destroy();
//      webView = null;
    }

}

Solution 2

using RTSP

 public class PlayVideoRemote extends Activity
 {
private VideoView vView;
private String vSource;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{ 
    //sets the Bundle
    super.onCreate(savedInstanceState);
    //sets the context 
    setContentView(R.layout.main);

    //get the VideoView from the layout file
    vView = (VideoView)findViewById(R.id.vview);

    //use this to get touch events
    vView.requestFocus();

    //loads video from remote server
    //set the video path
    vSource ="rtsp://v6.cache1.c.youtube.com/CjYLENy73wIaLQnF4qJzpSt4nhMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYMDFmvL1wMysTQw=/0/0/0/video.3gp";
    //set the video URI, passing the vSource as a URI
    vView.setVideoURI(Uri.parse(vSource));

    //enable this if you want to enable video controllers, such as pause and forward
    vView.setMediaController(new MediaController(this));

    //plays the movie
    vView.start();
}
}

for RTMP refer this Convert video Input Stream to RTMP

Share:
18,228
R_Hear
Author by

R_Hear

I am a student of Khon Kaen University

Updated on June 05, 2022

Comments

  • R_Hear
    R_Hear almost 2 years

    I have project about Video on Demand via Android TV Box. I have problem,I can not find open source of RTMP player. Had someone can help me or guide me about the rtmp player source code ?

    I use red5 to stream and buil for android 2.2(froyo).

  • R_Hear
    R_Hear over 11 years
    Thank you very much but I get error on -Utils utils; -RegisterActivities.registerActivity(this); - + "width=\""+utils.getDisplayWidth()+"\"" + "height=\""+(utils.getDisplayHeight()+40)+"\"" + " flashvars=@FILESRC@" error on utils how I can fig it.
  • R_Hear
    R_Hear over 11 years
    thank you. but it can not play video. I use red5 to stream, I can straem only on rtmp.
  • Sudipta Som
    Sudipta Som over 11 years
    utils is a object of a user defined class, where getDisplayHeight()/Width() are defined...so u just ignore those methods..u can put any height or width instead..
  • R_Hear
    R_Hear over 11 years
    Thank you very much. ^_^ @SudiptaforAndroid
  • Jovan
    Jovan over 11 years
    How can i play a live rtmp stream with this? I tried every combination, but it just plays something from C-span... Can anybody help me? :)
  • SweetWisher ツ
    SweetWisher ツ over 10 years
    What is Utils here?? RegisterActivities abd Utils give me error.. Do I need some external libs??
  • SweetWisher ツ
    SweetWisher ツ over 10 years
    Some error points: webView.getSettings().setPluginsEnabled(true); R.id.progressBarWebView) webView.refreshPlugins(true);
  • SweetWisher ツ
    SweetWisher ツ over 10 years
    Do I need to change webView.loadDataWithBaseURL("127.0.0.1", htmlPre + bodyHtml + htmlPost, "text/html", "UTF-8", null);?
  • dsharew
    dsharew over 9 years
    The is about using rtmp not rtsp
  • Saty
    Saty over 8 years
    @SudiptaforAndroid...Is that code working now cause I am getting a "Couldn't load plug in"