當前位置:首頁 » 編程語言 » mp3java

mp3java

發布時間: 2023-08-14 13:16:51

1. 怎樣用java流來分割一個mp3文件代碼

package xuan;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.Buffer;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
public class mp3 {
public static void cutMusic() throws IOException{
File file=new File("E:\\薛之謙 - 那是你離開了北京的生活.flac");
File file2=new File("E:\\music");
FileInputStream fis =new FileInputStream(file);
FileOutputStream fos=null;
//if(file2.exists()!=true) {
// file2.mkdirs();
//}

int len=0;
int x=0;
int y=1020*1024;
byte [] one=new byte[y];
if(file.length()%y!=0) {
x=(int)(file.length()/y+1);
}else if(file.length()%y==0) {
x=(int)(file.length()/y);
}
for(int i=1;i<=x;i++) {
len=fis.read(one);
fos=new FileOutputStream (new File(file2,i+".flac"));
fos.write(one,0,len);
}
fis.close();
fos.close();
}
public static void mergeMusic()throws IOException{
File file=new File("E:\\merge.flac");
File file2=new File("E:\\music");
// if(file.exists()!=true) {
// file.createNewFile();
// }
File[]f=file2.listFiles();
FileInputStream fis=null;
FileOutputStream fos=new FileOutputStream(file);
BufferedOutputStream bos =new BufferedOutputStream(fos,1024*1024);
int len=0;
for(int i=0;i<f.length;i++) {
fis =new FileInputStream(f[i]);
BufferedInputStream bis =new BufferedInputStream(fis,1024*1024);
while((len=bis.read())!=-1) {
bos.write(len);
}
fos.flush();
fis.close();
}
bos.close();
fos.close();
}
public static void main(String[] args) throws IOException{
cutMusic();
mergeMusic();
// TODO Auto-generated method stub
}
}

2. java如何實現播放mp3

簡單的實例,代碼如下豎謹,純粹JMF載入MP3並播放:
import javax.media.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PlayerMusic implements ControllerListener {// ControllerListener
// 控制事件
private Player player;

private boolean first, loop;

private String path;
private List mp3List;
private int mp3NO = 0;

PlayerMusic(List mp3List) {
this.mp3List = mp3List;
}

public void start() {
try {
player = Manager.createPlayer(new MediaLocator("file://" + mp3List.get(mp3NO)));
} catch (NoPlayerException ex) {
ex.printStackTrace();
System.out.println("不能播放余備基文件");
return;
} catch (IOException ex) {
ex.printStackTrace();
return;
}
if (player == null) {
System.out.println("播放器為空");
return;
}

first = false;
player.addControllerListener(this);
// 提取媒體內容
player.prefetch();

}

public void controllerUpdate(ControllerEvent e) {
// 當媒體播放結束時,循環播放
if (e instanceof EndOfMediaEvent) {
mp3NO++;
if(mp3NO<this.mp3List.size()){
this.start();
}
return;
}

// 當預提取媒體的內容結束
if (e instanceof PrefetchCompleteEvent) {
player.start();
return;
}
// 當實例化滾櫻後
if (e instanceof RealizeCompleteEvent) {
// pack(); //執行pack()操作
return;
}

}
public static void main(String[] args) {
List mp3List = new ArrayList();
mp3List.add("d://a.mp3");
mp3List.add("d://b.mp3");
mp3List.add("d://c.mp3");
PlayerMusic pm = new PlayerMusic(mp3List);
pm.start();
}
}

熱點內容
java遍歷二維數組 發布:2025-03-18 03:36:01 瀏覽:410
銳捷源碼 發布:2025-03-18 03:26:55 瀏覽:436
訴訟中止裁定後可否解壓 發布:2025-03-18 03:24:51 瀏覽:128
sqlserver全文搜索 發布:2025-03-18 03:23:58 瀏覽:715
u盤裡面文件夾沒有了 發布:2025-03-18 03:22:19 瀏覽:229
華為p系列手機哪個配置好 發布:2025-03-18 03:20:13 瀏覽:621
易語言連接access資料庫 發布:2025-03-18 03:12:48 瀏覽:661
苗木源碼 發布:2025-03-18 03:12:38 瀏覽:747
oracle卸載資料庫 發布:2025-03-18 03:05:15 瀏覽:46
編譯時生成固件怎麼辦 發布:2025-03-18 03:04:30 瀏覽:707