如何在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运行应用程序,请打开您的一个项目活动文件,然后单击工具栏中的运行 图标。选择您的移动设备作为选项,然后查看将显示您默认屏幕的移动设备−
广告