How to Live RTMP HLS in Android

Keywords: Android MediaPlayer xml Java

On android, live video/audio streaming is one of the few concerns.
Whenever we discuss streaming media, RTMP(Real Time Messaging Protocol) is indispensable. RTMP is a basic video/audio live streaming protocol, but unfortunately the Android standard VideoView does not support RTMP playback.
Therefore, if you want to play RTMP live streams on android, you must use libraries that support RTMP protocol. In this tutorial, we will discuss how to use android's Vitamio( https://www.vitamio.org/en/ ) The library plays streaming media transmitted by 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 accelerated decoder and renderer API. Vitamio is a very powerful library supporting a variety of audio and video formats such as FLV, TS/TP, WMV, DivX, Xvid and other standard formats. The difference is that it also supports similar. mkv and. srt embedded and plug-in subtitle playback. But it comes with a license, so get certified before using it. In this android RTMP example, we will discuss not only RTMP live streaming, but also m3u8 streaming (HLS), RTSP streaming and MMS (Microsoft Media Stream). First let's refer to the Vitamio Library in our project.

The steps to refer to 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
Specify the path to VitamioBundle, select the vitamio folder and click Finish
Add a dependency project (': vitamio') to the build.gradle(Module: app) dependency section
Open build.gradle (Module: vitamio) - Change the minimum sdk version to 7
Don't forget to add internet permissions to manifest.xml
Finish!
Android RTMP stream

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

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 android RTMP example, we will use a third-party library, Vitamio Live RTMP Streaming Media. After introducing Vitamio in engineering, add Vitamio's VideoView to your layout file:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

    <io.vov.vitamio.widget.VideoView
        android:id="@+id/vitamio_videoView"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

    <io.vov.vitamio.widget.VideoView
        android:id="@+id/vitamio_videoView"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

</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<String, String> 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);
            }
        });
    }
}


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<String, String> 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, it should be pointed out that please modify your path to play RTMP stream. On android, it is sometimes possible to play RTMP streams using header paths. Fortunately, Vitamio RTMP players also support this approach. Therefore, Vitamio libraries can be used for all types of RTMP flows. The above example will look like this:
Rtmpplayer

Android RTSP Streaming Media

Real-time Streaming Protocol (RTSP) transmits content through multimedia servers, such as YouTube using RTSP streams to publish content. The easy part about RTSP streaming is that it can be done through android standard VideoView. For more information, please refer to my VideoView example.

But if you use the Vitamio library, you can play RTSP streams better. In fact, Vitamio also supports the return of RTSP streams. The same procedure as above, including VideoView of Vitamio in the layout file, and using RTSP url specified by path variables

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);
    }
});


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

"How to play m3u8 video on android" is one of the most common problems for android developers. The simplest way to broadcast live video streams through Http protocol is to use standard VideoView. But only m3u8 streams can be played on devices above Android 3.0. Because of the introduction of HTTP/ HTTPS live broadcast and HTTP/ HTTPS progressive streaming protocol in Android 3.0, HTTPS is fully supported in android 3.1.

If you want to implement HTTP Real-Time Streaming Media (HLS) that supports android m3u8 streaming in earlier versions. Consider using the Vitamio library, which supports m3u8 playback above Android API 7. In the same way, Vitamio's VideoView is used in the layout file and the HTTP real-time streaming media URL is specified.

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);
    }
});


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 would look something like this:
Using Vitamio to play m3u8 stream on androi is as follows:
m3u8

Android MMS stream

The Vitamio library is a powerful library that also supports playback in streams of Microsoft Media Server (MMS). MMS is a network streaming media protocol, which is mainly used for network broadcasting and live broadcasting. There is no difference between using Vitamio for MMS streaming in anroid and other protocols. All you need to do is change the path variable to point to an 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);
    }
});


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
From the above discussion, it is certain that Vitamio is a powerful multi-platform library (ios and android). Vitamio library can play many kinds of video formats and protocols, such as RTMP, RTSP, HTTP Live, and HTTP Progressive Streaming Protocol. Another good feature is that vitamio supports subtitling and multi-track playback. The only disadvantage of Vitamio is that it is not completely open source. You may need to purchase a license to use it. I hope this will help. Contact us via Facebook, Google+ and Twitter for more updates.

Posted by Miri4413 on Wed, 15 May 2019 12:23:56 -0700