SurfaceView是否需要视频播放?

时间:2012-03-01 23:34:11

标签: android video-streaming

我已经阅读了一些关于视频播放的教程并找到了这个宝石: Android Media Player 与下面的代码相比,这是非常广泛的;使用SurfaceView进行视频播放有什么好处,是否有任何示例显示了surface.xml的实现方式?

谢谢:)

进口.....

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 个答案:

答案 0 :(得分: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();
      }
    }
  }