2个回答
展开全部
音乐也有格式,你什么都不说也不知道你要哪种.另外是j2se还是j2me你也没说,都是java,差别大了去了.
这是我自己写的一个MIDI和Wav的管理类,有载入播放停止等等,但是音量和节拍管理没有,要想实现高级功能最好使用jmf,基于j2se:
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import App.AppFrameBase;
import javax.sound.midi.MidiSystem;
import java.applet.AudioClip;
import java.applet.Applet;
import javax.sound.midi.Sequence;
import javax.sound.midi.InvalidMidiDataException;
import java.net.URL;
import java.net.MalformedURLException;
import javax.sound.midi.Sequencer;
import javax.sound.midi.MidiUnavailableException;
public class AudioHelper
{
public Sequencer MIDIPlayer ;
public AudioClip WAVPlayer ;
public Vector m_vAudio = new Vector() ;
private int m_nCurrentMidiIndex = -1 ;
private int m_nCurrentWavIndex = -1 ;
public AudioHelper()
{
//读取系统声音列表
try
{
MIDIPlayer = MidiSystem.getSequencer();
m_vAudio = findSound() ;
} catch (MidiUnavailableException ex){ex.printStackTrace();}
}
public static final Vector findSound()
{
String path = AppFrameBase.getApplicationDirectory() ;
File file = new File(path + "/Data/Audio/") ;
Vector v = new Vector() ;
File[] fileList = file.listFiles();
for(int i = 0 ; i < fileList.length ; i++)
if (fileList[i].isFile())
v.addElement(fileList[i].getName());
return v ;
}
public static final Sequence loadSound(String name)
{
String path = AppFrameBase.getApplicationDirectory() + "/Data/Audio/" + name ;
return loadSoundAsPath(path) ;
}
public static final Sequence loadSoundAsPath(String path)
{
try
{
return MidiSystem.getSequence(new File(path));
} catch (IOException ex)
{
ex.printStackTrace();
return null;
} catch (InvalidMidiDataException ex)
{
ex.printStackTrace();
return null;
}
}
public final String getSoundPath(int index)
{
if (index < 0 || index >= m_vAudio.size())
return null ;
return m_vAudio.elementAt(index).toString() ;
}
public static final AudioClip loadAudioClip(String name)
{
String path = AppFrameBase.getApplicationDirectory() ;
return loadAudioClipAsPath(path + "/Data/Audio/" + name) ;
}
public static final AudioClip loadAudioClipAsPath(String path)
{
try
{
return Applet.newAudioClip(new URL("file:" + path));
} catch (MalformedURLException ex)
{
return null;
}
}
public void stopSound()
{
stopMIDI() ;
stopWAV() ;
}
public void stopMIDI()
{
if (MIDIPlayer != null)
{
if (MIDIPlayer.isRunning())
MIDIPlayer.stop();
}
}
public void notifyStopMIDI()
{
stopMIDI() ;
if (MIDIPlayer != null)
if (MIDIPlayer.isOpen())
MIDIPlayer.close();
}
public void stopWAV()
{
if (WAVPlayer != null)
WAVPlayer.stop() ;
}
public void loopMIDI(boolean loop)
{
if (MIDIPlayer != null)
if (loop)
{
MIDIPlayer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
} else
{
MIDIPlayer.setLoopCount(0);
}
}
public void loopWAV(boolean loop)
{
if (WAVPlayer != null)
if (loop)
WAVPlayer.loop() ;
else
WAVPlayer.play() ;
}
public void playSound(int index,boolean loop)
{
String name = getSoundPath(index) ;
if (name == null)
throw new IndexOutOfBoundsException("Sound index " + index + " is out of current sound index max!") ;
try
{
if (isMidiFile(name))
{
if (m_nCurrentMidiIndex != index || (m_nCurrentMidiIndex == index && !MIDIPlayer.isRunning()))
{
stopMIDI();
if (m_nCurrentMidiIndex != index)
{
Sequence se = loadSound(name);
MIDIPlayer.setSequence(se);
}
MIDIPlayer.open();
loopMIDI(loop);
MIDIPlayer.start();
m_nCurrentMidiIndex = index ;
}
} else
if (isWavFile(name))
{
if (m_nCurrentWavIndex != index)
{
WAVPlayer = AudioHelper.loadAudioClip(name);
m_nCurrentWavIndex = index ;
}
if (WAVPlayer != null)
{
WAVPlayer.stop();
if (loop)
WAVPlayer.loop() ;
else
WAVPlayer.play();
}
}
} catch (InvalidMidiDataException ex)
{
ex.printStackTrace();
} catch (MidiUnavailableException ex)
{
ex.printStackTrace();
}
}
public static final boolean isMidiFile(String name)
{
return name.indexOf(".mid") != -1 ;
}
public static final boolean isWavFile(String name)
{
return name.indexOf(".wav") != -1 ;
}
}
这是我自己写的一个MIDI和Wav的管理类,有载入播放停止等等,但是音量和节拍管理没有,要想实现高级功能最好使用jmf,基于j2se:
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import App.AppFrameBase;
import javax.sound.midi.MidiSystem;
import java.applet.AudioClip;
import java.applet.Applet;
import javax.sound.midi.Sequence;
import javax.sound.midi.InvalidMidiDataException;
import java.net.URL;
import java.net.MalformedURLException;
import javax.sound.midi.Sequencer;
import javax.sound.midi.MidiUnavailableException;
public class AudioHelper
{
public Sequencer MIDIPlayer ;
public AudioClip WAVPlayer ;
public Vector m_vAudio = new Vector() ;
private int m_nCurrentMidiIndex = -1 ;
private int m_nCurrentWavIndex = -1 ;
public AudioHelper()
{
//读取系统声音列表
try
{
MIDIPlayer = MidiSystem.getSequencer();
m_vAudio = findSound() ;
} catch (MidiUnavailableException ex){ex.printStackTrace();}
}
public static final Vector findSound()
{
String path = AppFrameBase.getApplicationDirectory() ;
File file = new File(path + "/Data/Audio/") ;
Vector v = new Vector() ;
File[] fileList = file.listFiles();
for(int i = 0 ; i < fileList.length ; i++)
if (fileList[i].isFile())
v.addElement(fileList[i].getName());
return v ;
}
public static final Sequence loadSound(String name)
{
String path = AppFrameBase.getApplicationDirectory() + "/Data/Audio/" + name ;
return loadSoundAsPath(path) ;
}
public static final Sequence loadSoundAsPath(String path)
{
try
{
return MidiSystem.getSequence(new File(path));
} catch (IOException ex)
{
ex.printStackTrace();
return null;
} catch (InvalidMidiDataException ex)
{
ex.printStackTrace();
return null;
}
}
public final String getSoundPath(int index)
{
if (index < 0 || index >= m_vAudio.size())
return null ;
return m_vAudio.elementAt(index).toString() ;
}
public static final AudioClip loadAudioClip(String name)
{
String path = AppFrameBase.getApplicationDirectory() ;
return loadAudioClipAsPath(path + "/Data/Audio/" + name) ;
}
public static final AudioClip loadAudioClipAsPath(String path)
{
try
{
return Applet.newAudioClip(new URL("file:" + path));
} catch (MalformedURLException ex)
{
return null;
}
}
public void stopSound()
{
stopMIDI() ;
stopWAV() ;
}
public void stopMIDI()
{
if (MIDIPlayer != null)
{
if (MIDIPlayer.isRunning())
MIDIPlayer.stop();
}
}
public void notifyStopMIDI()
{
stopMIDI() ;
if (MIDIPlayer != null)
if (MIDIPlayer.isOpen())
MIDIPlayer.close();
}
public void stopWAV()
{
if (WAVPlayer != null)
WAVPlayer.stop() ;
}
public void loopMIDI(boolean loop)
{
if (MIDIPlayer != null)
if (loop)
{
MIDIPlayer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
} else
{
MIDIPlayer.setLoopCount(0);
}
}
public void loopWAV(boolean loop)
{
if (WAVPlayer != null)
if (loop)
WAVPlayer.loop() ;
else
WAVPlayer.play() ;
}
public void playSound(int index,boolean loop)
{
String name = getSoundPath(index) ;
if (name == null)
throw new IndexOutOfBoundsException("Sound index " + index + " is out of current sound index max!") ;
try
{
if (isMidiFile(name))
{
if (m_nCurrentMidiIndex != index || (m_nCurrentMidiIndex == index && !MIDIPlayer.isRunning()))
{
stopMIDI();
if (m_nCurrentMidiIndex != index)
{
Sequence se = loadSound(name);
MIDIPlayer.setSequence(se);
}
MIDIPlayer.open();
loopMIDI(loop);
MIDIPlayer.start();
m_nCurrentMidiIndex = index ;
}
} else
if (isWavFile(name))
{
if (m_nCurrentWavIndex != index)
{
WAVPlayer = AudioHelper.loadAudioClip(name);
m_nCurrentWavIndex = index ;
}
if (WAVPlayer != null)
{
WAVPlayer.stop();
if (loop)
WAVPlayer.loop() ;
else
WAVPlayer.play();
}
}
} catch (InvalidMidiDataException ex)
{
ex.printStackTrace();
} catch (MidiUnavailableException ex)
{
ex.printStackTrace();
}
}
public static final boolean isMidiFile(String name)
{
return name.indexOf(".mid") != -1 ;
}
public static final boolean isWavFile(String name)
{
return name.indexOf(".wav") != -1 ;
}
}
展开全部
音乐也有格式,你什么都不说也不知道你要哪种.另外是j2se还是j2me你也没说,都是java,差别大了去了.
这是我自己写的一个MIDI和Wav的管理类,有载入播放停止等等,但是音量和节拍管理没有,要想实现高级功能最好使用jmf,基于j2se:
import
java.io.File;
import
java.io.IOException;
import
java.util.Vector;
import
App.AppFrameBase;
import
javax.sound.midi.MidiSystem;
import
java.applet.AudioClip;
import
java.applet.Applet;
import
javax.sound.midi.Sequence;
import
javax.sound.midi.InvalidMidiDataException;
import
java.net.URL;
import
java.net.MalformedURLException;
import
javax.sound.midi.Sequencer;
import
javax.sound.midi.MidiUnavailableException;
public
class
AudioHelper
{
public
Sequencer
MIDIPlayer
;
public
AudioClip
WAVPlayer
;
public
Vector
m_vAudio
=
new
Vector()
;
private
int
m_nCurrentMidiIndex
=
-1
;
private
int
m_nCurrentWavIndex
=
-1
;
public
AudioHelper()
{
//读取系统声音列表
try
{
MIDIPlayer
=
MidiSystem.getSequencer();
m_vAudio
=
findSound()
;
}
catch
(MidiUnavailableException
ex){ex.printStackTrace();}
}
public
static
final
Vector
findSound()
{
String
path
=
AppFrameBase.getApplicationDirectory()
;
File
file
=
new
File(path
+
"/Data/Audio/")
;
Vector
v
=
new
Vector()
;
File[]
fileList
=
file.listFiles();
for(int
i
=
0
;
i
<
fileList.length
;
i++)
if
(fileList[i].isFile())
v.addElement(fileList[i].getName());
return
v
;
}
public
static
final
Sequence
loadSound(String
name)
{
String
path
=
AppFrameBase.getApplicationDirectory()
+
"/Data/Audio/"
+
name
;
return
loadSoundAsPath(path)
;
}
public
static
final
Sequence
loadSoundAsPath(String
path)
{
try
{
return
MidiSystem.getSequence(new
File(path));
}
catch
(IOException
ex)
{
ex.printStackTrace();
return
null;
}
catch
(InvalidMidiDataException
ex)
{
ex.printStackTrace();
return
null;
}
}
public
final
String
getSoundPath(int
index)
{
if
(index
<
0
||
index
>=
m_vAudio.size())
return
null
;
return
m_vAudio.elementAt(index).toString()
;
}
public
static
final
AudioClip
loadAudioClip(String
name)
{
String
path
=
AppFrameBase.getApplicationDirectory()
;
return
loadAudioClipAsPath(path
+
"/Data/Audio/"
+
name)
;
}
public
static
final
AudioClip
loadAudioClipAsPath(String
path)
{
try
{
return
Applet.newAudioClip(new
URL("file:"
+
path));
}
catch
(MalformedURLException
ex)
{
return
null;
}
}
public
void
stopSound()
{
stopMIDI()
;
stopWAV()
;
}
public
void
stopMIDI()
{
if
(MIDIPlayer
!=
null)
{
if
(MIDIPlayer.isRunning())
MIDIPlayer.stop();
}
}
public
void
notifyStopMIDI()
{
stopMIDI()
;
if
(MIDIPlayer
!=
null)
if
(MIDIPlayer.isOpen())
MIDIPlayer.close();
}
public
void
stopWAV()
{
if
(WAVPlayer
!=
null)
WAVPlayer.stop()
;
}
public
void
loopMIDI(boolean
loop)
{
if
(MIDIPlayer
!=
null)
if
(loop)
{
MIDIPlayer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
}
else
{
MIDIPlayer.setLoopCount(0);
}
}
public
void
loopWAV(boolean
loop)
{
if
(WAVPlayer
!=
null)
if
(loop)
WAVPlayer.loop()
;
else
WAVPlayer.play()
;
}
public
void
playSound(int
index,boolean
loop)
{
String
name
=
getSoundPath(index)
;
if
(name
==
null)
throw
new
IndexOutOfBoundsException("Sound
index
"
+
index
+
"
is
out
of
current
sound
index
max!")
;
try
{
if
(isMidiFile(name))
{
if
(m_nCurrentMidiIndex
!=
index
||
(m_nCurrentMidiIndex
==
index
&&
!MIDIPlayer.isRunning()))
{
stopMIDI();
if
(m_nCurrentMidiIndex
!=
index)
{
Sequence
se
=
loadSound(name);
MIDIPlayer.setSequence(se);
}
MIDIPlayer.open();
loopMIDI(loop);
MIDIPlayer.start();
m_nCurrentMidiIndex
=
index
;
}
}
else
if
(isWavFile(name))
{
if
(m_nCurrentWavIndex
!=
index)
{
WAVPlayer
=
AudioHelper.loadAudioClip(name);
m_nCurrentWavIndex
=
index
;
}
if
(WAVPlayer
!=
null)
{
WAVPlayer.stop();
if
(loop)
WAVPlayer.loop()
;
else
WAVPlayer.play();
}
}
}
catch
(InvalidMidiDataException
ex)
{
ex.printStackTrace();
}
catch
(MidiUnavailableException
ex)
{
ex.printStackTrace();
}
}
public
static
final
boolean
isMidiFile(String
name)
{
return
name.indexOf(".mid")
!=
-1
;
}
public
static
final
boolean
isWavFile(String
name)
{
return
name.indexOf(".wav")
!=
-1
;
}
}
这是我自己写的一个MIDI和Wav的管理类,有载入播放停止等等,但是音量和节拍管理没有,要想实现高级功能最好使用jmf,基于j2se:
import
java.io.File;
import
java.io.IOException;
import
java.util.Vector;
import
App.AppFrameBase;
import
javax.sound.midi.MidiSystem;
import
java.applet.AudioClip;
import
java.applet.Applet;
import
javax.sound.midi.Sequence;
import
javax.sound.midi.InvalidMidiDataException;
import
java.net.URL;
import
java.net.MalformedURLException;
import
javax.sound.midi.Sequencer;
import
javax.sound.midi.MidiUnavailableException;
public
class
AudioHelper
{
public
Sequencer
MIDIPlayer
;
public
AudioClip
WAVPlayer
;
public
Vector
m_vAudio
=
new
Vector()
;
private
int
m_nCurrentMidiIndex
=
-1
;
private
int
m_nCurrentWavIndex
=
-1
;
public
AudioHelper()
{
//读取系统声音列表
try
{
MIDIPlayer
=
MidiSystem.getSequencer();
m_vAudio
=
findSound()
;
}
catch
(MidiUnavailableException
ex){ex.printStackTrace();}
}
public
static
final
Vector
findSound()
{
String
path
=
AppFrameBase.getApplicationDirectory()
;
File
file
=
new
File(path
+
"/Data/Audio/")
;
Vector
v
=
new
Vector()
;
File[]
fileList
=
file.listFiles();
for(int
i
=
0
;
i
<
fileList.length
;
i++)
if
(fileList[i].isFile())
v.addElement(fileList[i].getName());
return
v
;
}
public
static
final
Sequence
loadSound(String
name)
{
String
path
=
AppFrameBase.getApplicationDirectory()
+
"/Data/Audio/"
+
name
;
return
loadSoundAsPath(path)
;
}
public
static
final
Sequence
loadSoundAsPath(String
path)
{
try
{
return
MidiSystem.getSequence(new
File(path));
}
catch
(IOException
ex)
{
ex.printStackTrace();
return
null;
}
catch
(InvalidMidiDataException
ex)
{
ex.printStackTrace();
return
null;
}
}
public
final
String
getSoundPath(int
index)
{
if
(index
<
0
||
index
>=
m_vAudio.size())
return
null
;
return
m_vAudio.elementAt(index).toString()
;
}
public
static
final
AudioClip
loadAudioClip(String
name)
{
String
path
=
AppFrameBase.getApplicationDirectory()
;
return
loadAudioClipAsPath(path
+
"/Data/Audio/"
+
name)
;
}
public
static
final
AudioClip
loadAudioClipAsPath(String
path)
{
try
{
return
Applet.newAudioClip(new
URL("file:"
+
path));
}
catch
(MalformedURLException
ex)
{
return
null;
}
}
public
void
stopSound()
{
stopMIDI()
;
stopWAV()
;
}
public
void
stopMIDI()
{
if
(MIDIPlayer
!=
null)
{
if
(MIDIPlayer.isRunning())
MIDIPlayer.stop();
}
}
public
void
notifyStopMIDI()
{
stopMIDI()
;
if
(MIDIPlayer
!=
null)
if
(MIDIPlayer.isOpen())
MIDIPlayer.close();
}
public
void
stopWAV()
{
if
(WAVPlayer
!=
null)
WAVPlayer.stop()
;
}
public
void
loopMIDI(boolean
loop)
{
if
(MIDIPlayer
!=
null)
if
(loop)
{
MIDIPlayer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
}
else
{
MIDIPlayer.setLoopCount(0);
}
}
public
void
loopWAV(boolean
loop)
{
if
(WAVPlayer
!=
null)
if
(loop)
WAVPlayer.loop()
;
else
WAVPlayer.play()
;
}
public
void
playSound(int
index,boolean
loop)
{
String
name
=
getSoundPath(index)
;
if
(name
==
null)
throw
new
IndexOutOfBoundsException("Sound
index
"
+
index
+
"
is
out
of
current
sound
index
max!")
;
try
{
if
(isMidiFile(name))
{
if
(m_nCurrentMidiIndex
!=
index
||
(m_nCurrentMidiIndex
==
index
&&
!MIDIPlayer.isRunning()))
{
stopMIDI();
if
(m_nCurrentMidiIndex
!=
index)
{
Sequence
se
=
loadSound(name);
MIDIPlayer.setSequence(se);
}
MIDIPlayer.open();
loopMIDI(loop);
MIDIPlayer.start();
m_nCurrentMidiIndex
=
index
;
}
}
else
if
(isWavFile(name))
{
if
(m_nCurrentWavIndex
!=
index)
{
WAVPlayer
=
AudioHelper.loadAudioClip(name);
m_nCurrentWavIndex
=
index
;
}
if
(WAVPlayer
!=
null)
{
WAVPlayer.stop();
if
(loop)
WAVPlayer.loop()
;
else
WAVPlayer.play();
}
}
}
catch
(InvalidMidiDataException
ex)
{
ex.printStackTrace();
}
catch
(MidiUnavailableException
ex)
{
ex.printStackTrace();
}
}
public
static
final
boolean
isMidiFile(String
name)
{
return
name.indexOf(".mid")
!=
-1
;
}
public
static
final
boolean
isWavFile(String
name)
{
return
name.indexOf(".wav")
!=
-1
;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询