How to play a video file in android?

荒凉一梦 提交于 2019-11-30 05:11:44

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

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

JKV

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.

You can use this library name MagicalExoPlayer

It is based on google video player (ExoPlayer) and it is really easy to use.

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!