Android Exoplayer 實現多個音視頻文件混合播放以及音軌切換

在之前的文章ExoPlayer中常見MediaSource子類的區別和使用場景中介紹了Exoplayer中各種子MediaSource的使用場景,這篇我們著重詳細介紹下實現多路流混合播放的用法。常見的使用場景有:視頻文件+電影字幕、正片視頻+廣告視頻、背景視頻+背景音樂等。

初始化Exoplayer就不多說了,隨便查查文檔就知道。

ExoPlayer mExoPlayer = new ExoPlayer.Builder(context, new DefaultMediaSourceFactory(context)).build();

1.視頻文件+電影字幕(MergingMediaSource)

Uri videoUri = Uri.parse("https://example.com/video.mp4");
Uri subtitleUri = Uri.parse("https://example.com/subtitles.srt");MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(videoUri);MediaSource subtitleSource = new SingleSampleMediaSource.Factory(dataSourceFactory).createMediaSource(subtitleUri, Format.createTextSampleFormat("subs", MimeTypes.TEXT_SUBRIP, C.SELECTION_FLAG_DEFAULT, "en"));MediaSource mergedSource = new MergingMediaSource(videoSource, subtitleSource);

2.廣告視頻+正片視頻(ConcatenatingMediaSource)

DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(dataSourceFactory);// 創建兩個視頻的 MediaSourceMediaSource video1Source = mediaSourceFactory.createMediaSource(MediaItem.fromUri("asset://android_asset/01.mp4"));MediaSource video2Source = mediaSourceFactory.createMediaSource(MediaItem.fromUri("asset://android_asset/02.mp4"));ConcatenatingMediaSource concatenatingMediaSource = new ConcatenatingMediaSource(video1Source,video2Source);mExoPlayer.setMediaSource(concatenatingMediaSource);

這樣就能讓兩個視頻按順序播放且無縫銜接,若還想它兩循環播放,可用LoopingMediaSource進一步封裝。

//無限循環
LoopingMediaSource loopingMediaSource = new LoopingMediaSource(concatenatingMediaSource); 
//循環5次
LoopingMediaSource loopingMediaSource = new LoopingMediaSource(concatenatingMediaSource,5);
mExoPlayer.setMediaSource(loopingMediaSource);

3.視頻+音頻

DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(dataSourceFactory);MediaSource video2Source = mediaSourceFactory.createMediaSource(MediaItem.fromUri("asset://android_asset/02.mp4"));
MediaSource audio1Source = mediaSourceFactory.createMediaSource(MediaItem.fromUri("asset://android_asset/audio/ori.mp2"));MergingMediaSource audioMerged = new MergingMediaSource(video2Source , audio1Source);
mExoPlayer.setMediaSource(audioMerged );

?上面是1個視頻+1個音頻,當然也可以支持1個視頻+多個音頻,比如電影中有多個不同語言的音軌

DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(dataSourceFactory);MediaSource video1Source = mediaSourceFactory.createMediaSource(MediaItem.fromUri("asset://android_asset/01.mp4"));// 合并兩個音頻源MediaSource audio1Source = mediaSourceFactory.createMediaSource(MediaItem.fromUri("asset://android_asset/audio/ori.mp2"));MediaSource audio2Source = mediaSourceFactory.createMediaSource(MediaItem.fromUri("asset://android_asset/audio/acc.mp2"));MergingMediaSource audioMerged = new MergingMediaSource(audio1Source,audio2Source);
//合并視頻和音頻MergingMediaSource finalSource = new MergingMediaSource(audioMerged,video1Source);mExoPlayer.setMediaSource(finalSource);

這樣就可以實現一個視頻混合多個音軌文件的播放了,那么如何動態切換不同音軌呢?TrackSelector

DefaultTrackSelector trackSelector = new DefaultTrackSelector(context);
mExoPlayer = new ExoPlayer.Builder(context, renderersFactory).setTrackSelector(trackSelector)        .build();//查看所有音軌信息private class PlayerEventListener implements Player.Listener {@SuppressLint("UnsafeOptInUsageError")@Overridepublic void onTracksChanged(Tracks tracks) {audioList.clear();Player.Listener.super.onTracksChanged(tracks);ImmutableList<Tracks.Group> trackGroups = tracks.getGroups();for (int index = 0; index < trackGroups.size(); index++) {Tracks.Group group = trackGroups.get(index);for (int jIndex = 0; jIndex < group.length; jIndex++) {Format format = group.getTrackFormat(jIndex);LOG.info("onTracksChanged format=" + Format.toLogString(format));if (MimeTypes.isAudio(format.sampleMimeType)) {audioList.add(format);}}}currentTrackGroups = mExoPlayer.getCurrentTrackGroups();}mExoPlayer.addListener(new PlayerEventListener());// 用戶選擇第 index 個音軌TrackGroup selectedGroup = null;selectedGroup = currentTrackGroups.get(1); //根據需要選擇第幾個音軌// 應用新音軌mExoPlayer.setTrackSelectionParameters(mExoPlayer.getTrackSelectionParameters().buildUpon().setOverrideForType(new TrackSelectionOverride(selectedGroup, 0))  // 需要切換到的音軌索引.build());

?這里有個問題就是如果視頻和音頻時長不一致,特別是想混合多個音頻和多個視頻時就會出問題,無法播放,報錯如下:

E/ExoPlayerImplInternal(11191): Playback error
E/ExoPlayerImplInternal(11191):   com.google.android.exoplayer2.ExoPlaybackException: Source error
E/ExoPlayerImplInternal(11191):       at com.google.android.exoplayer2.ExoPlayerImplInternal.handleIoException(ExoPlayerImplInternal.java:684)
E/ExoPlayerImplInternal(11191):       at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:660)
E/ExoPlayerImplInternal(11191):       at android.os.Handler.dispatchMessage(Handler.java:98)
E/ExoPlayerImplInternal(11191):       at android.os.Looper.loop(Looper.java:136)
E/ExoPlayerImplInternal(11191):       at android.os.HandlerThread.run(HandlerThread.java:61)
E/ExoPlayerImplInternal(11191):   Caused by: com.google.android.exoplayer2.source.MergingMediaSource$IllegalMergeException
E/ExoPlayerImplInternal(11191):       at com.google.android.exoplayer2.source.MergingMediaSource.onChildSourceInfoRefreshed(MergingMediaSource.java:252)
E/ExoPlayerImplInternal(11191):       at com.google.android.exoplayer2.source.MergingMediaSource.onChildSourceInfoRefreshed(MergingMediaSource.java:52)
E/ExoPlayerImplInternal(11191):       at com.google.android.exoplayer2.source.CompositeMediaSource.lambda$prepareChildSource$0$com-google-android-exoplayer2-source-CompositeMediaSource(CompositeMediaSource.java:120)
E/ExoPlayerImplInternal(11191):       at com.google.android.exoplayer2.source.CompositeMediaSource$$ExternalSyntheticLambda0.onSourceInfoRefreshed(D8$$SyntheticClass:0)
E/ExoPlayerImplInternal(11191):       at com.google.android.exoplayer2.source.BaseMediaSource.refreshSourceInfo(BaseMediaSource.java:94)
E/ExoPlayerImplInternal(11191):       at com.google.android.exoplayer2.source.ConcatenatingMediaSource.updateTimelineAndScheduleOnCompletionActions(ConcatenatingMediaSource.java:746)
E/ExoPlayerImplInternal(11191):       at com.google.android.exoplayer2.source.ConcatenatingMediaSource.handleMessage(ConcatenatingMediaSource.java:716)
E/ExoPlayerImplInternal(11191):       at com.google.android.exoplayer2.source.ConcatenatingMediaSource.$r8$lambda$xvlxaabNVihM68DRWdn_WPenrXk(ConcatenatingMediaSource.java)
E/ExoPlayerImplInternal(11191):       at com.google.android.exoplayer2.source.ConcatenatingMediaSource$$ExternalSyntheticLambda0.handleMessage(D8$$SyntheticClass:0)
E/ExoPlayerImplInternal(11191):       ... 3 more

?這個主要是播放時長不一致,無法同步時序導致,下一篇再討論如何解決此類情況。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/905209.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/905209.shtml
英文地址,請注明出處:http://en.pswp.cn/news/905209.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

推特逆向算法,推特爬蟲,數據分析,推特關鍵詞搜索

祝大家五一假期快樂&#xff01; 最近推特加了逆向&#xff0c;頻繁出現404&#xff0c;無法正常抓取數據&#xff0c;這里給出推特逆向的思路及代碼&#xff0c;供大家參考學習&#xff01; 本文將介紹如何使用 Python 模擬請求 Twitter 的 GraphQL 接口&#xff0c;結合 re…

圖形化編程平臺的破局之道:從工具同質化到生態差異化

一、同質化困局的底層邏輯剖析 在全球圖形化編程市場中&#xff0c;工具功能趨同已成為行業共識。據 Statista 2024 年數據顯示&#xff0c;主流平臺的基礎功能重合度高達 78%&#xff0c;核心模塊&#xff08;如條件判斷、循環結構&#xff09;的實現方式高度相似。這種現象的…

【Rust】枚舉和模式匹配

目錄 枚舉和模式匹配枚舉的定義Option 枚舉控制流運算符 match簡潔控制流 if let 枚舉和模式匹配 枚舉的定義 結構體給予你將字段和數據聚合在一起的方法&#xff0c;像 Rectangle 結構體有 width 和 height 兩個字段。而枚舉給予你一個途徑去聲明某個值是一個集合中的一員。…

應急響應靶機——WhereIS?

用戶名及密碼&#xff1a;zgsf/zgsf 下載資源還有個解題.exe: 1、攻擊者的兩個ip地址 2、flag1和flag2 3、后門程序進程名稱 4、攻擊者的提權方式(輸入程序名稱即可) 之前的命令&#xff1a; 1、攻擊者的兩個ip地址 先獲得root權限&#xff0c;查看一下歷史命令記錄&#x…

變量函數實戰:高保真APP原型“發票頁面”動態交互教程

變量函數是高保真交互原型設計中常見的高級交互功能&#xff0c;能夠避免重復復制與手動修改頁面元素和邏輯標注&#xff0c;讓演示更有真實體驗感。本文分享一個高保真APP交互原型頁面的實操案例&#xff0c;結合原型設計工具中的變量函數與邏輯判斷功能&#xff0c;手把手教你…

量子加密通信:守護信息安全的未來之盾

摘要 在數字化時代&#xff0c;信息安全成為全球關注的焦點。傳統加密技術面臨著被量子計算破解的風險&#xff0c;而量子加密通信作為一種基于量子力學原理的新型加密技術&#xff0c;提供了理論上無條件安全的通信保障。本文將詳細介紹量子加密通信的基本原理、技術實現、應用…

《Vue.js》閱讀之響應式數據與副作用函數

Vue.js 《Vue.js設計與實現》&#xff08;霍春陽&#xff09; 適合&#xff1a;從零手寫Vue3響應式系統&#xff0c;大廠面試源碼題直接覆蓋。重點章節&#xff1a;第4章&#xff08;響應式&#xff09;、第5章&#xff08;渲染器&#xff09;、第8章&#xff08;編譯器&…

數據處理專題(十三)

學會基本的圖像處理技術。? OpenCV 基礎 實踐&#xff1a;使用 OpenCV 進行圖像讀取、顯示和基本處理? 03 代碼示例 1. 導入必要的庫 import cv2import numpy as npimport matplotlib.pyplot as plt 2. 圖像讀取 # 讀取圖像image_path path_to_your_image.jpg # 替換…

springboot旅游小程序-計算機畢業設計源碼76696

目 錄 摘要 1 緒論 1.1研究背景與意義 1.2研究現狀 1.3論文結構與章節安排 2 基于微信小程序旅游網站系統分析 2.1 可行性分析 2.1.1 技術可行性分析 2.1.2 經濟可行性分析 2.1.3 法律可行性分析 2.2 系統功能分析 2.2.1 功能性分析 2.2.2 非功能性分析 2.3 系統…

P1874 快速求和

目錄 題目算法標簽: 動態規劃, 線性 d p dp dp思路代碼 題目 P1874 快速求和 算法標簽: 動態規劃, 線性 d p dp dp 思路 求的是最少組成 n n n的加法次數, 對于當前數字序列可以設計狀態表示 f [ i ] [ j ] f[i][j] f[i][j]表示考慮前 i i i個字符, 并且和是 j j j的所有方…

知名人工智能AI培訓公開課內訓課程培訓師培訓老師專家咨詢顧問唐興通AI在金融零售制造業醫藥服務業創新實踐應用

AI賦能未來工作&#xff1a;引爆效率與價值創造的實戰營 AI驅動的工作革命&#xff1a;從效率提升到價值共創 培訓時長&#xff1a; 本課程不僅是AI工具的操作指南&#xff0c;更是面向未來的工作方式升級羅盤。旨在幫助學員系統掌握AI&#xff08;特別是生成式AI/大語言模型…

Linux 內核參數

文章目錄 什么是內核參數參數種類配置方式1. 編譯內核時配置2. 內核啟動時配置3. 內核運行時配置4. 加載內核模塊時配置總結 什么是內核參數 內核參數是 Linux 系統中用于控制和調整內核行為的可配置選項。這些參數影響系統的性能、安全性和各種功能特性。 參數種類 大部分參…

pythonocc 拉伸特征

micromamba install -c conda-forge pythonocc-core opencascade.js安裝不起來&#xff0c;ai用pythonocc練個手 拉伸線框 線成面 from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Vec from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire f…

Vue.js 頁面切換空白與刷新 404 問題深度解析

在使用 Vue.js 開發單頁應用 (SPA) 的過程中&#xff0c;開發者經常會遇到兩個常見問題&#xff1a;頁面切換時出現短暫的空白屏幕&#xff0c;以及刷新頁面時返回 404 錯誤。這兩個問題不僅影響用戶體驗&#xff0c;還可能阻礙項目的正常上線。本文將深入探討這兩個問題的成因…

Go 語言 slice(切片) 的使用

序言 在許多開發語言中&#xff0c;動態數組是必不可少的一個組成部分。在實際的開發中很少會使用到數組&#xff0c;因為對于數組的大小大多數情況下我們是不能事先就確定好的&#xff0c;所以他不夠靈活。動態數組通過提供自動擴容的機制&#xff0c;極大地提升了開發效率。這…

Qt5.14.2 鏈接 MySQL 8.4 遇到的問題

問題一: "Plugin caching_sha2_password could not be loaded: 找不到指定的模塊。 Library path is caching_sha2_password.dll QMYSQL: Unable to connect" 解決方法: alter user root@localhost identified with mysql_native_password by root;問題二: ERR…

Docker 部署 - Crawl4AI 文檔 (v0.5.x)

Docker 部署 - Crawl4AI 文檔 (v0.5.x) 快速入門 &#x1f680; 拉取并運行基礎版本&#xff1a; # 不帶安全性的基本運行 docker pull unclecode/crawl4ai:basic docker run -p 11235:11235 unclecode/crawl4ai:basic# 帶有 API 安全性啟用的運行 docker run -p 11235:1123…

開發工具分享: Web前端編碼常用的在線編譯器

1.OneCompiler 工具網址&#xff1a;https://onecompiler.com/ OneCompiler支持60多種編程語言&#xff0c;在全球有超過1280萬用戶&#xff0c;讓開發者可以輕易實現代碼的編寫、運行和共享。 OneCompiler的線上調試功能完全免費&#xff0c;對編程語言的覆蓋也很全&#x…

Docker-配置私有倉庫(Harbor)

配置私有倉庫&#xff08;Harbor&#xff09; 一、環境準備安裝 Docker 三、安裝docker-compose四、準備Harbor五、配置證書六、部署配置Harbor七、配置啟動服務八、定制本地倉庫九、測試本地倉庫 Harbor(港灣)&#xff0c;是一個用于 存儲 和 分發 Docker 鏡像的企業級 Regi…

關于高并發GIS數據處理的一點經驗分享

1、背景介紹 筆者過去幾年在參與某個大型央企的項目開發過程中,遇到了十分棘手的難題。其與我們平常接觸的項目性質完全不同。在一般的項目中,客戶一般只要求我們能夠通過桌面軟件對原始數據進行加工處理,將各類地理信息數據加工處理成地圖/場景和工作空間,然后再將工作空…