Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play a video file in android?

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);
}
}
like image 676
zeeshan Avatar asked May 18 '11 05:05

zeeshan


People also ask

What video format can play on Android?

Android Phones Supported Video Formats. The default video player in Android phones enables you to play video in H. 263, H. 264 AVC, MPEG-4 SP, and VP8.

Why won't my videos play on my Android?

The file might not be compatible with your Android phone or the media player you are using. Chances are that the SD card where your video has been stored could be corrupted as well. If the video has been shot on your phone, then some changes in its settings or encoding could have made the video unsupported.


2 Answers

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

like image 138
AndroidLover Avatar answered Oct 17 '22 11:10

AndroidLover


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

like image 6
Hesam Avatar answered Oct 17 '22 11:10

Hesam