歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用開發之視頻播放器

Android應用開發之視頻播放器

日期:2017/3/1 10:27:59   编辑:Linux編程

資源:

ImageButton所用圖片4張

Strings:
  1. <string name="app_name">MyVideoPlayer</string>
  2. <string name="video_text">視頻文件</string>
  3. <string name="notfoundsdcard_error">找不到Sd卡</string>
  4. <string name="notfoundfile_error">找不到視頻文件</string>

布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#FFFFCC"
  6. android:orientation="vertical" >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/video_text" />
  11. <EditText
  12. android:id="@+id/videoFileEt"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text="cat.3gp" />
  16. <TableLayout
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:gravity="center"
  20. android:stretchColumns="*" >
  21. <TableRow >
  22. <ImageButton
  23. android:id="@+id/playBtn"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:src="@drawable/play" />
  27. <ImageButton
  28. android:id="@+id/pauseBtn"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:src="@drawable/pause" />
  32. <ImageButton
  33. android:id="@+id/resetBtn"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:src="@drawable/reset" />
  37. <ImageButton
  38. android:id="@+id/stopBtn"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:src="@drawable/stop" />
  42. </TableRow>
  43. </TableLayout>
  44. <SurfaceView
  45. android:id="@+id/surfaceView"
  46. android:layout_width="fill_parent"
  47. android:layout_height="240dip" />
  48. </LinearLayout>

Activity

  1. package cn.class3g.videoplayer;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import android.app.Activity;
  5. import android.media.AudioManager;
  6. import android.media.MediaPlayer;
  7. import android.os.Bundle;
  8. import android.os.Environment;
  9. import android.util.Log;
  10. import android.view.SurfaceHolder;
  11. import android.view.SurfaceHolder.Callback;
  12. import android.view.SurfaceView;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.EditText;
  16. import android.widget.ImageButton;
  17. import android.widget.Toast;
  18. public class VideoPlayerActivity extends Activity implements OnClickListener {
  19. private static final String TAG = "VideoPlayerActivity";
  20. private ImageButton playBtn, pauseBtn, resetBtn, stopBtn;
  21. private EditText fileEt;
  22. private SurfaceView videoSv;
  23. private MediaPlayer mediaPlayer;
  24. private SurfaceHolder holder;
  25. private int position = 0;
  26. File videoFile = null;
  27. public void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.main);
  30. findViews();
  31. mediaPlayer = new MediaPlayer();
  32. }
  33. private void findViews() {
  34. fileEt = (EditText) this.findViewById(R.id.videoFileEt);
  35. playBtn = (ImageButton) this.findViewById(R.id.playBtn);
  36. pauseBtn = (ImageButton) this.findViewById(R.id.pauseBtn);
  37. resetBtn = (ImageButton) this.findViewById(R.id.resetBtn);
  38. stopBtn = (ImageButton) this.findViewById(R.id.stopBtn);
  39. playBtn.setOnClickListener(this);
  40. pauseBtn.setOnClickListener(this);
  41. resetBtn.setOnClickListener(this);
  42. stopBtn.setOnClickListener(this);
  43. videoSv = (SurfaceView) this.findViewById(R.id.surfaceView);
  44. holder = videoSv.getHolder();
  45. holder.setFixedSize(176, 144); // 設置分辨率
  46. /* 下面設置Surface不維護自己的緩沖區,而是等待屏幕的渲染引擎將內容推送到用戶面前 */
  47. holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  48. //holder.addCallback(new SurfaceCallback());
  49. }
  50. public void onClick(View v) {
  51. if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) {
  52. String fileName = fileEt.getText().toString().trim();
  53. videoFile = new File(
  54. Environment.getExternalStorageDirectory(), fileName);
  55. if (videoFile.exists()) {
  56. try {
  57. switch (v.getId()) {
  58. case R.id.playBtn:
  59. playVideo(videoFile);
  60. break;
  61. case R.id.pauseBtn:
  62. // if(mediaPlayer.isPlaying()){
  63. // mediaPlayer.pause();
  64. // }else{
  65. // mediaPlayer.start();
  66. // }
  67. if(mediaPlayer.isPlaying()){
  68. position = mediaPlayer.getCurrentPosition();
  69. mediaPlayer.pause();
  70. }
  71. break;
  72. case R.id.resetBtn:
  73. if(mediaPlayer.isPlaying()){
  74. mediaPlayer.seekTo(0);
  75. }else{
  76. playVideo(videoFile);
  77. }
  78. break;
  79. case R.id.stopBtn:
  80. if(mediaPlayer.isPlaying()){
  81. mediaPlayer.stop();
  82. }
  83. break;
  84. }
  85. } catch (Exception e) {
  86. Log.e(TAG, e.toString());
  87. }
  88. } else {
  89. showToast(R.string.notfoundfile_error);
  90. }
  91. } else {
  92. showToast(R.string.notfoundsdcard_error);
  93. }
  94. }
  95. private void playVideo(File videoFile) throws IOException {
  96. mediaPlayer.reset();
  97. mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  98. mediaPlayer.setDataSource(videoFile.getAbsolutePath());
  99. mediaPlayer.setDisplay(holder);
  100. mediaPlayer.prepare();
  101. mediaPlayer.start();
  102. }
  103. private void showToast(int resId) {
  104. Toast.makeText(this, resId, 1).show();
  105. }
  106. }
Copyright © Linux教程網 All Rights Reserved