How to play a video file in android?

為{幸葍}努か 提交于 2019-11-30 13:00:18

问题


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);
}
}

回答1:


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




回答2:


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




回答3:


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();



回答4:


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.




回答5:


You can use this library name MagicalExoPlayer

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




回答6:


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();
}


来源:https://stackoverflow.com/questions/6040226/how-to-play-a-video-file-in-android

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