How to play a video file in android?
Asked Answered
T

6

16

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 not working. Track controller is moving but I can't see any video on screen.

public class MPlayer extends Activity{
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playvideo);
    VideoView videoView = new VideoView(MPlayer.this);
    videoView.setMediaController(new MediaController(this));
    videoView.setVideoURI(Uri.parse("http://www.semanticdevlab.com/abc.mp4"));
    videoView.requestFocus();
    videoView.start();
    LinearLayout l = (LinearLayout)findViewById(R.id.mplayer);
    l.addView(videoView);
}
}
Teniacide answered 18/5, 2011 at 5:25 Comment(2)
Your file may not be compatible with android's fairly limited decoders. Copy it onto the sdcard and try playing it locally with the built in video application. Getting audio but no video is a common symptom of that. If that doesn't work, your program likely won't be able to stream it, unless you use software decoding (which will probably not be satisfactory on most devices)Goody
see this link for download and play the video here!Callender
T
6

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

Timberlake answered 18/5, 2011 at 5:30 Comment(1)
I tried this one. But got errors: AudioTrack ObtainBuffer timed out Audio Flinger write blocked for 70 secsTeniacide
T
17

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

Topic answered 26/9, 2012 at 8:31 Comment(2)
at the moment i am typing this comment, the link provided here is not working... This would have helped in my research...Pt
For a public URL I used videoView.setVideoPath(path); instead.Comstock
T
6

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

Timberlake answered 18/5, 2011 at 5:30 Comment(1)
I tried this one. But got errors: AudioTrack ObtainBuffer timed out Audio Flinger write blocked for 70 secsTeniacide
S
6

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();
Sedum answered 18/5, 2011 at 5:40 Comment(0)
W
3

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.

Waziristan answered 18/5, 2011 at 5:33 Comment(0)
L
1

This is how I played a video file from Network in my project

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>

enter image description here

Lowell answered 8/4, 2020 at 10:26 Comment(0)
S
0

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();
}
Supercargo answered 21/12, 2017 at 0:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.