Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write a simple video player on android?

Tags:

android

I have code for simple audio player on android. But it is not working for videos. Please guide me to write a simple video player. The code of audio player is

package com.example.helloplayer;

public class HelloPlayer extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MediaPlayer mp = MediaPlayer.create(this, R.raw.file); 
        mp.start();
   }
}
like image 499
Gpathy Avatar asked Jul 04 '11 06:07

Gpathy


People also ask

Does Android have a built in video player?

The video player is available for free in the Google Play Store. You can also get plugins for additional features. The only major downside is that it includes commercials.

How do I view videos on Android?

For showing the video in our android application we will use the VideoView widget. The VideoView widget is capable of playing media files, and the formats supported by the VideoView are 3gp and MP4. By using VideoView you can play media files from the local storage and also from the internet.


1 Answers

For making a video player you will have to make use of video view. a sample is shown below

Layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<VideoView android:id="@+id/videoView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>

Here i am playing a video stored in "resource/raw" folder , "one" is the name of video file,you can replace it with name of your video file .also make sure that you are going to play an android supported video format

VideoplayerActivitvity

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoplayerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    VideoView videoView =(VideoView)findViewById(R.id.videoView);
    MediaController mediaController= new MediaController(this);
    mediaController.setAnchorView(videoView);        
    Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.one);        
    videoView.setMediaController(mediaController);
    videoView.setVideoURI(uri);        
    videoView.requestFocus();

    videoView.start();


    }
    }   
like image 99
Sandeep Avatar answered Oct 04 '22 03:10

Sandeep