剛好了解,可以實現的,通過java代碼調用ffmpeg去轉換,是ok的, 但是ffmpeg沒有對應java的api(我是沒找到),我的實現方式去通過java代碼調用命令行,來完成轉換的.
創新互聯建站-專業網站定制、快速模板網站建設、高性價比德宏州網站開發、企業建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式德宏州網站制作公司更省心,省錢,快速模板網站建設找我們,業務覆蓋德宏州地區。費用合理售后完善,十載實體公司更值得信賴。
千千靜聽 就可以了
把歌曲放進播放列表中 右鍵點擊要轉化的歌曲 選擇 "轉化格式"
在里面的 "輸出格式" 選擇 "wave 文件輸出" 就行了
記得修改保存的地址
/*
*?實現錄音機的功能
*/
package?com.liuyun.MyRecord1;
import?java.awt.*;
import?javax.swing.*;
import?java.awt.event.*;
import?java.io.*;
import?javax.sound.sampled.*;
import?java.lang.*;
public?class?MyRecord?extends?JFrame?implements?ActionListener{
//定義錄音格式
AudioFormat?af?=?null;
//定義目標數據行,可以從中讀取音頻數據,該?TargetDataLine?接口提供從目標數據行的緩沖區讀取所捕獲數據的方法。
TargetDataLine?td?=?null;
//定義源數據行,源數據行是可以寫入數據的數據行。它充當其混頻器的源。應用程序將音頻字節寫入源數據行,這樣可處理字節緩沖并將它們傳遞給混頻器。
SourceDataLine?sd?=?null;
//定義字節數組輸入輸出流
ByteArrayInputStream?bais?=?null;
ByteArrayOutputStream?baos?=?null;
//定義音頻輸入流
AudioInputStream?ais?=?null;
//定義停止錄音的標志,來控制錄音線程的運行
Boolean?stopflag?=?false;
//記錄開始錄音的時間
long?startPlay;
//定義所需要的組件
JPanel?jp1,jp2,jp3;
JLabel?jl1=null;
JButton?captureBtn,stopBtn,playBtn,saveBtn;
public?static?void?main(String[]?args)?{
//創造一個實例
MyRecord?mr?=?new?MyRecord();
}
//構造函數
public?MyRecord()
{
//組件初始化
jp1?=?new?JPanel();
jp2?=?new?JPanel();
jp3?=?new?JPanel();
//定義字體
Font?myFont?=?new?Font("華文新魏",Font.BOLD,30);
jl1?=?new?JLabel("錄音機功能的實現");
jl1.setFont(myFont);
jp1.add(jl1);
captureBtn?=?new?JButton("開始錄音");
//對開始錄音按鈕進行注冊監聽
captureBtn.addActionListener(this);
captureBtn.setActionCommand("captureBtn");
//對停止錄音進行注冊監聽
stopBtn?=?new?JButton("停止錄音");
stopBtn.addActionListener(this);
stopBtn.setActionCommand("stopBtn");
//對播放錄音進行注冊監聽
playBtn?=?new?JButton("播放錄音");
playBtn.addActionListener(this);
playBtn.setActionCommand("playBtn");
//對保存錄音進行注冊監聽
saveBtn?=?new?JButton("保存錄音");
saveBtn.addActionListener(this);
saveBtn.setActionCommand("saveBtn");
this.add(jp1,BorderLayout.NORTH);
this.add(jp2,BorderLayout.CENTER);
this.add(jp3,BorderLayout.SOUTH);
jp3.setLayout(null);
jp3.setLayout(new?GridLayout(1,?4,10,10));
jp3.add(captureBtn);
jp3.add(stopBtn);
jp3.add(playBtn);
jp3.add(saveBtn);
//設置按鈕的屬性
captureBtn.setEnabled(true);
stopBtn.setEnabled(false);
playBtn.setEnabled(false);
saveBtn.setEnabled(false);
//設置窗口的屬性
this.setSize(400,300);
this.setTitle("錄音機");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public?void?actionPerformed(ActionEvent?e)?{
if(e.getActionCommand().equals("captureBtn"))
{
//點擊開始錄音按鈕后的動作
//停止按鈕可以啟動
captureBtn.setEnabled(false);
stopBtn.setEnabled(true);
playBtn.setEnabled(false);
saveBtn.setEnabled(false);
//調用錄音的方法
capture();
//記錄開始錄音的時間
startPlay?=?System.currentTimeMillis();
}else?if?(e.getActionCommand().equals("stopBtn"))?{
//點擊停止錄音按鈕的動作
captureBtn.setEnabled(true);
stopBtn.setEnabled(false);
playBtn.setEnabled(true);
saveBtn.setEnabled(true);
//調用停止錄音的方法
stop();
//記錄停止錄音的時間
long?stopPlay?=?System.currentTimeMillis();
//輸出錄音的時間
System.out.println("Play?continues?"?+?(stopPlay-startPlay));
}else?if(e.getActionCommand().equals("playBtn"))
{
//調用播放錄音的方法
play();
}else?if(e.getActionCommand().equals("saveBtn"))
{
//調用保存錄音的方法
save();
}
}
//開始錄音
public?void?capture()
{
try?{
//af為AudioFormat也就是音頻格式
af?=?getAudioFormat();
DataLine.Info?info?=?new?DataLine.Info(TargetDataLine.class,af);
td?=?(TargetDataLine)(AudioSystem.getLine(info));
//打開具有指定格式的行,這樣可使行獲得所有所需的系統資源并變得可操作。
td.open(af);
//允許某一數據行執行數據?I/O
td.start();
//創建播放錄音的線程
Record?record?=?new?Record();
Thread?t1?=?new?Thread(record);
t1.start();
}?catch?(LineUnavailableException?ex)?{
ex.printStackTrace();
return;
}
}
//停止錄音
public?void?stop()
{
stopflag?=?true;
}
//播放錄音
public?void?play()
{
//將baos中的數據轉換為字節數據
byte?audioData[]?=?baos.toByteArray();
//轉換為輸入流
bais?=?new?ByteArrayInputStream(audioData);
af?=?getAudioFormat();
ais?=?new?AudioInputStream(bais,?af,?audioData.length/af.getFrameSize());
try?{
DataLine.Info?dataLineInfo?=?new?DataLine.Info(SourceDataLine.class,?af);
sd?=?(SourceDataLine)?AudioSystem.getLine(dataLineInfo);
sd.open(af);
sd.start();
//創建播放進程
Play?py?=?new?Play();
Thread?t2?=?new?Thread(py);
t2.start();
}?catch?(Exception?e)?{
e.printStackTrace();
}finally{
try?{
//關閉流
if(ais?!=?null)
{
ais.close();
}
if(bais?!=?null)
{
bais.close();
}
if(baos?!=?null)
{
baos.close();
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
}
//保存錄音
public?void?save()
{
//取得錄音輸入流
af?=?getAudioFormat();
byte?audioData[]?=?baos.toByteArray();
bais?=?new?ByteArrayInputStream(audioData);
ais?=?new?AudioInputStream(bais,af,?audioData.length?/?af.getFrameSize());
//定義最終保存的文件名
File?file?=?null;
//寫入文件
try?{
//以當前的時間命名錄音的名字
//將錄音的文件存放到F盤下語音文件夾下
File?filePath?=?new?File("F:/語音文件");
if(!filePath.exists())
{//如果文件不存在,則創建該目錄
filePath.mkdir();
}
long?time?=?System.currentTimeMillis();
file?=?new?File(filePath+"/"+time+".wav");
AudioSystem.write(ais,?AudioFileFormat.Type.WAVE,?file);
//將錄音產生的wav文件轉換為容量較小的mp3格式
//定義產生后文件名
String?tarFileName?=?time+".mp3";
Runtime?run?=?null;
try?{
run?=?Runtime.getRuntime();
long?start=System.currentTimeMillis();
//調用解碼器來將wav文件轉換為mp3文件
strongspan?style="color:#ff0000;"Process?p=run.exec(filePath+"/"+"lame?-b?16?"+filePath+"/"+file.getName()+"?"+filePath+"/"+tarFileName);?//16為碼率,可自行修改
/span/strong//釋放進程
p.getOutputStream().close();
p.getInputStream().close();
p.getErrorStream().close();
p.waitFor();
long?end=System.currentTimeMillis();
System.out.println("convert?need?costs:"+(end-start)+"ms");
//刪除無用的wav文件
if(file.exists())
{
file.delete();
}
}?catch?(Exception?e)?{
e.printStackTrace();
}finally{
//最后都要執行的語句
//run調用lame解碼器最后釋放內存
run.freeMemory();
}
}?catch?(Exception?e)?{
e.printStackTrace();
}finally{
//關閉流
try?{
if(bais?!=?null)
{
bais.close();
}
if(ais?!=?null)
{
ais.close();
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
}
//設置AudioFormat的參數
public?AudioFormat?getAudioFormat()
{
//下面注釋部分是另外一種音頻格式,兩者都可以
AudioFormat.Encoding?encoding?=?AudioFormat.Encoding.
PCM_SIGNED?;
float?rate?=?8000f;
int?sampleSize?=?16;
String?signedString?=?"signed";
boolean?bigEndian?=?true;
int?channels?=?1;
return?new?AudioFormat(encoding,?rate,?sampleSize,?channels,
(sampleSize?/?8)?*?channels,?rate,?bigEndian);
//??????//采樣率是每秒播放和錄制的樣本數
//??????float?sampleRate?=?16000.0F;
//??????//?采樣率8000,11025,16000,22050,44100
//??????//sampleSizeInBits表示每個具有此格式的聲音樣本中的位數
//??????int?sampleSizeInBits?=?16;
//??????//?8,16
//??????int?channels?=?1;
//??????//?單聲道為1,立體聲為2
//??????boolean?signed?=?true;
//??????//?true,false
//??????boolean?bigEndian?=?true;
//??????//?true,false
//??????return?new?AudioFormat(sampleRate,?sampleSizeInBits,?channels,?signed,bigEndian);
}
//錄音類,因為要用到MyRecord類中的變量,所以將其做成內部類
class?Record?implements?Runnable
{
//定義存放錄音的字節數組,作為緩沖區
byte?bts[]?=?new?byte[10000];
//將字節數組包裝到流里,最終存入到baos中
//重寫run函數
public?void?run()?{
baos?=?new?ByteArrayOutputStream();
try?{
stopflag?=?false;
while(stopflag?!=?true)
{
//當停止錄音沒按下時,該線程一直執行
//從數據行的輸入緩沖區讀取音頻數據。
//要讀取bts.length長度的字節,cnt?是實際讀取的字節數
int?cnt?=?td.read(bts,?0,?bts.length);
if(cnt??0)
{
baos.write(bts,?0,?cnt);
}
}
}?catch?(Exception?e)?{
e.printStackTrace();
}finally{
try?{
//關閉打開的字節數組流
if(baos?!=?null)
{
baos.close();
}
}?catch?(IOException?e)?{
e.printStackTrace();
}finally{
td.drain();
td.close();
}
}
}
}
//播放類,同樣也做成內部類
class?Play?implements?Runnable
{
//播放baos中的數據即可
public?void?run()?{
byte?bts[]?=?new?byte[10000];
try?{
int?cnt;
//讀取數據到緩存數據
while?((cnt?=?ais.read(bts,?0,?bts.length))?!=?-1)
{
if?(cnt??0)
{
//寫入緩存數據
//將音頻數據寫入到混頻器
sd.write(bts,?0,?cnt);
}
}
}?catch?(Exception?e)?{
e.printStackTrace();
}finally{
sd.drain();
sd.close();
}
}
}
}
網站欄目:java歌曲格式轉換代碼 java加音樂
本文來源:http://vcdvsql.cn/article22/doicejc.html
成都網站建設公司_創新互聯,為您提供ChatGPT、網站策劃、外貿建站、做網站、網站收錄、網站維護
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯