How does Android broadcast RTMP streams?

Source: Internet
Author: User

How does Android broadcast RTMP streams?
On android, live video/audio streams are a small part of attention. Every Time we discuss streaming media, RTMP (Real Time Messaging Protocol) is indispensable. RTMP is a basic live video/audio stream protocol, but unfortunately Android-standard videoView does not support RTMP playback. Therefore, to play RTMP live streams on android, you must use a library that supports the RTMP protocol. In this tutorial, we will discuss how to use the [Vitamio] library of Android to play streaming media transmitted by the RTMP protocol. Android Vitamio Library

Vitamio is an open-source project based on FFmpeg on android and ios. Vitamio provides us with a clean, simple, comprehensive, and real hardware decoder and Renderer API. Vitamio is a service that supports multiple audio/video formats, such as FLV, TS/TP, WMV, and DivX, xvid and other powerful libraries in a variety of standard formats. In addition, subtitle embedding and external subtitle playing are also supported. MKV and. srt embedding. However, it carries a license, so you must first obtain authentication before using it. In this android RTMP example, we will not only discuss RTMP live streams, but also m3u8 streams (HLS), RTSP streams, and MMS (Microsoft Media Stream ). First, let us reference the Vitamio library in our project.

Follow these steps to reference the Vitamio library in Android Studio:
  1. Download Vitamio bundle
  2. Decompress the package and choose File> Import Module on Android Studio.
  3. Specify the VitamioBundle path, select the vitamio folder, and click Finish.
  4. Add a dependency Project (': vitamio') in the dependency section of build. gradle (Module: app ')
  5. Open build. gradle (Module: vitamio)-change the minimum sdk version to 7
  6. Do not forget to add internet permissions in manifest. xml
  7. Done! Android RTMP stream

    Before talking about how to use it, let's take a look at RTMP. Real Time Messaging Protocol (RTMP) is a Protocol owned by Adobe Systems. This protocol is a flash player developed by Adobe for audio and video streams. Later, part of the agreement was made public for public use. For more information, see here. This protocol is mostly used for IPTV and real-time on-demand video streaming, but it is also used for other applications.

    On android, standard VideoView does not support RTMP playback. However, WebView can play RTMP streams. This solves the problem of playing RTMP streams, but I think web apps cannot provide a good interface and experience. Therefore, in this android RTMP example, we will use the third-party library-Vitamio live RTMP stream streaming media. After you reference Vitamio in the project, add the VideoView of Vitamio in your layout file:

    Activity_main.xml

            <LINEARLAYOUT android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">        <IO.VOV.VITAMIO.WIDGET.VIDEOVIEW android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/vitamio_videoView" />    </LINEARLAYOUT>

    In addition, please write your activity as follows:

    MainActivity. java

        package com.truiton.rtmpplayer;    import android.net.Uri;    import android.os.Bundle;    import android.support.v7.app.ActionBarActivity;    import java.util.HashMap;    import io.vov.vitamio.LibsChecker;    import io.vov.vitamio.MediaPlayer;    import io.vov.vitamio.widget.MediaController;    import io.vov.vitamio.widget.VideoView;    public class MainActivity extends ActionBarActivity {        private static final String TAG = "MainActivity";        private String path;        //private HashMap
       
         options;        private VideoView mVideoView;        @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            if (!LibsChecker.checkVitamioLibs(this))                return;            setContentView(R.layout.activity_main);            mVideoView = (VideoView) findViewById(R.id.vitamio_videoView);            path = "rtmp://rrbalancer.broadcast.tneg.de:1935/pw/ruk/ruk";            /*options = new HashMap<>();            options.put("rtmp_playpath", "");            options.put("rtmp_swfurl", "");            options.put("rtmp_live", "1");            options.put("rtmp_pageurl", "");*/            mVideoView.setVideoPath(path);            //mVideoView.setVideoURI(Uri.parse(path), options);            mVideoView.setMediaController(new MediaController(this));            mVideoView.requestFocus();            mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {                @Override                public void onPrepared(MediaPlayer mediaPlayer) {                    mediaPlayer.setPlaybackSpeed(1.0f);                }            });        }    }
       

    Although the above Code is clear and clear, it should be noted that you should modify the path of your RTMP stream. On android, RTMP streams may be played using a header path. Fortunately, the Vitamio RTMP player also supports this method. Therefore, the Vitamio library can be used for all types of RTMP streams. The above example will look like this:

    Android RTSP Streaming Media

    The real-time stream protocol (RTSP) transmits content through a multimedia server. For example, YouTube uses the RTSP stream to publish content. The easy part of RTSP stream is that it can be completed through android-standard VideoView. For more information, see my VideoView example.

    However, if you use the Vitamio library, you can play RTSP streams better. In fact, Vitamio also supports RTSP stream playback. The above process is the same, including the VideoView of Vitamio in the layout file, and the RTSP url specified by the PATH variable

        mVideoView = (VideoView) findViewById(R.id.vitamio_videoView);    path = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov";    mVideoView.setVideoPath(path);    mVideoView.setMediaController(new MediaController(this));    mVideoView.requestFocus();    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {        @Override        public void onPrepared(MediaPlayer mediaPlayer) {            mediaPlayer.setPlaybackSpeed(1.0f);        }    });
    Android m3u8 Streaming Media

    "Playing m3u8 videos on android" is one of the most common problems for android Developers. The simplest way to live video streams over Http is to use standard VideoView, but m3u8 streams can only be played on devices above android. Because HTTP/HTTPS live broadcast and HTTP/HTTPS progressive streaming protocols are introduced in Android 3.0, HTTPS is fully supported in android3.1.

    If you want to implement HTTP Real-Time Streaming (HLS) that supports android m3u8 streams in earlier versions ). You should consider using the Vitamio library, which supports playing m3u8 videos over android API7. In the same way, use the VideoView of Vitamio in the layout file and specify the HTTP live streaming URL.

        mVideoView = (VideoView) findViewById(R.id.vitamio_videoView);    path = "http://93.184.221.133/00573D/236/236-0.m3u8";    mVideoView.setVideoPath(path);    mVideoView.setMediaController(new MediaController(this));    mVideoView.requestFocus();    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {        @Override        public void onPrepared(MediaPlayer mediaPlayer) {            mediaPlayer.setPlaybackSpeed(1.0f);        }    });

    Playing m3u8 stream on Android with Vitamio wowould look something like this:
    The following figure shows how to use Vitamio to play m3u8 streams on androi:

    Android MMS stream

    The Vitamio library is a powerful library and supports playing in Microsoft Media Server (MMS) streams. As a network-based streaming media protocol, MMS is mainly used for network broadcasting and radio broadcasting. The MMS stream used with Vitamio in anroid is no different from other protocols. All you need to do is change the PATH variable to a MMS url:

        mVideoView = (VideoView) findViewById(R.id.vitamio_videoView);    path = "mms://beotelmedia.beotel.net/studiob";    mVideoView.setVideoPath(path);    mVideoView.setMediaController(new MediaController(this));    mVideoView.requestFocus();    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {        @Override        public void onPrepared(MediaPlayer mediaPlayer) {            mediaPlayer.setPlaybackSpeed(1.0f);        }    });
    Conclusion

    Through the above discussion, we can say that Vitamio is a powerful multi-platform Library (ios and android ). You can use the Vitamio library to play multiple types of video formats and protocols, such as RTMP, RTSP, HTTP Live, and HTTP progressive stream protocols. Another good feature is that vitamio supports subtitle and multi-track playback. The only drawback of Vitamio is that it is not completely open-source. You may need to purchase a license to use it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.