2012-03-01 105 views
1

我已經通讀了一些關於視頻播放的教程,發現這個寶石: Android Media Player 這是相當廣泛的比較下面的代碼;使用SurfaceView進行視頻播放有什麼好處,並且有沒有任何示例可以顯示如何實現surface.xml?視頻播放需要surfaceView嗎?

謝謝:)

進口.....

public class VideoViewDemo extends Activity { 

/** 
* TODO: Set the path variable to a streaming video URL or a local media 
* file path. 
*/ 
private String path = ""; 
private VideoView mVideoView; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.videoview); 
    mVideoView = (VideoView) findViewById(R.id.surface_view); 

    if (path == "") { 
     // Tell the user to provide a media file URL/path. 
     Toast.makeText(
       VideoViewDemo.this, 
       "Please edit VideoViewDemo Activity, and set path" 
         + " variable to your media file URL/path", 
       Toast.LENGTH_LONG).show(); 

    } else { 

      /* 
      * Alternatively,for streaming media you can use 
      * mVideoView.setVideoURI(Uri.parse(URLstring)); 
      */ 
      mVideoView.setVideoPath(path); 
      mVideoView.setMediaController(new MediaController(this)); 
      mVideoView.requestFocus(); 

     } 
    } 
} 

回答

1

Commonsware example

佈局的main.xml:

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

    <VideoView 
     android:id="@+id/video" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</LinearLayout> 

VideoDemo.class

public class VideoDemo extends Activity { 
    private VideoView video; 
    private MediaController ctlr; 

    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     getWindow().setFormat(PixelFormat.TRANSLUCENT); 
     setContentView(R.layout.main); 

     File clip=new File(Environment.getExternalStorageDirectory(), 
        "test.mp4"); 

     if (clip.exists()) { 
     video=(VideoView)findViewById(R.id.video); 
     video.setVideoPath(clip.getAbsolutePath()); 

     ctlr=new MediaController(this); 
     ctlr.setMediaPlayer(video); 
     video.setMediaController(ctlr); 
     video.requestFocus(); 
     video.start(); 
     } 
    } 
    } 
+0

謝謝菲利克斯,是否有利用SurfaceView在android網站上顯示你如何在上面編碼?另外我通過ADB連接到我的Logitech revue,如果我使用位於我的計算機上的.mp4文件,它是否仍然可以流式傳輸?或者我需要將該文件移到該修訂。 – Fabii 2012-03-02 01:11:24