Android में TextureView पर वीडियो प्ले करने का पूरा गाइड
TextureView का उपयोग करके वीडियो चलाना SurfaceView से अधिक लचीला है क्योंकि यह व्यू हायरार्की में ठीक से एनिमेट, स्केल और ट्रांसफॉर्म हो सकता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 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: एसेट्स फोल्डर में वीडियो रखें
res → New → Folder → Assets Folder बनाएं और उसमें अपना वीडियो फाइल (जैसे videoplayback.mp4) कॉपी करें।
चरण 4: 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(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) {}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
चरण 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>
महत्वपूर्ण नोट्स
- हमेशा
onDestroy()मेंmediaPlayer.release()कॉल करें। - बड़ी फाइलों के लिए एसेट्स के बजाय रॉ फोल्डर या स्ट्रीमिंग URL बेहतर है।
- आधुनिक ऐप्स में ExoPlayer (Media3) उपयोग करने की सलाह दी जाती है।