How Android live streaming rtmp streams

Source: Internet
Author: User



On Android, video/audio streaming is a very rare part of the spotlight. RTMP (Real time Messaging Protocol) is indispensable whenever we discuss streaming media. RTMP is a basic video/audio streaming protocol, but unfortunately Android standard Videoview does not support rtmp playback.  Therefore, if you want to play rtmp live streaming on Android, you must use a library that supports the RTMP protocol. Library 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, real hardware accelerator decoder and renderer Api,vitamio is a very powerful library that supports a wide variety of audio and video formats such as FLV, TS/TP, WMV, DivX, XviD and many other standard formats. What's different is that it also supports similar. mkv and. SRT embedding and external subtitle playback. But it comes with a license, so be sure to get certified before using it. In this Android rtmp example, we will discuss not only the rtmp live stream, but also the m3u8 stream (HLS), RTSP Stream, and MMS (Microsoft Media stream). First let's refer to the Vitamio library in our project.


The steps for referencing the Vitamio library in Android Studio are as follows:


Download Vitamio bundle Https://github.com/yixia/VitamioBundle Unzip and File->import Module on Android Studio To specify the Vitamiobundle path, select the Vitamio folder click Finish to add the dependent item (': Vitamio ') on the Build.gradle (Module:app) Dependency section to open Build.gradle (Module: Vitamio)-Change the minimum SDK version to 7 do not forget to add Internet permissions in Manifest.xml complete!


Android rtmp Stream


Before we tell you how to use it, let's get to the next rtmp. Real time Messaging Protocol (RTMP) is an agreement owned by Adobe Systems. The agreement is a Flash player owned by Adobe to develop audio and video streams. Later portions of the agreement were publicly available for public use. See here for more information. This protocol is mostly used for IPTV and real-time video on demand streaming, but it is also used for other leads.



On Android, the standard Videoview does not support rtmp playback. But WebView can play rtmp streams. This solves the problem of playing rtmp streams, but I don't think web apps can provide a good interface and experience. So in this example of the Android rtmp we will use third-party libraries to stream streaming media-vitamio live rtmp streams. After referencing Vitamio in your project, add the Vitamio videoview to your layout file:



Activity_main.xml



<codeclass="language-xml"hljs="">    <!--?xml version=1.0encoding=utf-8?-->    <linearlayout android:layout_height="match_parent"android:layout_width="match_parent"android:orientation="vertical"xmlns:android="http://schemas.android.com/apk/res/android">        <io.vov.vitamio.widget.videoview android:id="@+id/vitamio_videoView"android:layout_height="wrap_content"android:layout_width="wrap_content">    </io.vov.vitamio.widget.videoview></linearlayout></code>


Please also write your activity as follows:



Mainactivity.java



<codeclass="language-java"hljs="">   packagecom.truiton.rtmpplayer;    importandroid.net.Uri;    importandroid.os.Bundle;    importandroid.support.v7.app.ActionBarActivity;    importjava.util.HashMap;    importio.vov.vitamio.LibsChecker;    importio.vov.vitamio.MediaPlayer;    importio.vov.vitamio.widget.MediaController;    importio.vov.vitamio.widget.VideoView;    publicclassMainActivityextendsActionBarActivity {        privatestaticfinalString TAG = MainActivity;        privateString path;        //private HashMap<string, string=""> options;        privateVideoView mVideoView;        @Override        protectedvoidonCreate(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(newMediaController(this));            mVideoView.requestFocus();            mVideoView.setOnPreparedListener(newMediaPlayer.OnPreparedListener() {                @Override                publicvoidonPrepared(MediaPlayer mediaPlayer) {                    mediaPlayer.setPlaybackSpeed(1.0f);                }            });        }    }</string,></code>


Although the above code is clear, it is important to note that you should modify the path of the rtmp stream you are playing. On Android, it is sometimes possible to play rtmp streams with a header path. Fortunately, the Vitamio rtmp player also supports this approach. Therefore, all types of rtmp streams can use the Vitamio library. The example above would look like this:


Android RTSP Streaming Media


Live Streaming Protocol (RTSP) transmits content through a multimedia server, such as YouTube, which publishes content using RTSP streaming. The easy part about the RTSP stream is that it can be done with Android standard Videoview, for more information, please refer to my videoview example.



But if you use the Vitamio library, you can play the RTSP stream better. In fact Vitamio also supports the playback of RTSP streams. And the above procedure is the same, including the Vitamio videoview in the layout file, and the RTSP URL specified using the PATH variable



<codeclass="language-java"hljs="">    mVideoView = (VideoView) findViewById(R.id.vitamio_videoView);    path = rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov;    mVideoView.setVideoPath(path);    mVideoView.setMediaController(newMediaController(this));    mVideoView.requestFocus();    mVideoView.setOnPreparedListener(newMediaPlayer.OnPreparedListener() {        @Override        publicvoidonPrepared(MediaPlayer mediaPlayer) {            mediaPlayer.setPlaybackSpeed(1.0f);        }    });</code>
Android m3u8 Streaming Media


"How to play m3u8 video on Android" is one of the most common problems for Android developers. The simplest way to stream live video via the HTTP protocol is to use standard videoview. However, the m3u8 stream can only be played on devices above android3.0. Since the introduction of Http/https Live and HTTP/HTTPS progressive streaming protocols in Android 3.0, HTTPS is fully supported in android3.1.



If you want to implement HTTP live streaming (HLS) on an earlier version that supports Android m3u8 streaming. You should consider using the Vitamio library, which supports playing m3u8 above Android API7. Using the same method, use the Vitamio videoview in the layout file, and specify the HTTP live streaming URL.



<codeclass="language-java"hljs="">    mVideoView = (VideoView) findViewById(R.id.vitamio_videoView);    path = http://93.184.221.133/00573D/236/236-0.m3u8;    mVideoView.setVideoPath(path);    mVideoView.setMediaController(newMediaController(this));    mVideoView.requestFocus();    mVideoView.setOnPreparedListener(newMediaPlayer.OnPreparedListener() {        @Override        publicvoidonPrepared(MediaPlayer mediaPlayer) {            mediaPlayer.setPlaybackSpeed(1.0f);        }    });</code>


Playing m3u8 stream on Android with Vitamio would look something like this:
Use Vitamio on Androi to play the m3u8 stream effect as follows:


Android MMS Stream


The Vitamio library is a powerful library that also supports playback in Microsoft Media Server (MMS) streams. MMS is a network streaming media protocol, which is mainly used for webcast and radio broadcasting. Using Vitamio for MMS streaming in Anroid is not different from other protocols. All you need to do is change the path variable to point to an MMS URL:



<codeclass="language-java"hljs="">    mVideoView = (VideoView) findViewById(R.id.vitamio_videoView);    path = mms://beotelmedia.beotel.net/studiob;    mVideoView.setVideoPath(path);    mVideoView.setMediaController(newMediaController(this));    mVideoView.requestFocus();    mVideoView.setOnPreparedListener(newMediaPlayer.OnPreparedListener() {        @Override        publicvoidonPrepared(MediaPlayer mediaPlayer) {            mediaPlayer.setPlaybackSpeed(1.0f);        }    });</code>
Conclusion


Through the discussion above, it is possible to say that Vitamio is a powerful multi-platform library (iOS and Android). By using the Vitamio library you can play multiple types of video formats and protocols such as RTMP, RTSP, HTTP Live, and HTTP progressive streaming protocol. Another good feature is that Vitamio supports subtitles and multi-track playback. The only drawback of Vitamio is that it is not entirely open source. You may need to purchase a license to use it. Hopefully this will help. Contact us for more updates via Facebook, Google + and Twitter.



How Android live streaming rtmp streams


Related Article

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.