点击“显示视频”按钮后如何播放视频?

时间:2011-05-16 02:46:08

标签: android

有人可以告诉我应该添加到当前代码中的特定代码以播放视频吗?

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

      }
}

3 个答案:

答案 0 :(得分:1)

在Android中播放媒体文件不仅仅是一种方式..

试试这个:

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

      }
}

我希望这会有所帮助..

答案 1 :(得分:0)

试试这个......

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

  }

通过传递所需的参数(视频文件路径,文件名,自动播放),在按钮单击时调用此方法。

希望这可以帮助你..

答案 2 :(得分:0)

尝试此代码,我认为这会对您有所帮助

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

    }
}