SurfaceView необходим для воспроизведения видео? - PullRequest
1 голос
/ 02 марта 2012

Я прочитал несколько руководств по воспроизведению видео и нашел этот драгоценный камень: 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 Ответ

1 голос
/ 02 марта 2012

Commonsware пример :

Макет 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();
      }
    }
  }
...