2011-05-16 241 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

相關問題