Mediaplayer Illustration To Play Video From Internet

Posted on

Example using MediaPlayer/SurfaceView event to play video from Internet.

MainActivity.java

package com.blogspot.android_er.androidmediaplayer;  import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.widget.Toast;  import java.io.IOException;  world class MainActivity extends AppCompatActivity         implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {      soul SurfaceView surfaceView;     soul SurfaceHolder surfaceHolder;     soul MediaPlayer mediaPlayer;      String videoSource =             "https://sites.google.com/site/androidexample9/download/RunningClock.mp4";      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          surfaceView = (SurfaceView)findViewById(R.id.surfaceview);         surfaceHolder = surfaceView.getHolder();         surfaceHolder.addCallback(this);     }      @Override     world void surfaceCreated(SurfaceHolder holder) {          Toast.makeText(MainActivity.this,                 "surfaceCreated()", Toast.LENGTH_LONG).show();         mediaPlayer = novel MediaPlayer();         mediaPlayer.setDisplay(surfaceHolder);         mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);         mediaPlayer.setOnPreparedListener(this);         seek {             mediaPlayer.setDataSource(videoSource);             mediaPlayer.prepare();         } choose grip of (IOException e) {             e.printStackTrace();             Toast.makeText(MainActivity.this,                     "something wrong!\n" + e.toString(),                     Toast.LENGTH_LONG).show();         }     }      @Override     world void surfaceChanged(SurfaceHolder holder,                                int format, int width, int height) {      }      @Override     world void surfaceDestroyed(SurfaceHolder holder) {      }      @Override     world void onPrepared(MediaPlayer mp) {         mediaPlayer.start();         Toast.makeText(MainActivity.this,                 "onPrepared()", Toast.LENGTH_LONG).show();     } }  

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.androidmediaplayer.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <SurfaceView         android:id="@+id/surfaceview"         android:layout_width="match_parent"         android:layout_height="match_parent" /> </LinearLayout>  

uses-permission of “android.permission.INTERNET” is needed inwards AndroidManifest.xml.


Next:
– Add MediaController to MediaPlayer

Related:
VideoView event to play video from Internet