2012-03-01 49 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