Android TextureView實現視頻播放的方法

一、什麼是TextureView

TextureView是Android 4.0(API level 14)引入的一個新的控制項,它可以在UI線程之外維護一個可渲染的surface,這允許我們可以將播放視頻這樣複雜的任務放到子線程中去執行,從而提高程序的性能。相較於SurfaceView,在TextureView中我們可以使用TextureView.setTransform()方法來對視頻進行旋轉、移動等的變換操作。但是使用TextureView也有一些限制,比如只有在硬體加速開啟的情況下才有效,並且在TextureView上不能覆蓋其他View。

二、使用TextureView播放視頻

我們還需要使用MediaPlayer來進行視頻的播放。在UI布局文件中,我們可以如下聲明TextureView和一個Button控制項:

    <TextureView
        android:id="@+id/textureView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/startBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="開始播放" />

在Java代碼中,我們可以通過如下代碼來初始化MediaPlayer和TextureView:

    private MediaPlayer mediaPlayer;
    private TextureView textureView;
    private Button startBtn;
    
    private void initView() {
        textureView = findViewById(R.id.textureView);
        startBtn = findViewById(R.id.startBtn);
        
        // 初始化MediaPlayer並設置監聽器
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();  // 開始播放視頻
            }
        });
        
        // 設置TextureView的監聽器
        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                mediaPlayer.setSurface(new Surface(surface));
                try {
                    mediaPlayer.setDataSource("video.mp4");  // 視頻文件的路徑
                    mediaPlayer.prepareAsync();  // 非同步載入視頻
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
            }
            
            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                mediaPlayer.stop();  // 銷毀TextureView時停止播放視頻
                mediaPlayer.release();
                return true;
            }
            
            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {
            }
        });
        
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.start();  // 點擊按鈕開始播放
            }
        });
    }

三、將TextureView與RecyclerView聯動

在某些場景下,我們需要將TextureView與RecyclerView聯動,實現類似於社交軟體中的視頻滾動瀏覽效果。

在我們的RecyclerView的Adapter中,需要為每個TextureView設置一個SurfaceTextureListener。在onBindViewHolder()方法中,我們可以創建一個新的MediaPlayer,為它設置數據源和回調監聽,以及為TextureView設置SurfaceTextureListener。在onViewRecycled()方法中,我們需要釋放MediaPlayer所佔用的資源。

public class VideoAdapter extends RecyclerView.Adapter<VideoViewHolder> {

    private List<String> videoList;
    private MediaPlayer mediaPlayer;
    private TextureView.SurfaceTextureListener surfaceTextureListener;

    public VideoAdapter(List<String> videoList) {
        this.videoList = videoList;
        mediaPlayer = new MediaPlayer();
        surfaceTextureListener = createSurfaceTextureListener();
    }

    @NonNull
    @Override
    public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 創建ViewHolder
    }

    @Override
    public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
        TextureView textureView = holder.getTextureView();
        textureView.setSurfaceTextureListener(surfaceTextureListener);
        String videoUrl = videoList.get(position);
        try {
            mediaPlayer.setDataSource(videoUrl);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onViewRecycled(@NonNull VideoViewHolder holder) {
        TextureView textureView = holder.getTextureView();
        mediaPlayer.stop();
        mediaPlayer.reset();
        textureView.setSurfaceTextureListener(null);
    }

    @Override
    public int getItemCount() {
        return videoList.size();
    }

    private TextureView.SurfaceTextureListener createSurfaceTextureListener() {
        return new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                mediaPlayer.setSurface(new Surface(surface));
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        };
    }
}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/293075.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-25 18:39
下一篇 2024-12-25 18:39

相關推薦

  • 解決.net 6.0運行閃退的方法

    如果你正在使用.net 6.0開發應用程序,可能會遇到程序閃退的情況。這篇文章將從多個方面為你解決這個問題。 一、代碼問題 代碼問題是導致.net 6.0程序閃退的主要原因之一。首…

    編程 2025-04-29
  • ArcGIS更改標註位置為中心的方法

    本篇文章將從多個方面詳細闡述如何在ArcGIS中更改標註位置為中心。讓我們一步步來看。 一、禁止標註智能調整 在ArcMap中設置標註智能調整可以自動將標註位置調整到最佳顯示位置。…

    編程 2025-04-29
  • Python中init方法的作用及使用方法

    Python中的init方法是一個類的構造函數,在創建對象時被調用。在本篇文章中,我們將從多個方面詳細討論init方法的作用,使用方法以及注意點。 一、定義init方法 在Pyth…

    編程 2025-04-29
  • Python創建分配內存的方法

    在python中,我們常常需要創建並分配內存來存儲數據。不同的類型和數據結構可能需要不同的方法來分配內存。本文將從多個方面介紹Python創建分配內存的方法,包括列表、元組、字典、…

    編程 2025-04-29
  • 使用Vue實現前端AES加密並輸出為十六進位的方法

    在前端開發中,數據傳輸的安全性問題十分重要,其中一種保護數據安全的方式是加密。本文將會介紹如何使用Vue框架實現前端AES加密並將加密結果輸出為十六進位。 一、AES加密介紹 AE…

    編程 2025-04-29
  • 用不同的方法求素數

    素數是指只能被1和自身整除的正整數,如2、3、5、7、11、13等。素數在密碼學、計算機科學、數學、物理等領域都有著廣泛的應用。本文將介紹幾種常見的求素數的方法,包括暴力枚舉法、埃…

    編程 2025-04-29
  • Python中讀入csv文件數據的方法用法介紹

    csv是一種常見的數據格式,通常用於存儲小型數據集。Python作為一種廣泛流行的編程語言,內置了許多操作csv文件的庫。本文將從多個方面詳細介紹Python讀入csv文件的方法。…

    編程 2025-04-29
  • Python學習筆記:去除字元串最後一個字元的方法

    本文將從多個方面詳細闡述如何通過Python去除字元串最後一個字元,包括使用切片、pop()、刪除、替換等方法來實現。 一、字元串切片 在Python中,可以通過字元串切片的方式來…

    編程 2025-04-29
  • 用法介紹Python集合update方法

    Python集合(set)update()方法是Python的一種集合操作方法,用於將多個集合合併為一個集合。本篇文章將從以下幾個方面進行詳細闡述: 一、參數的含義和用法 Pyth…

    編程 2025-04-29
  • Vb運行程序的三種方法

    VB是一種非常實用的編程工具,它可以被用於開發各種不同的應用程序,從簡單的計算器到更複雜的商業軟體。在VB中,有許多不同的方法可以運行程序,包括編譯器、發布程序以及命令行。在本文中…

    編程 2025-04-29

發表回復

登錄後才能評論