首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我想要一个简单的代码来播放我的android应用程序项目从rtmp流视频

我想要一个简单的代码来播放我的android应用程序项目从rtmp流视频
EN

Stack Overflow用户
提问于 2012-09-12 19:15:32
回答 2查看 18.1K关注 0票数 3

我有关于通过Android电视盒的视频点播项目。我有问题,我找不到开源的RTMP播放器。有没有人可以帮助我或指导我关于rtmp播放器的源代码?

我使用red5来流式传输和构建Android2.2(Froyo)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-12 20:22:03

rtmp live流可以在Android webview嵌入flash播放器中播放。下面是播放rtmp的完整源代码。

虽然你必须通过这个url http://www.adobe.com/devnet-apps/flashruntimes/certified-devices.html在这里列出了闪存支持的设备。因此,在这样一个支持闪存的设备中测试代码。

代码语言:javascript
运行
复制
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;
    }

}
票数 0
EN

Stack Overflow用户

发布于 2012-09-12 19:40:21

使用RTSP

代码语言:javascript
运行
复制
 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();
}
}

对于RTMP,请参阅此Convert video Input Stream to RTMP

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12386892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档