Android Studio: RTMP Streaming

From OnnoWiki
Jump to navigation Jump to search

Sumber: http://www.truiton.com/2015/03/stream-rtmp-live-android/


Streaming live video/audio in android is one of the very few interesting parts. Whenever we talk of streaming, the possibility of using RTMP(Real Time Messaging Protocol) cannot be ruled out. As RTMP is one of the basic protocols available for streaming live video/audio feed. But unfortunately Android’s standard VideoView does not support the playback of RTMP. Therefore to natively play a live RTMP stream in Android, an external library is required which can provide us an Android RTMP player. In this tutorial we will discuss how this can be achieved by the usage of Android Vitamio library. Android Vitamio Library

Vitamio is an open source project for Android and iOS built up on FFmpeg code. Vitamio provides us a clean and simple API with full and real hardware accelerated decoder and render-er. Vitamio is a very powerful library which supports a wide variety of video/audio formats like FLV, TS/TP, WMV, DivX, Xvid and many other standard formats. What makes it different is that it also supports the playback of embedded and external subtitles like .mkv and .srt. But Vitamio comes with a license, hence please go through this license page before using it in your project. In this Android RTMP example, we will not only discuss the live streaming of RTMP stream in Android, but will also discuss how we can stream m3u8 playlists(HLS), RTSP streams and MMS (Microsoft Media Stream). But first lets include the Android Vitamio library in out project. Steps to include Vitamio library in Android Studio:

   Download the Vitamio bundle from here : https://github.com/yixia/VitamioBundle
   Unzip it and go to File->Import Module in Android Studio.
   Navigate to VitamioBundle-master, select vitamio folder and press finish.
   Add compile project(‘:vitamio’) in build.gradle(Module: app) dependencies section.
   Open build.gradle (Module: vitamio) – change the min sdk version to 7.
   Also please don’t forget to add the internet permission in your manifest.
   Done !

Streaming RTMP Stream Live in Android

Before heading directly to the implementation part, first lets understand RTMP a little. Real Time Messaging Protocol (RTMP) is a protocol owned by Adobe Systems. This protocol was developed to stream audio and video content to company’s proprietary flash player. But later on as it evolved, a part of it was released for public use. More of it can be read here. Mostly this type of protocol is used for IPTV and live VoD streaming, but it can be used in many areas as well.

To play an RTMP stream in Android, a normal VideoView cannot be used, as it does not support the RTMP playback. But a WebView can be used to play the RTMP stream without any external library. This solves the problem but in my personal opinion web apps don’t give a nice look and feel to an app. Hence in this Android RTMP example we will be using an external library – Vitamio for streaming a live RTMP stream. After including it in the project, please add Vitamio’s VideoView in 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>

Further to run this Android RTMP player, please code your activity something like this: 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);
            }
        });
    }
}

Although the above code is self explanatory, please change the path to play your RTMP stream. At times to stream an RTMP stream in Android, you may have to pass the headers along with the path. Luckily you may see above that Vitamio RTMP player supports that too. Hence all types of RTMP streams can be used with Vitamio library. The above example would look something like this:

Android RTMP Stream Live Android RTSP Streaming

Real Time Streaming Protocol (RTSP) is used for streaming content from media servers. Like YouTube uses RTSP streams to distribute content. The easy part about RTSP streaming is that, it can be done through the standard VideoView of Android. To read more about it please refer to my VideoView Example.

But if you are implementing a solution through the usage of Vitamio library, you might prefer using the same for RTSP streaming as well. As a matter of fact Vitamio also supports the playback of RTSP stream. To do so the process is same, include Vitamio’s VideoView in your layout file and use the path variable to specify the RTSP url.

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

How to play m3u8 video in Android? is one of the most common questions among the Android developers. The easiest way to do HTTP Live Streaming (HLS) in Android is through the usage of standard VideoView. But as you might have guessed, there are some limitations to it. Through standard Android VideoView you can stream m3u8 streams but only on the devices having Android 3.0 and above. This is because the HTTP/HTTPS live streaming and HTTP/HTTPS progressive streaming protocols were introduced in Android 3.0 and full support with HTTPS was given in 3.1.

If you wish to do HTTP Live Streaming (HLS) and support Android m3u8 streaming, for earlier versions of android. You should consider using the Vitamio library, this library adds the support for the playback of m3u8 lists on android API 7 and above. To do so the process is same- include Vitamio’s VideoView in your layout file and use the path variable to 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 would look something like this:

Android m3u8 Streaming Android MMS Stream

Vitamio Library is a powerful library which also supports the playback of Microsoft Media Server (MMS) stream in Android. MMS is network streaming protocol, mostly used for webcasts and live radio. Using Vitamio for the playback of an MMS stream in Android is not that different from using other protocols. All you have to do just replace 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);
    }
});


Conclusion

As we discussed above, I would like to conclude by saying that Vitamio is a very powerful multi-platform library, for iOS and Android both. By using Vitamio library one can stream many types of video formats and protocols like RTMP, RTSP, HTTP Live, and HTTP progressive streaming protocol. Another great playback feature that comes along with Vitamio is that it supports the playback of subtitles and multiple audio tracks. The only disadvantage with Vitamio is that, its not completely open-source. You might have to purchase a license to use it. Hope this helped. Connect with us through our Facebook, Google+ and Twitter profiles for more updates.



Referensi