2011-05-16 192 views
0

有人能告诉我什么是我应该添加到我当前的代码播放视频的具体代码?如何在点击“显示视频”按钮后播放视频?

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

     } 
} 

回答

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

     } 
} 

我希望这有助于..

+0

,所以我应该把这个代码进入ViewVideo.class? – misery 2011-05-16 03:09:26

+0

你可以通过传递这些参数来调用这个方法:path,filename和autoplay ..你可以在onCreate(Bundle savedInstanceState)之后放置这个方法.. – CMA 2011-05-16 07:02:49

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

    } 

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

希望这有助于你..

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

    } 
} 
+0

@ user755060你能告诉我如何在视图之后离开viewVideo活动视频已完成播放视频?上面编辑 – misery 2011-05-18 02:55:42

相关问题