Android
 Computer >> コンピューター >  >> プログラミング >> Android

Android TextureViewでビデオを再生する方法は?


この例は、AndroidTextureViewで動画を再生する方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://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="https://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からアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、[実行]をクリックします ツールバーのAndroid TextureViewでビデオを再生する方法は? アイコン。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-

Android TextureViewでビデオを再生する方法は?


  1. Android Phone でアプリをサイドロードする方法

    Android の最も優れた点は、たくさんのエキサイティングなアプリから選択できることです。 Play ストアだけでも何百万ものアプリが利用できます。 Android スマートフォンでどのようなタスクを実行しようとしても、Play ストアには少なくとも 10 種類のアプリが用意されています。これらのアプリはすべて、Android が最もカスタマイズ可能なオペレーティング システムの称号を獲得する上で重要な役割を果たしています。 Android のユーザー エクスペリエンスを他とは異なる方法で独自のものにするのは、デバイスにインストールされている一連のアプリです。 しかし、話はここで終わりませ

  2. Android Phone でビデオを安定させる方法

    携帯電話で録画したビデオを FPS (フレーム/秒) で測定できます。 FPS が高ければ高いほど、ビデオの品質は向上します。ただし、ビデオの録画中は携帯電話を安定させておくことが不可欠です。 Android フォンに高品質のカメラが搭載されている場合でも、ビデオを録画するときに電話が安定していないと、ビデオはうまくいきません。誰もがどこにでも三脚を持っているわけではないため、移動中の車やランニング中に記録したビデオは、品質を共有したり歪めたりする可能性があります。 したがって、あなたを助けるために、私たちは小さなガイドをここに持っています Android Phone で動画を安定させる方法