java合并MP3文件

java合并MP3文件,第1张

代码没问题
是这样的每个MP3由两到三个部分构成:ID3v2标签+MP3声音+(ID3v1标签),后面一个不一定有。
其中的标签就是MP3的各种信息,比如说歌曲名、演唱者、唱片封面什么的
所以按你这种直接合并的方式,合并出来的就是:
标签+MP3声音+标签+标签+MP3声音+标签
自然中间就有一段没有声音了
建议你参考标签格式,对于MP3文件进行处理,然后再合并就好了
id3官网:>public static void main(String[] args) throws IOException {
//我这里固定两个文件位置和名称,自己随意更改
String srcPath = "E:/songmp3";
String targetPath = "E:/newsongmp3";
File srcFile = new File(srcPath);
File targetFile = new File(targetPath);
if(!targetFileexists()){
targetFilecreateNewFile();
}
byte[] buffer = new byte[41024];
BufferedInputStream bis ;
BufferedOutputStream bos;
bis = new BufferedInputStream(new FileInputStream(srcFile));
bos = new BufferedOutputStream(new FileOutputStream(targetFile));
int length =0;
while((length = bisread(buffer))!=-1){
boswrite(buffer);
}
bosflush();
bosclose();
bisclose();
}

用Java的话你给以用javaapplet包中的类 它的方法newAudioClip(javanetURL)或Applet类的实例方法:getAudioClip(URL url,String name)
或者在另一个线程中创建音频对象

JAVA应用程序添加背景音乐示例:

1先定义一个 URL 对象,并赋NULL值;

URL   musicURL   =   null;

2然后为定义的 URL 赋值,即得到要播放的URL地址,

musicURL   =   new   URL( "音乐地址");//得到要播放音乐的url

3创建一个播放音频的实例,并得到这个实例

AudioClip   ac   =   AppletnewAudioClip(musicURL);   //得到一个播放音频的实例      

4进行播放控制

acplay();   //播放一编

acloop();   //循环播放

acstop();//停止

播放本地音乐示例:

private void playMusic(){
       try {
           javaxmediaPlayer player;
           if(null==player){
               File musicFile=new File("/musicmp3");//得到一个MP3文件
               if(musicFileexists()){
                   MediaLocator  locator=new MediaLocator("file:"+musicFilegetAbsolutePath());
                   player = ManagercreateRealizedPlayer(locator);
                   playerprefetch();//预读文件
               }else{
                   Systemerrprintln(progetMusicFile()+" 找不到");
               }
           }
           //centerpaneladd(playergetControlPanelComponent()!=nullplayergetControlPanelComponent():null);//显示播放工具
           playerstart();//播放
       } catch (CannotRealizeException ex) {
           exprintStackTrace();
       } catch (NoPlayerException ex) {
           exprintStackTrace();
       } catch (IOException ex) {
           exprintStackTrace();
       }
   }

可以通过Service来播放背景音乐,以下是实现代码:
1在AndroidManifestxml文件中的<application>标签内加入下边语句
<service android:name="MusicServer">
<intent-filter>
<action android:name="comangelAndroidMUSIC"/>
<category android:name="androidintentcategorydefault" />
</intent-filter>
</service>
2新建MusicServerjava类,内容为
import androidappService;
import androidcontentIntent;
import androidmediaMediaPlayer;
import androidosIBinder;
public class MusicServer extends Service {
private MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent,int startId){
superonStart(intent, startId);
if(mediaPlayer==null){
// Rrawmmp是资源文件,MP3格式的
mediaPlayer = MediaPlayercreate(this, Rrawabc);
mediaPlayersetLooping(true);
mediaPlayerstart();
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
superonDestroy();
mediaPlayerstop();
}
}
3将歌曲放入raw文件夹下,名称为abc。
4在Activity中加入代码
private Intent intent = new Intent("comangelAndroidMUSIC");
onCreate方法中加入startService(intent);
就可以播放了。

package mp3;
import javaioFile;
import javaioFileNotFoundException;
import javaioIOException;
import javaioRandomAccessFile;
public class ReadMp3 {
private SongInfo info = null;
private RandomAccessFile ran = null;
private File file = null;
public ReadMp3() throws FileNotFoundException {
file = new File("/rec/感觉不到你mp3");
ran = new RandomAccessFile(file, "r");
Systemoutprintln("文件装载完毕");
}
public static void main(String[] args) throws IOException {
ReadMp3 read = new ReadMp3();
byte[] buffer = new byte[128];
readranseek(readranlength() - 128);
readranread(buffer);
SongInfo info = new SongInfo(buffer);
Systemoutprintln("name:" + infogetSongName() + " year:"
+ infogetYear() + " 歌手:" + infogetArtist() + " 专辑名:"
+ infogetAlbum() + " 备注:" + infogetComment());
}
}
package mp3;
/
一个歌曲信息的类的结构表示 这个歌曲是使用ID3V1的信息存储结构的

@author hadeslee
/
public class SongInfo {
private final String TAG = "TAG"; // 文件头1-3
private String songName; // 歌曲名4-33
private String artist; // 歌手名34-63
private String album; // 专辑名61-93
private String year; // 年94-97
private String comment; // 备注98-125
private byte r1, r2, r3; // 三个保留位126,127,128
private boolean valid; // 是否合法
public transient String fileName; // 此歌曲对应的文件名,没有封装
public SongInfo(byte[] data) {
if (datalength != 128) {
throw new RuntimeException("数据长度不合法:" + datalength);
}
String tag = new String(data, 0, 3);
// 只有前三个字节是TAG才处理后面的字节
if (tagequalsIgnoreCase("TAG")) {
valid = true;
songName = new String(data, 3, 30)trim();
artist = new String(data, 33, 30)trim();
album = new String(data, 63, 30)trim();
year = new String(data, 93, 4)trim();
comment = new String(data, 97, 28)trim();
r1 = data[125];
r2 = data[126];
r3 = data[127];
} else {
valid = false;
}
}
public SongInfo() {
}
/
返回是否合法

@return 是否
/
public boolean isValid() {
return valid;
}
/
得到此对象的128个字节的表示形式

@return
/
public byte[] getBytes() {
byte[] data = new byte[128];
Systemarraycopy(TAGgetBytes(), 0, data, 0, 3);
byte[] temp = songNamegetBytes();
Systemarraycopy(temp, 0, data, 3, templength > 30 30 : templength);
temp = artistgetBytes();
System
arraycopy(temp, 0, data, 33, templength > 30 30
: templength);
temp = albumgetBytes();
System
arraycopy(temp, 0, data, 63, templength > 30 30
: templength);
temp = yeargetBytes();
Systemarraycopy(temp, 0, data, 93, templength > 4 4 : templength);
temp = commentgetBytes();
System
arraycopy(temp, 0, data, 97, templength > 28 28
: templength);
data[125] = r1;
data[126] = r2;
data[127] = r3;
return data;
}
public String getArtist() {
return artist;
}
public void setArtist(String authorName) {
thisartist = authorName;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
thiscomment = comment;
}
public byte getR1() {
return r1;
}
public void setR1(byte r1) {
thisr1 = r1;
}
public byte getR2() {
return r2;
}
public void setR2(byte r2) {
thisr2 = r2;
}
public byte getR3() {
return r3;
}
public void setR3(byte r3) {
thisr3 = r3;
}
public String getSongName() {
return songName;
}
public void setSongName(String songName) {
if (songName == null) {
throw new NullPointerException("歌名不能是null!");
}
valid = true;
thissongName = songName;
}
public String getAlbum() {
return album;
}
public void setAlbum(String specialName) {
thisalbum = specialName;
}
public String getYear() {
return year;
}
public void setYear(String year) {
thisyear = year;
}
}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12791383.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-28
下一篇 2023-05-28

发表评论

登录后才能评论

评论列表(0条)

保存