如何在Android TextureView中播放影片?


此示例演示瞭如何在Android TextureView中播放影片。

步驟1 −在Android Studio中建立新專案,轉到檔案⇒新建專案並填寫所有必需的詳細資訊以新建專案。

步驟2 −將以下程式碼新增到res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<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"
   android:padding="8dp"
   tools:context=".MainActivity">
<TextureView
   android:id="@+id/textureView"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>
</LinearLayout>

步驟3 −建立資產資料夾並將影片複製貼上到資產資料夾中。

步驟4 −將以下程式碼新增到src/MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.content.res.AssetFileDescriptor;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.view.Surface;
import android.view.TextureView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener, MediaPlayer.OnVideoSizeChangedListener {
   TextureView textureView;
   private MediaPlayer mediaPlayer;
   AssetFileDescriptor fileDescriptor;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textureView = findViewById(R.id.textureView);
      textureView.setSurfaceTextureListener(this);
      mediaPlayer = new MediaPlayer();
      try {
         fileDescriptor = getAssets().openFd("videoplayback.mp4");
      }
       catch (IOException e) {
         e.printStackTrace();
      }
   }
   @Override
   public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
      Surface surface = new Surface(surfaceTexture);
      try {
         mediaPlayer.setSurface(surface);
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mediaPlayer.setDataSource(fileDescriptor);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
               @Override
               public void onPrepared(MediaPlayer mp) {
                  mediaPlayer.start();
               }
            });
         }
      }
      catch (IOException e) {
         e.printStackTrace();
      }
   }
   @Override
   public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
   }
   @Override
   public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
      return false;
   }
   @Override
   public void onSurfaceTextureUpdated(SurfaceTexture surface) {
   }
   @Override
   public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
   }
}

步驟5 −將以下程式碼新增到androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.com.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

讓我們嘗試執行您的應用程式。我假設您已經將您的實際Android移動裝置與您的計算機連線。要從Android Studio執行應用程式,請開啟您的一個專案活動檔案,然後單擊工具欄中的執行播放圖示 圖示。選擇您的移動裝置作為選項,然後檢視將顯示您預設螢幕的移動裝置−

更新於:2020年7月8日

1K+瀏覽量

開啟您的 職業

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.