import sun.audio.*
import java.io.*
public class playsound{
public static void main(String[] args){
try {
FileInputStream fileau=new FileInputStream("D:/01.mid" )
AudioStream as=new AudioStream(fileau)
AudioPlayer.player.start(as)
}
catch (Exception e) {System.out.println("失败咯!")}
}
}
(二)
import javax.swing.*
import javax.sound.midi.*
import java.awt.GridLayout
import java.io.File
public class PlayMidi extends JFrame{
PlayMidi(String song)
{
super("Pla MIDI Files")
setSize(180,100)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
MidiPanel midi=new MidiPanel(song)
JPanel pane=new JPanel()
pane.add(midi)
setContentPane(pane)
show()
}
public static void main(String[] arguments)
{
if(arguments.length != 1)
{
System.out.println("Usage: java PlayMidi filename")
}
else
{
PlayMidi pm=new PlayMidi(arguments[0])
}
}
}
class MidiPanel extends JPanel implements Runnable
{
Thread runner
JProgressBar progress=new JProgressBar()
Sequence currentSound//音序
Sequencer player//默认音序器
String songFile//歌曲
MidiPanel(String song)
{
super()
songFile=song
JLabel label=new JLabel("者芹Playing file...")
setLayout(new GridLayout(2,1))
add(label)
add(progress)
if(runner == null)
{
runner=new Thread(this)
runner.start()
}
}
public void run()
{
try
{
System.out.println("11111")
System.out.println(songFile)
File file=new File(songFile)
System.out.println("22222")
currentSound=MidiSystem.getSequence(file)//获取音序文件
System.out.println("33333")
player=MidiSystem.getSequencer()//获取音序器
System.out.println("4444")
player.open()
player.setSequence(currentSound)//社定音序器播放指定音乐文件
progress.setMinimum(0)
System.out.println("失败咯!")
progress.setMaximum((int)player.getMicrosecondLength())//设置最大位歌曲时乱嫌瞎间
player.start()
while(player.isRunning())
{
progress.setValue((int)player.getMicrosecondPosition())//设置播放文件哗空显示当前播放进度
try
{
Thread.sleep(1000)
}
catch(InterruptedException e){System.out.println("失败咯!11")}
}
progress.setValue((int)player.getMicrosecondPosition())//同上
player.close()
}
catch(Exception ex)
{
System.out.println("失败咯!22")
System.out.println(ex.toString())
}
}
}
在JAVA.applet中有AudioClip类支持播放,但好像只支持播放au和 wav格式的代码:
AudioClip ac=getAudioClip (getCodeBase(), File)
// 创 建Audioclip 对 象 并 装 入.au 文 件
ac.play()/巧历物/ 播 放 一 次
ac.stop()// 停 止孝液 播 放
ac.loop()// 循 环烂改 播 放
可以通过Service来播放背景音乐,以下是实现代码:1.在AndroidManifest.xml文件中的<application>标签内加扮仔斗入下边语句
<service android:name=".MusicServer">
<intent-filter>
<action android:name="com.angel.Android.MUSIC"/>
<category android:name="android.intent.category.default" />
</intent-filter>
</service>
2.新建MusicServer.java类,内容为
import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.IBinder
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){
super.onStart(intent, startId)
if(mediaPlayer==null){
// R.raw.mmp是资源文件,MP3格式的厅磨
mediaPlayer = MediaPlayer.create(this, R.raw.abc)
mediaPlayer.setLooping(true)
mediaPlayer.start()
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy()
mediaPlayer.stop()
}
}
3.将歌曲放入raw文件夹下,名称戚败为abc。
4.在Activity中加入代码
private Intent intent = new Intent("com.angel.Android.MUSIC")
onCreate方法中加入startService(intent)
就可以播放了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)