VideoView
主程序
package com.test.myplayvideo;
import android.support.v4.os.EnvironmentCompat;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore.Video;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity implements OnClickListener{
private Button mButtonPlay;
private VideoView mVideoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonPlay=(Button) findViewById(R.id.button_play_video);
mButtonPlay.setOnClickListener(this);
mVideoView=(VideoView) findViewById(R.id.video_view);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_play_video:
mVideoView.setVideoPath(Environment.getExternalStorageDirectory() + "/netease/cloudmusic/MV/aa.mp4");
mVideoView.setMediaController(new MediaController(MainActivity.this));
mVideoView.start();
break;
default:
break;
}
}
}
佈局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button_play_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放" />
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
SurfaceView
主程序
package com.test.mymediasurfaceview;
import android.support.v7.app.ActionBarActivity;
import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore.Audio;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button mButtonPlay;
private SurfaceView mSurfaceView;
private MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonPlay = (Button) findViewById(R.id.button_surface);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
mButtonPlay.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_surface:
if (player == null) {
player = new MediaPlayer();
}
player.reset();
try {
player.setDataSource(Environment.getExternalStorageDirectory() + "/netease/cloudmusic/MV/aa.mp4");
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDisplay(mSurfaceView.getHolder());
player.prepare();
player.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
}
佈局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.test.mymediasurfaceview.MainActivity" >
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="@+id/button_surface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
/>
</RelativeLayout>