除了使用 MediaRecorder 可以進行錄影和錄音,支援捕獲和編碼各種常見的音訊和視頻格式。MediaPlayer 還可以播放音樂和視頻。首先是編寫一個最簡單的 MediaPlayer 播放視頻程式(MediaPlayer Video Program),試驗 MediaPlayer 播放視頻是否可用?
Android MediaPlayer 播放視頻程式(MediaPlayer Video Program) |
- 操作系統:Windows 7 64-bit 版本
- 開發環境:Android Studio 4.0.1 版本
- 原程式:C:\Development\Development_Android\Android_Project\DIY-Android-141-15 MediaPlayer Video Bugworkshop
- 程式:C:\Development\Development_Android\Android_Project\DIY-Android-141-15 MediaPlayer Video Bugworkshop
- 測試環境:Vivo(維沃)X5Max Android 4.4.4(KitKat)版本
- 設定 MediaPlayer 參數
- 設置 Button - Play Pause Stop 參數
- 加入 Video mp4 檔案在 raw 內
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">
<SurfaceView android:id="@+id/sfv_show" android:layout_width="411dp" android:layout_height="296dp" tools:ignore="MissingConstraints" />
<Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginLeft="20dp" android:layout_marginTop="124dp" android:text="Start" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/sfv_show" />
<Button android:id="@+id/btn_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginLeft="30dp" android:layout_marginTop="124dp" android:text="Pause" app:layout_constraintStart_toEndOf="@+id/btn_start" app:layout_constraintTop_toBottomOf="@+id/sfv_show" />
<Button android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginLeft="30dp" android:layout_marginTop="124dp" android:text="Stop" app:layout_constraintStart_toEndOf="@+id/btn_pause" app:layout_constraintTop_toBottomOf="@+id/sfv_show" />
</androidx.constraintlayout.widget.ConstraintLayout> |
MainActivity.java:
package bugworkshop.blogspot.com.mediaplayervideo;
import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, SurfaceHolder.Callback{
private MediaPlayer mPlayer = null; private SurfaceView sfv_show; private SurfaceHolder surfaceHolder; private Button btn_start; private Button btn_pause; private Button btn_stop;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
sfv_show = (SurfaceView) findViewById(R.id.sfv_show); btn_start = (Button) findViewById(R.id.btn_start); btn_pause = (Button) findViewById(R.id.btn_pause); btn_stop = (Button) findViewById(R.id.btn_stop);
btn_start.setOnClickListener(this); btn_pause.setOnClickListener(this); btn_stop.setOnClickListener(this);
surfaceHolder = sfv_show.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setFixedSize(320, 220); }
@Override protected void onDestroy() { super.onDestroy(); if (mPlayer.isPlaying()) { mPlayer.stop(); } mPlayer.release(); }
@Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_start: mPlayer.start(); break; case R.id.btn_pause: mPlayer.pause(); break; case R.id.btn_stop: mPlayer.stop(); break; } }
@Override public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) { mPlayer = MediaPlayer.create(MainActivity.this, R.raw.rover); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mPlayer.setDisplay(surfaceHolder); }
@Override public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) { }
@Override public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) { }
} |
沒有留言:
張貼留言