Wednesday, May 6, 2015

Play stream video (from Raspberry Pi) on VideoView

This exercise play stream video from network on VideoView. (with APK download on bottom of this post)


- Prepare stream video from Raspberry Pi with Camera Module. Refer to my another blog post about Raspberry Pi "Stream Raspberry Pi Camera Module video using raspivid and vlc".


- Once stream video is playing, run this example, enter the IP of Raspberry Pi and assigned port.


activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidmediaplayer.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/addr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="http://192.168.xxx.xxx:8090" />
    <Button
        android:id="@+id/connect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Connect" />
    <VideoView
        android:id="@+id/streamview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

MainActivity.java
package com.example.androidmediaplayer;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import android.net.Uri;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

 EditText addrField;
 Button btnConnect;
 VideoView streamView;
 MediaController mediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        addrField = (EditText)findViewById(R.id.addr);
        btnConnect = (Button)findViewById(R.id.connect);
        streamView = (VideoView)findViewById(R.id.streamview);
        
        btnConnect.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    String s = addrField.getEditableText().toString();
    playStream(s);
   }});
        
    }
    
    private void playStream(String src){
     Uri UriSrc = Uri.parse(src);
        if(UriSrc == null){
         Toast.makeText(MainActivity.this, 
          "UriSrc == null", Toast.LENGTH_LONG).show();
        }else{
         streamView.setVideoURI(UriSrc);
            mediaController = new MediaController(this);
            streamView.setMediaController(mediaController);
            streamView.start();
            
            Toast.makeText(MainActivity.this, 
             "Connect: " + src, 
             Toast.LENGTH_LONG).show();
        }
    }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  streamView.stopPlayback();
 }

}

Permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidmediaplayer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="22" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


download filesDownload the files.

Or try the APK Here.

Updated@2016-07-06:
- Re-test on Raspberry Pi 3 + Camera Module V2 NoIR + Raspbian Jessie, play in Windows 10 + VLC Media Player and Android App with VideoView (with APK download).


12 comments:

Anonymous said...

Hi! Nice tutorial, but it doesn't work for me... it says "Cannot play the video"

Erik said...

Are you using the same source as me? Raspberry Pi/VLC?

Unknown said...

Nice tuturial but it doesnot work for me too

Unknown said...

I think device version.. my device Galaxy 5s, this android version Lollipop.
I tested the past version, and run . I Think Lollipop version chage RTSP Structure.

Unknown said...

Device version problem. My device Galaxy 5s. NOT work too.
I tested it's version down. The past android version, and work it.
rollipop version maybe change RTSP Structure.

Anonymous said...

I am using this example but when i run the app it says


E/MediaPlayer-JNI﹕ QCMediaPlayer mediaplayer NOT present

I am trying to view a live stream from a rapberry pi, its working on my laptop using vlc/network stream.
Any suggestions?

Erik said...

hello Aldo Contreras,

I re-tested without problem: Stream video from Raspberry Pi 3 + Camera Module V2, play in Windows 10/VLC Media Player and Android/VideoView..

Unknown said...

동영상을 재생 할 수 없습니다. 라고 뜨는데 왜 그런건가요??

Satheesh Chandran said...

Thanks, Simply the best

Anonymous said...

Hello,

I am trying to stream video on Android phone from an ip in videoview but it continuously saying one error : "D/MediaPlayer: Couldn't open file on client side, trying server side"
Can you help me in solving this error ?

Thanks in Advance!

Anonymous said...

Hello,my app also cannot display the video, could you tell me which Android version you were used? please, please.

Anonymous said...

does it work with webcam, raspberry pi 3 ?