當前位置:首頁 » 編程語言 » 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-07-02 00:00:46 瀏覽:304
煙花的代碼c語言 發布:2025-07-01 23:56:04 瀏覽:223
安卓默認打開文件方式怎麼修改 發布:2025-07-01 23:30:38 瀏覽:862
壓縮機接線座 發布:2025-07-01 23:17:48 瀏覽:662
iqoo瀏覽器緩存路徑 發布:2025-07-01 23:12:38 瀏覽:691
明日之後如何獲得最新伺服器 發布:2025-07-01 23:12:35 瀏覽:50
tv加密頻道 發布:2025-07-01 23:10:58 瀏覽:623
如何找到5d4通信密碼 發布:2025-07-01 23:03:35 瀏覽:233
華為pop伺服器地址怎麼填 發布:2025-07-01 23:02:44 瀏覽:462
訪問量賺錢 發布:2025-07-01 22:47:58 瀏覽:383