Showing posts with label VideoView. Show all posts
Showing posts with label VideoView. Show all posts
Wednesday, July 6, 2016
Stream video from Raspberry Pi 3 + Camera V2, play in Android/VideoView
This video show how to stream Camera Module NoIR V2 video from Raspberry Pi 3/Raspbian Jessie using vlc, play in Android App with VideoView. Details, refer my another blogspot.
Monday, May 9, 2016
Open mp4 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT and ACTION_PICK, and play in VideoView.
This example show how to open video file of mp4 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT and ACTION_PICK, and play in VideoView, also add MediaController to the VideoView.
This example can also play videos stored in Google Drive:
(tested on XiaoMi RedMi 2 running Android 4.4.4)
MainActivity.java
package com.blogspot.android_er.myopenmp4;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
Button btnOpenDocument, btnGetContent, btnPick;
TextView info;
VideoView myVideoView;
Uri videoFileUri = null;
final static int RQS_OPEN_DOCUMENT = 1;
final static int RQS_GET_CONTENT = 2;
final static int RQS_PICK = 3;
MediaController mediaController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
info = (TextView) findViewById(R.id.info);
myVideoView = (VideoView)findViewById(R.id.vview);
btnOpenDocument = (Button)findViewById(R.id.opendocument);
btnGetContent = (Button)findViewById(R.id.getcontent);
btnPick = (Button)findViewById(R.id.pick);
btnOpenDocument.setOnClickListener(btnOpenDocumentOnClickListener);
btnGetContent.setOnClickListener(btnGetContentOnClickListener);
btnPick.setOnClickListener(btnPickOnClickListener);
mediaController = new MediaController(MainActivity.this);
myVideoView.setMediaController(mediaController);
}
private void prepareVideo(){
Toast.makeText(MainActivity.this,
videoFileUri.toString(),
Toast.LENGTH_LONG).show();
myVideoView.setVideoURI(videoFileUri);
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
}
MediaPlayer.OnCompletionListener myVideoViewCompletionListener =
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(MainActivity.this, "End of Video",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =
new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
long duration = myVideoView.getDuration(); //in millisecond
Toast.makeText(MainActivity.this,
"Duration: " + duration + " (ms)",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnErrorListener myVideoViewErrorListener =
new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
String errWhat = "";
switch (what){
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
errWhat = "MEDIA_ERROR_UNKNOWN";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
errWhat = "MEDIA_ERROR_SERVER_DIED";
break;
default: errWhat = "unknown what";
}
String errExtra = "";
switch (extra){
case MediaPlayer.MEDIA_ERROR_IO:
errExtra = "MEDIA_ERROR_IO";
break;
case MediaPlayer.MEDIA_ERROR_MALFORMED:
errExtra = "MEDIA_ERROR_MALFORMED";
break;
case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
errExtra = "MEDIA_ERROR_UNSUPPORTED";
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
errExtra = "MEDIA_ERROR_TIMED_OUT";
break;
default:
errExtra = "...others";
}
Toast.makeText(MainActivity.this,
"Error!!!\n" +
"what: " + errWhat + "\n" +
"extra: " + errExtra,
Toast.LENGTH_LONG).show();
return true;
}
};
View.OnClickListener btnOpenDocumentOnClickListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("video/mp4");
startActivityForResult(
Intent.createChooser(intent, "ACTION_OPEN_DOCUMENT"),
RQS_OPEN_DOCUMENT);
}
};
View.OnClickListener btnGetContentOnClickListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("video/mp4");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(
intent, "ACTION_GET_CONTENT"), RQS_GET_CONTENT);
}
};
View.OnClickListener btnPickOnClickListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(
Intent.createChooser(intent, "ACTION_PICK"),
RQS_PICK);
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == RQS_OPEN_DOCUMENT
|| requestCode == RQS_GET_CONTENT
|| requestCode == RQS_PICK){
videoFileUri = data.getData();
info.setText(videoFileUri.toString());
prepareVideo();
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.myopenmp4.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" />
<Button
android:id="@+id/opendocument"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ACTION_OPEN_DOCUMENT" />
<Button
android:id="@+id/getcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ACTION_GET_CONTENT" />
<Button
android:id="@+id/pick"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ACTION_PICK" />
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<VideoView
android:id="@+id/vview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
uses-permission of "android.permission.READ_EXTERNAL_STORAGE" is needed in AndroidManifest.xml.
Related:
- Open mp3 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT and ACTION_PICK, with checking and requesting permission at runtime.
- VideoView example to play video from Internet.
Tuesday, April 12, 2016
Capture frames in VideoView using MediaMetadataRetriever
MainActivity.java
package com.blogspot.android_er.androidvideoview;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.VideoView;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
Button btnCapture;
Spinner spOption;
VideoView myVideoView;
String videoSource =
"https://sites.google.com/site/androidexample9/download/RunningClock.mp4";
Uri uriVideoSource;
MediaController myMediaController;
MediaMetadataRetriever myMediaMetadataRetriever;
String[] stringOpts = {
"none",
"OPTION_CLOSEST",
"OPTION_CLOSEST_SYNC",
"OPTION_NEXT_SYNC",
"OPTION_PREVIOUS_SYNC"};
int[] valOptions ={
0, //will not be used
MediaMetadataRetriever.OPTION_CLOSEST,
MediaMetadataRetriever.OPTION_CLOSEST_SYNC,
MediaMetadataRetriever.OPTION_NEXT_SYNC,
MediaMetadataRetriever.OPTION_PREVIOUS_SYNC};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView)findViewById(R.id.vview);
prepareVideo();
spOption = (Spinner)findViewById(R.id.option);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, stringOpts);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spOption.setAdapter(adapter);
btnCapture = (Button)findViewById(R.id.capture);
btnCapture.setOnClickListener(btnCaptureOnClickListener);
}
View.OnClickListener btnCaptureOnClickListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
int currentPosition = myVideoView.getCurrentPosition(); //in millisecond
Toast.makeText(MainActivity.this,
"Current Position: " + currentPosition + " (ms)",
Toast.LENGTH_LONG).show();
Bitmap bmFrame;
int pos = currentPosition * 1000; //unit in microsecond
int opt = spOption.getSelectedItemPosition();
if(opt == 0){
bmFrame = myMediaMetadataRetriever
.getFrameAtTime(pos);
}else{
bmFrame = myMediaMetadataRetriever
.getFrameAtTime(pos,
valOptions[opt]);
}
if(bmFrame == null){
Toast.makeText(MainActivity.this,
"bmFrame == null!",
Toast.LENGTH_LONG).show();
}else {
AlertDialog.Builder myCaptureDialog =
new AlertDialog.Builder(MainActivity.this);
ImageView capturedImageView = new ImageView(MainActivity.this);
capturedImageView.setImageBitmap(bmFrame);
LinearLayout.LayoutParams capturedImageViewLayoutParams =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
capturedImageView.setLayoutParams(capturedImageViewLayoutParams);
myCaptureDialog.setView(capturedImageView);
myCaptureDialog.show();
}
}
};
private void prepareVideo(){
myMediaMetadataRetriever = new MediaMetadataRetriever();
myMediaMetadataRetriever.setDataSource(
videoSource, new HashMap<String, String>());
myMediaController = new MediaController(MainActivity.this);
myVideoView.setMediaController(myMediaController);
Toast.makeText(MainActivity.this, videoSource, Toast.LENGTH_LONG).show();
uriVideoSource = Uri.parse(videoSource);
myVideoView.setVideoURI(uriVideoSource);
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
}
MediaPlayer.OnCompletionListener myVideoViewCompletionListener =
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(MainActivity.this, "End of Video",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =
new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
long duration = myVideoView.getDuration(); //in millisecond
Toast.makeText(MainActivity.this,
"Duration: " + duration + " (ms)",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnErrorListener myVideoViewErrorListener =
new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
String errWhat = "";
switch (what){
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
errWhat = "MEDIA_ERROR_UNKNOWN";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
errWhat = "MEDIA_ERROR_SERVER_DIED";
break;
default: errWhat = "unknown what";
}
String errExtra = "";
switch (extra){
case MediaPlayer.MEDIA_ERROR_IO:
errExtra = "MEDIA_ERROR_IO";
break;
case MediaPlayer.MEDIA_ERROR_MALFORMED:
errExtra = "MEDIA_ERROR_MALFORMED";
break;
case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
errExtra = "MEDIA_ERROR_UNSUPPORTED";
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
errExtra = "MEDIA_ERROR_TIMED_OUT";
break;
default:
errExtra = "...others";
}
Toast.makeText(MainActivity.this,
"Error!!!\n" +
"what: " + errWhat + "\n" +
"extra: " + errExtra,
Toast.LENGTH_LONG).show();
return true;
}
};
}
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidvideoview.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" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/capture"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Capture"/>
<Spinner
android:id="@+id/option"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
<VideoView
android:id="@+id/vview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
uses-permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.
Monday, April 11, 2016
Add MediaController to VideoView
Last post show a example of "VideoView example to play video from Internet" without controls. This example show adding MediaController to VideoView, to provide controls of Play/Pause, Forward and Backward.
test on Nexus 9 tablet (Emulator) running Marshmallow:
test on Nexus 7 running Android 5.1.1:
MainActivity.java
package com.blogspot.android_er.androidvideoview;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
VideoView myVideoView;
String videoSource =
"https://sites.google.com/site/androidexample9/download/RunningClock.mp4";
Uri uriVideoSource;
MediaController myMediaController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView)findViewById(R.id.vview);
prepareVideo();
}
private void prepareVideo(){
myMediaController = new MediaController(MainActivity.this);
myVideoView.setMediaController(myMediaController);
Toast.makeText(MainActivity.this, videoSource, Toast.LENGTH_LONG).show();
uriVideoSource = Uri.parse(videoSource);
myVideoView.setVideoURI(uriVideoSource);
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
}
MediaPlayer.OnCompletionListener myVideoViewCompletionListener =
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(MainActivity.this, "End of Video",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =
new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
long duration = myVideoView.getDuration(); //in millisecond
Toast.makeText(MainActivity.this,
"Duration: " + duration + " (ms)",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnErrorListener myVideoViewErrorListener =
new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
String errWhat = "";
switch (what){
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
errWhat = "MEDIA_ERROR_UNKNOWN";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
errWhat = "MEDIA_ERROR_SERVER_DIED";
break;
default: errWhat = "unknown what";
}
String errExtra = "";
switch (extra){
case MediaPlayer.MEDIA_ERROR_IO:
errExtra = "MEDIA_ERROR_IO";
break;
case MediaPlayer.MEDIA_ERROR_MALFORMED:
errExtra = "MEDIA_ERROR_MALFORMED";
break;
case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
errExtra = "MEDIA_ERROR_UNSUPPORTED";
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
errExtra = "MEDIA_ERROR_TIMED_OUT";
break;
default:
errExtra = "...others";
}
Toast.makeText(MainActivity.this,
"Error!!!\n" +
"what: " + errWhat + "\n" +
"extra: " + errExtra,
Toast.LENGTH_LONG).show();
return true;
}
};
}
For layout and uses-permission, refer last post "VideoView example to play video from Internet".
Next:
- Capture frames in VideoView using MediaMetadataRetriever
Related:
- Open mp4 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT and ACTION_PICK, and play in VideoView.
VideoView example to play video from Internet
Example of VideoView to play video from Internet:
MainActivity.java
layout/activity_main.xml
uses-permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.
When I tested it on Nexus 7 running Android 5.1.1, control bar (Play/Pause, Forward and Backward) is shown over the VideoView. But not shown on RedMi 2 running Android 5.0, Android Emulator of Nexus 6P phone, Nexus 7 tablet running Marshmallow, and Nexus 5x phone running Android N.
test on Nexus 7 running Android 5.1.1:
test on RedMi 2 running Android 5.0
test on Nexus 6P phone (Emulator) running Marshmallow
test on Nexus 9 tablet (Emulator) running Marshmallow
test on Nexus 5X (Emulator) running Android N
Remark:
To runing this example on Android Emulator, have to manual select "Hardware = GLES 2.0" in Emulated Performance of Graphics.
Next:
- Add MediaController to VideoView, to provide controls of Play/Pause, Forward and Backward.
Related:
- MediaPlayer example to play video from Internet
- Open mp4 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT and ACTION_PICK, and play in VideoView.
MainActivity.java
package com.blogspot.android_er.androidvideoview;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
VideoView myVideoView;
String videoSource =
"https://sites.google.com/site/androidexample9/download/RunningClock.mp4";
Uri uriVideoSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView)findViewById(R.id.vview);
prepareVideo();
}
private void prepareVideo(){
Toast.makeText(MainActivity.this, videoSource, Toast.LENGTH_LONG).show();
uriVideoSource = Uri.parse(videoSource);
myVideoView.setVideoURI(uriVideoSource);
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
}
MediaPlayer.OnCompletionListener myVideoViewCompletionListener =
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(MainActivity.this, "End of Video",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =
new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
long duration = myVideoView.getDuration(); //in millisecond
Toast.makeText(MainActivity.this,
"Duration: " + duration + " (ms)",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnErrorListener myVideoViewErrorListener =
new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
String errWhat = "";
switch (what){
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
errWhat = "MEDIA_ERROR_UNKNOWN";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
errWhat = "MEDIA_ERROR_SERVER_DIED";
break;
default: errWhat = "unknown what";
}
String errExtra = "";
switch (extra){
case MediaPlayer.MEDIA_ERROR_IO:
errExtra = "MEDIA_ERROR_IO";
break;
case MediaPlayer.MEDIA_ERROR_MALFORMED:
errExtra = "MEDIA_ERROR_MALFORMED";
break;
case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
errExtra = "MEDIA_ERROR_UNSUPPORTED";
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
errExtra = "MEDIA_ERROR_TIMED_OUT";
break;
default:
errExtra = "...others";
}
Toast.makeText(MainActivity.this,
"Error!!!\n" +
"what: " + errWhat + "\n" +
"extra: " + errExtra,
Toast.LENGTH_LONG).show();
return true;
}
};
}
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidvideoview.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" />
<VideoView
android:id="@+id/vview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
uses-permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.
When I tested it on Nexus 7 running Android 5.1.1, control bar (Play/Pause, Forward and Backward) is shown over the VideoView. But not shown on RedMi 2 running Android 5.0, Android Emulator of Nexus 6P phone, Nexus 7 tablet running Marshmallow, and Nexus 5x phone running Android N.
test on Nexus 7 running Android 5.1.1:
test on RedMi 2 running Android 5.0
test on Nexus 6P phone (Emulator) running Marshmallow
test on Nexus 9 tablet (Emulator) running Marshmallow
test on Nexus 5X (Emulator) running Android N
Remark:
To runing this example on Android Emulator, have to manual select "Hardware = GLES 2.0" in Emulated Performance of Graphics.
Next:
- Add MediaController to VideoView, to provide controls of Play/Pause, Forward and Backward.
Related:
- MediaPlayer example to play video from Internet
- Open mp4 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT and ACTION_PICK, and play in VideoView.
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
MainActivity.java
Permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.
Download 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).
- 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>
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).
Subscribe to:
Posts (Atom)





