FFmpeg+javacpp中純音頻播放

FFmpeg+javacpp中純音頻播放

  • 1. Java Sound播放
  • 2、整合音頻信息AudioInfo
  • 3、添加ExecutorService執行播放

FFmpeg+javacpp+javacv使用
FFmpeg+javacpp中FFmpegFrameGrabber
FFmpeg+javacpp中仿ffplay播放

JavaCV 1.5.12 API
JavaCPP Presets for FFmpeg 7.1.1-1.5.12 API

1. Java Sound播放

如FFmpeg+javacpp中仿ffplay播放中 2.1 grabSamples() 播放案例
gitee: xhbruce/JavacvAddFFmpegDemo

public class AudioPlayer implements Runnable {private final String filePath;public AudioPlayer(String filePath) {this.filePath = filePath;//AvLog.close();}public void playAudio() throws Exception {FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(filePath);grabber.start();int audioBitrate = grabber.getAudioBitrate(); // 獲取音頻流的比特率(bitrate)信息int audioChannels = grabber.getAudioChannels(); // 獲取當前音頻流的通道數量(聲道數)int audioCodec = grabber.getAudioCodec(); //String audioCodecName = grabber.getAudioCodecName(); // 獲取當前音頻流所使用的音頻編碼器(Codec)名稱double audioFrameRate = grabber.getAudioFrameRate(); //Map<String, String> audioMetadata = grabber.getAudioMetadata(); //Map<String, Buffer> audioSideData = grabber.getAudioSideData(); //int lengthInAudioFrames = grabber.getLengthInAudioFrames(); // 獲取音頻流的總幀數(以音頻幀為單位)boolean hasAudio = grabber.hasAudio(); // 是否有音頻流int sampleFormat = grabber.getSampleFormat(); // 獲取音頻采樣格式(Sample Format)int sampleRate = grabber.getSampleRate(); // 獲取音頻流的采樣率(Sample Rate)XLog.i("audioBitrate: " + audioBitrate + ", audioChannels: " + audioChannels + ", audioCodec: " + audioCodec + ", audioCodecName: " + audioCodecName+ ", audioFrameRate: " + audioFrameRate + ", audioMetadata=" + audioMetadata + ", audioSideData=" + audioSideData + ", lengthInAudioFrames: " + lengthInAudioFrames+ ", hasAudio=" + hasAudio + ", sampleFormat: " + sampleFormat + ",sampleRate: " + sampleRate);AudioFormat audioFormat = AudioUtil.toAudioFormat(sampleFormat, sampleRate, audioChannels);XLog.d("audioFormat: " + audioFormat);DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);SourceDataLine audioLine = (SourceDataLine) AudioSystem.getLine(info);audioLine.open(audioFormat);audioLine.start();Frame frame;long totalDurationMs = grabber.getLengthInTime(); // 獲取總時長(毫秒)XLog.d("音頻總時長: " + totalDurationMs + " ms -- " + AudioUtil.getDurationString(totalDurationMs));while ((frame = grabber.grabSamples()) != null) {if (frame.samples == null || frame.samples.length == 0) continue;Buffer buffer = frame.samples[0];byte[] audioBytes = AudioUtil.toByteArrayApplySourceDataLine(buffer);audioLine.write(audioBytes, 0, audioBytes.length);}audioLine.drain();audioLine.stop();audioLine.close();grabber.stop();}@Overridepublic void run() {try {playAudio();} catch (Exception e) {throw new RuntimeException(e);}}public static void main(String[] args) {String musicUrl = "F:\\Music\\Let Me Down Slowly.mp3";
//        String musicUrl = "F:\\Music\\張碧晨 - 涼涼.flac";
//        String musicUrl = "F:\\Music\\愛得比你深 - 張學友.ape";new Thread(new AudioPlayer(musicUrl)).start();}
}

2、整合音頻信息AudioInfo

audioBitrate = grabber.getAudioBitrate(); // 獲取音頻流的比特率(bitrate)信息
audioChannels = grabber.getAudioChannels(); // 獲取當前音頻流的通道數量(聲道數)
audioCodec = grabber.getAudioCodec(); //
audioCodecName = grabber.getAudioCodecName(); // 獲取當前音頻流所使用的音頻編碼器(Codec)名稱
audioFrameRate = grabber.getAudioFrameRate(); //
metadata = new Metadata(grabber.getMetadata()); //
audioMetadata = grabber.getAudioMetadata(); //
audioSideData = grabber.getAudioSideData(); //
lengthInAudioFrames = grabber.getLengthInAudioFrames(); // 獲取音頻流的總幀數(以音頻幀為單位)
sampleFormat = grabber.getSampleFormat(); // 獲取音頻采樣格式(Sample Format)
sampleRate = grabber.getSampleRate(); // 獲取音頻流的采樣率(Sample Rate)
ext = grabber.getFormat(); //grabber.getFormatContext().iformat().name().getString();
totalDurationMs = grabber.getLengthInTime(); // 獲取總時長(毫秒)

https://gitee.com/xhbruce/JavacvAddFFmpegDemo/blob/master/src/main/java/org/xhbruce/ffmpeg/info/AudioInfo.java

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.xhbruce.utils.AudioUtil;import java.nio.Buffer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;/*** @author xhbruce*/
public class AudioInfo {private static final String TAG = "AudioInfo";public FFmpegFrameGrabber grabber;public Path filePath;public String ext; //文件格式public int audioBitrate;public int audioChannels;public int audioCodec;public String audioCodecName;public double audioFrameRate;Metadata metadata;// tag信息Map<String, String> audioMetadata;Map<String, Buffer> audioSideData;public int lengthInAudioFrames;public int sampleFormat;public int sampleRate;public long totalDurationMs;public long startTime;public AudioInfo(String url) {grabber = new FFmpegFrameGrabber(url);filePath = Paths.get(url);try {init();} catch (FFmpegFrameGrabber.Exception e) {throw new RuntimeException(e);}}private void init() throws FFmpegFrameGrabber.Exception {grabber.start();if (grabber.hasAudio()/*是否有音頻流*/) {audioBitrate = grabber.getAudioBitrate(); // 獲取音頻流的比特率(bitrate)信息audioChannels = grabber.getAudioChannels(); // 獲取當前音頻流的通道數量(聲道數)audioCodec = grabber.getAudioCodec(); //audioCodecName = grabber.getAudioCodecName(); // 獲取當前音頻流所使用的音頻編碼器(Codec)名稱audioFrameRate = grabber.getAudioFrameRate(); //metadata = new Metadata(grabber.getMetadata()); //audioMetadata = grabber.getAudioMetadata(); //audioSideData = grabber.getAudioSideData(); //lengthInAudioFrames = grabber.getLengthInAudioFrames(); // 獲取音頻流的總幀數(以音頻幀為單位)sampleFormat = grabber.getSampleFormat(); // 獲取音頻采樣格式(Sample Format)sampleRate = grabber.getSampleRate(); // 獲取音頻流的采樣率(Sample Rate)ext = grabber.getFormat(); //grabber.getFormatContext().iformat().name().getString();totalDurationMs = grabber.getLengthInTime(); // 獲取總時長(毫秒)startTime = grabber.getFormatContext().start_time();}grabber.stop();}public String getMetadata(String key) {return metadata.getValue(key);}@Overridepublic String toString() {StringBuilder stringBuffer = new StringBuilder("AudioInfo [\nInput #0, " + ext + "(" + audioCodecName + "), from '" + filePath.toAbsolutePath() + "': title: " + getMetadata(Metadata.TITLE) + ", artist: " + getMetadata(Metadata.ARTIST) + "\n");stringBuffer.append(metadata.toString());stringBuffer.append("Duration: ").append(AudioUtil.getDurationString(totalDurationMs)).append(", start: ").append(AudioUtil.getStartTimeString(startTime)).append(", sampleRate: ").append(sampleRate).append(" HZ").append(", bitrate: ").append(audioBitrate / 1000).append(" kb/s\n");if (!audioMetadata.isEmpty()) stringBuffer.append("AudioMetadata: ").append(audioMetadata.size()).append("\n");for (Map.Entry<String, String> entry : audioMetadata.entrySet()) {stringBuffer.append(String.format("%-14s%s", "\t" + entry.getKey(), ":\t" + entry.getValue())).append("\n");}if (!audioSideData.isEmpty()) stringBuffer.append("AudioSideData: ").append(audioSideData.size()).append("\n");for (Map.Entry<String, Buffer> entry : audioSideData.entrySet()) {stringBuffer.append(String.format("%-14s%s", "\t" + entry.getKey(), ":\t" + entry.getValue())).append("\n");}stringBuffer.append("]");return stringBuffer.toString();}
}
import org.bytedeco.ffmpeg.avformat.AVFormatContext;
import org.bytedeco.ffmpeg.avutil.AVDictionary;
import org.bytedeco.ffmpeg.avutil.AVDictionaryEntry;import java.util.HashMap;
import java.util.Map;import static org.bytedeco.ffmpeg.global.avutil.*;/*** @author xhbruce** ffmpeg-6.0\libavformat\avformat.h* album        -- name of the set this work belongs to* album_artist -- main creator of the set/album, if different from artist.* e.g. "Various Artists" for compilation albums.* artist       -- main creator of the work* comment      -- any additional description of the file.* composer     -- who composed the work, if different from artist.* copyright    -- name of copyright holder.* creation_time-- date when the file was created, preferably in ISO 8601.* date         -- date when the work was created, preferably in ISO 8601.* disc         -- number of a subset, e.g. disc in a multi-disc collection.* encoder      -- name/settings of the software/hardware that produced the file.* encoded_by   -- person/group who created the file.* filename     -- original name of the file.* genre        -- <self-evident>.* language     -- main language in which the work is performed, preferably* in ISO 639-2 format. Multiple languages can be specified by* separating them with commas.* performer    -- artist who performed the work, if different from artist.* E.g for "Also sprach Zarathustra", artist would be "Richard* Strauss" and performer "London Philharmonic Orchestra".* publisher    -- name of the label/publisher.* service_name     -- name of the service in broadcasting (channel name).* service_provider -- name of the service provider in broadcasting.* title        -- name of the work.* track        -- number of this work in the set, can be in form current/total.* variant_bitrate -- the total bitrate of the bitrate variant that the current stream is part of*/
public class Metadata {private AVDictionary avDictionary;private Map<String, String> metadata = new HashMap<String, String>();public static final String ALBUM = "album";public static final String ALBUM_ARTIST = "album_artist";public static final String ARTIST = "artist";public static final String COMMENT = "comment";public static final String COMPOSER = "composer";public static final String COMPYRIGHT = "copyright";public static final String CREATION_TIME = "creation_time";public static final String DATE = "date";public static final String DISC = "disc";public static final String ENCODER = "encoder";public static final String ENCODER_BY = "encoded_by";public static final String FILENAME = "filename";public static final String GENRE = "genre";public static final String LANGUAGE = "language";public static final String PERFORMER = "performer";public static final String PUBLISHER = "publisher";public static final String SERVICE_NAME = "service_name";public static final String SERVICE_PROVIDER = "service_provider";public static final String TITLE = "title";public static final String TRACK = "track";public static final String VARIANT_BITRATE = "variant_bitrate";private int dictCount;public Metadata(AVFormatContext formatContext) {this(formatContext.metadata());}public Metadata(AVDictionary dictionary) {avDictionary = dictionary;dictCount = av_dict_count(avDictionary);initMetadate();}private void initMetadate() {metadata = new HashMap<String, String>();AVDictionaryEntry tag = null;while ((tag = av_dict_iterate(avDictionary, tag)) != null) {String key = tag.key().getString();String value = tag.value().getString();metadata.put(key, value);}}public Metadata(Map<String, String> metadata) {this.metadata = metadata;dictCount = metadata.size();}/*** or av_dict_get(avDictionary, key, null, 0);*/public String getValue(String key) {return metadata.get(key);}public int size() {return dictCount;}@Overridepublic String toString() {StringBuilder stringBuffer = new StringBuilder();stringBuffer.append("Metadata: ").append(dictCount).append("\n");for (Map.Entry<String, String> entry : metadata.entrySet()) {stringBuffer.append(String.format("%-14s%s", "\t" + entry.getKey(), ":\t" + entry.getValue())).append("\n");}return stringBuffer.toString();}
}

3、添加ExecutorService執行播放

implementation 'com.github.goxr3plus:java-stream-player:+' 實質也是Java Sound播放,作為參考。

  • ExecutorService audioPlayerExecutorServicec護理音頻播放
  • ExecutorService eventsExecutorService處理音頻播放狀態
public class AudioPlayer implements AudioPlayerInterface, Callable<Void> {private AudioInfo audioInfo;private volatile AudioInputStream audioInputStream;private AudioFormat audioFormat;private DataLine.Info info;private SourceDataLine audioLine;private final Object audioLock = new Object();/*** audio 播放服務*/private final ExecutorService audioPlayerExecutorService;/*** audio 播放狀態更新服務*/private final ExecutorService eventsExecutorService;/*** 通知監聽事件*/private final ArrayList<AudioPlayerListener> listeners;public AudioPlayer() {audioPlayerExecutorService = Executors.newSingleThreadExecutor();eventsExecutorService = Executors.newSingleThreadExecutor();listeners = new ArrayList<>();}@Overridepublic void addStreamPlayerListener(AudioPlayerListener audioPlayerListener) {listeners.add(audioPlayerListener);}@Overridepublic void removeStreamPlayerListener(AudioPlayerListener audioPlayerListener) {if (listeners != null) {listeners.remove(audioPlayerListener);}}public void open(String filePath) {open(new File(filePath));}@Overridepublic void open(File file) {audioInfo = new AudioInfo(file);initAudioInputStream();try {audioInfo.grabber.start();} catch (FFmpegFrameGrabber.Exception e) {throw new RuntimeException(e);}}private void initAudioInputStream() {audioFormat = AudioUtil.toAudioFormat(audioInfo.sampleFormat, audioInfo.sampleRate, audioInfo.audioChannels);createAudioLine();}private void createAudioLine() {try {info = new DataLine.Info(SourceDataLine.class, audioFormat);audioLine = (SourceDataLine) AudioSystem.getLine(info);XLog.i("Line : " + audioLine);XLog.i("Line Info : " + info);XLog.i("Line AudioFormat: " + audioFormat);audioLine.open(audioFormat);} catch (LineUnavailableException e) {throw new RuntimeException(e);}}@Overridepublic void play() {audioLine.start();audioPlayerExecutorService.submit(this);}@Overridepublic boolean pause() {return false;}@Overridepublic boolean resume() {return false;}@Overridepublic void stop() {}@Overridepublic Void call() throws FFmpegFrameGrabber.Exception {XLog.d("響應 audioPlayerExecutorService");synchronized (audioLock) {Frame frame;while ((frame = audioInfo.grabber.grabSamples()) != null) {if (frame.samples == null || frame.samples.length == 0) continue;Buffer buffer = frame.samples[0];byte[] audioBytes = AudioUtil.toByteArrayApplySourceDataLine(buffer);audioLine.write(audioBytes, 0, audioBytes.length);}}audioLine.drain();audioLine.stop();audioLine.close();audioInfo.grabber.stop();return null;}
}

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

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

相關文章

洛谷P1036 [NOIP 2002 普及組] 選數

P1036 [NOIP 2002 普及組] 選數 題目描述 已知 nnn 個整數 x1,x2,??,xnx_1,x_2,\cdots,x_nx1?,x2?,?,xn?&#xff0c;以及 111 個整數 kkk&#xff08;k<nk<nk<n&#xff09;。從 nnn 個整數中任選 kkk 個整數相加&#xff0c;可分別得到一系列的和。例如當 n4n…

Linux學習記錄(八)文件共享

本文記錄在Vmware中啟用文件共享時的一些注意事項&#xff1a;1.提前安裝vmware-tools&#xff0c;可以通過Vmware的虛擬機菜單欄中拿到文件&#xff0c;然后直接運行vmware-install.pl文件進行安裝&#xff1b;也可以通過指令sudo apt-get install open-vm-tools進行安裝。推薦…

洛谷 火燒赤壁 差分/貪心

題目背景曹操平定北方以后&#xff0c;公元 208 年&#xff0c;率領大軍南下&#xff0c;進攻劉表。他的人馬還沒有到荊州&#xff0c;劉表已經病死。他的兒子劉琮聽到曹軍聲勢浩大&#xff0c;嚇破了膽&#xff0c;先派人求降了。孫權任命周瑜為都督&#xff0c;撥給他三萬水軍…

Linux 用戶與組管理全解析

Linux 用戶與組管理一、用戶和組的基本概念 1. 用戶賬號類型 超級用戶&#xff08;root&#xff09;&#xff1a;默認擁有系統最高權限&#xff08;UID0&#xff09;&#xff0c;僅建議用于系統管理與維護&#xff0c;日常操作應使用普通用戶。普通用戶&#xff1a;由管理員創建…

開疆智能ModbusTCP轉Profient網關連接ER機器人配置案例

本案例時西門子1200PLC通過ModbusTCP轉Profinet網關連接埃斯頓機器人的配置案例&#xff0c;網關作為ModbusTCP的客戶端連接機器人。配置過程&#xff1a;首先打開機器人通訊手冊。查詢機器人支持的功能碼及默認IP和端口號打開網關配置軟件“Gateway Configuration Studio”新建…

Docker換源加速(更換鏡像源)詳細教程(2025.3最新可用鏡像,全網最詳細)

文章目錄前言可用鏡像源匯總換源方法1-臨時換源換源方法2-永久換源&#xff08;推薦&#xff09;常見問題及對應解決方案1.換源后&#xff0c;可以成功pull&#xff0c;但是search會出錯補充1.如何測試鏡像源是否可用2.Docker內的Linux換源教程換源速通版&#xff08;可以直接無…

機器學習【三】SVM

本文系統介紹了支持向量機(SVM)的理論與實踐。理論部分首先區分了線性可分與不可分問題&#xff0c;闡述了SVM通過尋找最優超平面實現分類的核心思想&#xff0c;包括支持向量、間隔最大化等關鍵概念。詳細講解了硬間隔與軟間隔SVM的數學原理&#xff0c;以及核函數(線性核、多…

DevOps平臺大比拼:Gitee、Jenkins與CircleCI如何選型?

DevOps平臺大比拼&#xff1a;Gitee、Jenkins與CircleCI如何選型&#xff1f; 在數字化轉型浪潮席卷全球的當下&#xff0c;DevOps已成為企業提升研發效能的關鍵引擎。面對市場上紛繁復雜的DevOps工具鏈&#xff0c;如何選擇最適合自身業務需求的平臺成為技術決策者的重要課題。…

開源醫院信息管理系統:基于若依框架的智慧醫療解決方案

引言在數字化浪潮的推動下&#xff0c;醫療行業正加速向信息化、智能化轉型。醫院信息管理系統&#xff08;HIS&#xff09;作為醫療管理的核心工具&#xff0c;直接影響醫院的運營效率和服務質量。近期&#xff0c;一款基于 若依框架 Vue 的開源醫院管理系統&#xff08;hosp…

我的世界進階模組開發教程——附魔(2)

EnchantmentHelper 類詳解 EnchantmentHelper 是 Minecraft 中處理物品附魔邏輯的核心工具類,提供附魔的存儲、查詢、計算和應用等功能。以下是對其字段和方法的逐行詳細解釋: 關鍵字段 private static final String TAG_ENCH_ID = "id"; // NBT標簽鍵:附…

深度學習零基礎入門(4)-卷積神經網絡架構

許久不見~ 本節我們延續上一節的話題來看看卷積神經網絡的架構&#xff0c;看看具體的卷積、池化等操作卷積神經網絡詳解&#xff1a;從基礎操作到整體架構 一、卷積操作&#xff1a;特征提取的核心 卷積是卷積神經網絡&#xff08;CNN&#xff09;的核心操作&#xff0c;靈感來…

C語言的控制語句

C的控制語句 控制語句是C語言中用于控制程序執行流程的結構。通過控制語句,可以根據條件執行不同的代碼塊,或者重復執行某些操作,從而實現復雜的邏輯和功能。掌握控制語句是編寫有效和高效C程序的關鍵。 1 條件控制 條件控制語句用于根據某些條件來決定程序的執行路徑。C語…

Mac電腦基本功能快捷鍵

1. 個性化桌面 將喜愛照片添加為桌面墻紙。前往“系統設置”&#xff0c;然后點按邊欄中的“墻紙”。點按“添加照片”&#xff0c;然后從文件或“照片”App選取一張照片。 2. 截屏 按下鍵盤上的Shift &#xfffc; Command ? 5&#xff0c;然后選取捕捉整個屏幕、App窗口或…

微算法科技(NASDAQ: MLGO)開發量子邊緣檢測算法,為實時圖像處理與邊緣智能設備提供了新的解決方案

圖像邊緣檢測是計算機視覺的核心任務&#xff0c;傳統算法&#xff08;如 Sobel、Canny&#xff09;依賴梯度計算與閾值分割&#xff0c;在處理高分辨率、復雜紋理圖像時面臨計算效率瓶頸。隨著量子計算技術的發展&#xff0c;利用量子態疊加與并行處理特性&#xff0c;微算法科…

斷點續傳Demo實現

基于我們的DownloadManager.swift代碼&#xff0c;讓我詳細解釋斷點續傳需要實現的核心功能&#xff1a; 斷點續傳的核心實現要素 1. 后臺會話配置 private func setupBackgroundSession() {let config URLSessionConfiguration.background(withIdentifier: "com.test.do…

《Leetcode》-面試題-hot100-子串

題目列表 560. 和為K的子數組 中等難度 leetcode鏈接 239 滑動窗口最大值 困難難度 leetcode鏈接 76 最小覆蓋子串 困難難度 leetcode鏈接 題目 &#xff08;1&#xff09;和為K的子數組 給你一個整數數組 nums 和一個整數 k &#xff0c;請你統計并返回 該數組中和為 …

點擊彈框以外的區域關閉彈框

在 Vue 3 中&#xff0c;如果你想判斷點擊的目標是否在彈框內&#xff0c;可以通過以下步驟實現。這里我們將使用 ref 來引用彈框組件&#xff0c;并在點擊事件中進行判斷。 示例代碼 1. 創建彈框子組件 首先&#xff0c;創建一個名為 Modal.vue 的子組件。 <!-- Modal.vue …

00.Vue基礎入門【小白級別手把手!】

目錄 一、Vue介紹 二、創建Vue項目 nodeJs nvm版本管理 創建Vue項目 VS Code編輯器 三、.Vue文件結構說明 數據渲染 四、Vue項目目錄說明 main.ts文件說明 五、Vue官網文檔學習 一、Vue介紹 基礎介紹 Vue是一個前端Web框架&#xff0c;屬于單頁應用&#xff08;SPA&am…

將Varjo XR技術融入戰斗機訓練模擬器,有效提升模擬訓練沉浸感與效率

本周在Varjo總部&#xff0c;收到了一份令人興奮的禮物&#xff0c;一架由Dogfight Boss與varjo XR-4集成的訓練模擬器。這是一個專業級模擬器&#xff0c;專為高保真訓練和任務排練而設計&#xff0c;非常注重細節&#xff0c;提高了沉浸水平。為此Dogfight Boss的首席執行官L…

C# async await 實現機制詳解

一、async/await 異步編程實現機制 1.1 核心概念 async/await 是 C# 5.0 引入的語法糖&#xff0c;它基于**狀態機&#xff08;State Machine&#xff09;**模式實現&#xff0c;將異步方法轉換為編譯器生成的狀態機類。 1.2 編譯器轉換過程 當編譯器遇到 async 方法時&#xf…