I am placed video MP4 to my domain space. I have its public URL, Now i want to play it in my android app; but don\'t know how can I do this. I used following code which is n
The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.
Code:
videoView = (VideoView)findViewById(R.id.ViewVideo);
videoView.setVideoURI(Uri.parse(“android.resource://” + getPackageName() +”/”+R.raw.video));
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();
if you want see source code : Play video file using VideoView in Android
You should do it in onResume
, because in onCreate
VideoView
does not knows its size and can't create properly surface to display video.
public class MPlayer extends Activity{
VideoView videoView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playvideo);
videoView = new VideoView(MPlayer.this);
videoView.setMediaController(new MediaController(this));
LinearLayout l = (LinearLayout)findViewById(R.id.mplayer);
l.addView(videoView);
}
@Override
protected void onResume() {
super.onResume();
videoView.setVideoURI(Uri.parse("http://www.semanticdevlab.com/abc.mp4"));
videoView.start();
}
I think this may help you find some solution.
mp=new MediaPlayer();
mp.setDataSource(path);
mp.setScreenOnWhilePlaying(true);
mp.setDisplay(holder);
mp.prepare();
mp.start();
If you are trying this in your emulator, you might have to try it in a real device, because sometimes I too use face the same problem. I will not be able to view the video in emulator, but the video will play without any problem in the mobile. the problem is, I think with the emulator, not with your code.
Most of the time, I'm using following code:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(PATH_TO_FILE);
mp.prepare();
mp.start();
for more information look at this page: http://developer.android.com/guide/topics/media/index.html and http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html
Required Kotlin, AndroidX
Show a loading dialog while the file is buffering and then start playback:
private fun playVideo(videopath: String) {
Log.e("Playing Video File: ", "" + videopath);
try {
//Show Loader
val builder: AlertDialog.Builder = AlertDialog.Builder(this@ScreenCaptureImageActivity);
builder.setCancelable(false); // if you want user to wait for some process to finish,
builder.setView(R.layout.layout_loading_dialog);
progressDialog = builder.create();
//add Controller
val mediaController = MediaController(this@ScreenCaptureImageActivity);
videoView.setMediaController(mediaController)
//Add URI
//Uncomment to play from local path
//videoView.setVideoURI(Uri.parse(videopath))
//Example Play from Internet
videoView.setVideoPath("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4")
videoView.setOnPreparedListener {
progressDialog!!.dismiss();
//Start Playback
videoView.start()
Log.e(TAG, "Video Started");
}
} catch (e: Exception) {
progressDialog!!.dismiss();
Log.e(TAG, "Video Play Error :" + e.localizedMessage);
}
}
Loader 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="wrap_content"
android:orientation="horizontal"
android:padding="20dp">
<ProgressBar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center"
android:text="Please wait! This may take a moment." />
</LinearLayout>
**For Network Access add network config in the manifest, from ANdroid P its required **
<application
...
android:networkSecurityConfig="@xml/network_security_config"
>
Add network_security_config.xml in res/xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>