misery
misery

Reputation: 13

How to play video after onClick on "Show Video" button?

Can someone tell me what is the specific code i should add into my current codes to play the video?

public class ViewVideo extends Activity {
      private String filename;
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Bundle extras = getIntent().getExtras();
            filename = extras.getString("videoPath");

            VideoView viewVideo = new VideoView(this);
            setContentView(viewVideo);
            viewVideo.setVideoPath(filename);
            viewVideo.setMediaController(new MediaController(this));
            viewVideo.requestFocus();
            viewVideo.start();

      }
}

Upvotes: 0

Views: 5897

Answers (3)

Karthik
Karthik

Reputation: 391

Try this code i think this will help you

package com.VideoPlayer;

import android.app.Activity;
import android.media.*;
import android.os.Bundle;
import android.widget.*;
import android.view.*;

public class myPlayer extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */
    Button play,stop,sd;
    MediaPlayer mp;
    String path;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        play=(Button)findViewById(R.id.play);
        stop=(Button)findViewById(R.id.stop);
        sd=(Button)findViewById(R.id.sd);
        play.setOnClickListener(this);
        stop.setOnClickListener(this);
        sd.setOnClickListener(this);
        //mp=MediaPlayer.create(this, R.raw.sample);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v==play)
        {
             if( mp.isPlaying()==false && mp!=null)
             {

                      mp.start();
             }
        }
        if(v==stop)
        {
            mp.stop();
            mp.release();
            mp=null;
        }
        if(v==sd)
        {
            path="/sdcard/sample.mp4";
            try{
            mp=new MediaPlayer();
            mp.setDataSource(path);
            mp.prepare();
            mp.start();
            }
            catch( Exception e)
            {
                Toast.makeText(this,e.toString(),Toast.LENGTH_LONG)
                .show();
            }
        }

    }
}

Upvotes: 0

Krishna
Krishna

Reputation: 1474

Try this...

public void videoPlayer(String path, String fileName, boolean autoplay){
     //get current window information, and set format, set it up differently, if you need some special effects
     getWindow().setFormat(PixelFormat.TRANSLUCENT); 
     //the VideoView will hold the video
     VideoView videoHolder = new VideoView(this); 
     //MediaController is the ui control howering above the video (just like in the default youtube player).
     videoHolder.setMediaController(new MediaController(this)); 
     //assing a video file to the video holder
     videoHolder.setVideoURI(Uri.parse(path+"/"+fileName)); 
     //get focus, before playing the video.
     videoHolder.requestFocus(); 
     if(autoplay){
         videoHolder.start(); 
     }

  }

Call this method on button click by passing the required arguments ( video file path, file name, autoplay ).

Hope this helps you..

Upvotes: 0

CMA
CMA

Reputation: 2808

there is more than a way to play media files in android..

try this one:

public void videoPlayer(String path, String fileName, boolean autoplay){

    getWindow().setFormat(PixelFormat.TRANSLUCENT);

      VideoView videoHolder = new VideoView(this);

      videoHolder.setMediaController(new MediaController(this));

      videoHolder.setVideoURI(Uri.parse(path+"/"+fileName));

      videoHolder.requestFocus();


      if(autoplay){

              videoHolder.start();

      }
}

i hope this helps..

Upvotes: 1

Related Questions