24
votes

I want to live streaming the video and it is in m3u8 format. So i tried the below code

public class StreamingPlayer extends Activity implements
OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback{

    private static final String TAG = StreamingPlayer.class.getSimpleName();
    private int mVideoWidth;
    private int mVideoHeight;
    private MediaPlayer mMediaPlayer;
    private SurfaceView mPreview;
    private SurfaceHolder holder;
    private String path;

    private boolean mIsVideoSizeKnown = false;
    private boolean mIsVideoReadyToBePlayed = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mediaplayer_2);
        mPreview = (SurfaceView) findViewById(R.id.surface);
        holder = mPreview.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    private void playVideo() {
        doCleanUp();
        try {

            /*
             * TODO: Set path variable to progressive streamable mp4 or
             * 3gpp format URL. Http protocol should be used.
             * Mediaplayer can only play "progressive streamable
             * contents" which basically means: 1. the movie atom has to
             * precede all the media data atoms. 2. The clip has to be
             * reasonably interleaved.
             * 
             */

            path = "httplive://xboodangx.api.channel.livestream.com/3.0/playlist.m3u8";

            if (path == "") {
                // Tell the user to provide a media file URL.
                Toast
                .makeText(
                        this,
                        "Please edit MediaPlayerDemo_Video Activity,"
                        + " and set the path variable to your media file URL.",
                        Toast.LENGTH_LONG).show();
            } 

            Log.e("PATH", "Path = " + path);
            // Create a new media player and set the listeners
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.setDisplay(holder);
                    mMediaPlayer.setOnBufferingUpdateListener(this);
                    mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.prepare();
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }
    }

    public void onBufferingUpdate(MediaPlayer arg0, int percent) {
        Log.d(TAG, "onBufferingUpdate percent:" + percent);

    }

    public void onCompletion(MediaPlayer arg0) {
        Log.d(TAG, "onCompletion called");
    }

    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
        Log.v(TAG, "onVideoSizeChanged called");
        if (width == 0 || height == 0) {
            Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
            return;
        }
        mIsVideoSizeKnown = true;
        mVideoWidth = width;
        mVideoHeight = height;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    public void onPrepared(MediaPlayer mediaplayer) {
        Log.d(TAG, "onPrepared called");
        mIsVideoReadyToBePlayed = true;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
        Log.d(TAG, "surfaceChanged called");

    }

    public void surfaceDestroyed(SurfaceHolder surfaceholder) {
        Log.d(TAG, "surfaceDestroyed called");
    }


    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated called");
        playVideo();

    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseMediaPlayer();
        doCleanUp();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseMediaPlayer();
        doCleanUp();
    }

    private void releaseMediaPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    private void doCleanUp() {
        mVideoWidth = 0;
        mVideoHeight = 0;
        mIsVideoReadyToBePlayed = false;
        mIsVideoSizeKnown = false;
    }

    private void startVideoPlayback() {
        Log.v(TAG, "startVideoPlayback");
        holder.setFixedSize(mVideoWidth, mVideoHeight);
        mMediaPlayer.start();
    }


}

In logcat it shows onBufferingUpdate percent:100 But i can't see the video.

Audio is working but suddenly it was struck.

And i tried this video link http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8 it is working. But my video link is not working and i changed httplive://... instead of http:// but no use.

And i saw this answer also Android video stream mms and m3u8.

In above link it shows The video cannot be played message.

6
Have you tried testing it on a real device? sometimes the emulator sucks when dealing with HTTP Live Streaming.yorkw
@yorkw Thanks for your response.Yes i tested in real device not in emulator.Ramakrishna
The URI h t t p://myvideo.m3u8 looks weird to me, where do you host the media? a remote server? If you have access to the broadcasting code on server side, try change it to a more solid URL like h t t p://domain-name/service-name/myvideo.m3u8. not sure if this work but worth to try.yorkw
this code is working or the edition was before fixing the problem?Guillermo Varini

6 Answers

15
votes

The video was existed in http://www.livestream.com. In this there is Mobile Api for live streaming.

The Api is:

http://www.livestream.com/userguide/index.php?title=Mobile_API#How_to_get_mobile_compatible_clips_from_a_channel.27s_library

In above link there is full information for mobile compatible. To get the rtsp link from the channel to use this link

http://xproshowcasex.channel-api.livestream-api.com/2.0/getstream

Replace the your channel name instead of proshowcase. And then get all mobile compatible url's like IPhone, Android, Blackberry etc.,

Using that url you can stream the video in Android by using video view or media player.

For more information please read the Mobile Api link.

If any one get the same problem I hope this answer will helps you.

Best of luck.

7
votes

I have no problem to play stream:

videoView1.setVideoPath("http://***.net/livedemo/_definst_/stream3.stream/playlist.m3u8?wowzasessionid=773395207");
videoView1.start();

About the message:

The video cannot be played

Maybe you need to add permissions to your Manifest file:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
1
votes

I think you should move this:

mMediaPlayer.setOnPreparedListener(this);

To be before the call to prepare()

1
votes

Did you try to play your link with native player directly through web browser? If you can not play it with native player, it means that your file is not supported by your current Android version. HTTP Live Streaming format can have some specificities that are not well supported by Android, whereas it can play on IOS.

0
votes

I tried m3u8 video format for more than 6 months and it is not succeeded. It is playing in my iPhone app and native applications. My streaming server is Red5 and it has no RTSP plugin. It gives out RTMP streaming and it could not be streamed in Android. I waited for one year to get an OS having support for RTSP streaming but google haven't. Still I am using a web view with a flash player to stream live video(It has not much clarity). I feel shame to say this to my client and continuing search to play the live stream in Android default player.

I think your video url may not RTSP.

0
votes

How to play .M3U8 Streaming in Android

activity_main.xml

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

    <VideoView
        android:id="@+id/myVideoView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Main.java

package com.grexample.ooyalalive;

import java.net.URL;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class Main extends Activity {

    private String urlStream;
    private VideoView myVideoView;
    private URL url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_vv);//***************
            myVideoView = (VideoView)this.findViewById(R.id.myVideoView);
            MediaController mc = new MediaController(this);
            myVideoView.setMediaController(mc);         
            urlStream = "http://jorgesys.net/i/irina_delivery@117489/master.m3u8";

            myVideoView.setVideoURI(Uri.parse(urlStream)); 

    }
}

I have seen a lot of people have problems playing .M3U8, it depends on the codecs used for the streaming and compatibility with the device, for example some of my .m3u8 files are only supported in devices with screens of 1200 x800 and higher.