我的想法是直接存储音乐流,就写了下面的代码。因为在手机上运行时发现要播放音乐时再创建流,画面会卡一会儿才继续。
hmIs = getClass().getResourceAsStream("/hitMenu.mid"); collIs = getClass().getResourceAsStream("/collision.mid"); goIs = getClass().getResourceAsStream("/gameOver.mid"); rgIs = getClass().getResourceAsStream("/rightGrid.mid"); wgIs = getClass().getResourceAsStream("/wrongGrid.mid");
可是这样子的写法只能第一次正常播放。其它时候就手机音乐抛异常:
javax.microedition.media.MediaException: Bad MIDI Format at com.sun.mmedia.QSoundMIDIPlayer.doRealize(+203) at com.sun.mmedia.BasicPlayer.realize(+84) at MusicPlayer.playMusic(+20) at MusicPlayer.playWrongGrid(+8) at TestCanvas.run(+73)
源代码如下,请问该如何修改呢?
/** 播放声音的类。 */public class MusicPlayer { private int soundVolume; private InputStream hmIs; private InputStream collIs; private InputStream goIs; private InputStream rgIs; private InputStream wgIs; public MusicPlayer() { soundVolume = 100; hmIs = getClass().getResourceAsStream("/hitMenu.mid"); collIs = getClass().getResourceAsStream("/collision.mid"); goIs = getClass().getResourceAsStream("/gameOver.mid"); rgIs = getClass().getResourceAsStream("/rightGrid.mid"); wgIs = getClass().getResourceAsStream("/wrongGrid.mid"); } public void playHitMenu() { playMusic(hmIs); } public void playCollision() { playMusic(collIs); } public void playGameOver() { playMusic(goIs); } public void playRightGrid() { playMusic(rgIs); } public void playWrongGrid() { playMusic(wgIs); } public void playMusic(InputStream is) { if (soundVolume != 0) { try { Player player = Manager.createPlayer(is, "audio/midi"); player.realize(); VolumeControl control = (VolumeControl) player .getControl("VolumeControl"); if (control != null) { control.setLevel(soundVolume); } player.prefetch(); player.start(); // player.stop(); } catch (MediaException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }}//Canvas类public class TestCanvas extends Canvas implements Runnable { private int key = 0; private MusicPlayer musicPlayer = new MusicPlayer(); protected void paint(Graphics g) { g.setColor(0); g.fillRect(0, 0, 240, 310); g.setColor(0xffff00); g.drawString("1.hitMenu", 120, 50, Graphics.HCENTER | Graphics.TOP); g.drawString("2.collision", 120, 80, Graphics.HCENTER | Graphics.TOP); g.drawString("3.rightGrid", 120, 110, Graphics.HCENTER | Graphics.TOP); g.drawString("4.wrongGrid", 120, 140, Graphics.HCENTER | Graphics.TOP); g.drawString("5.gameOver", 120, 170, Graphics.HCENTER | Graphics.TOP); } public void run() { while (true) { if (key == 49) { musicPlayer.playHitMenu(); } else if (key == 50) { musicPlayer.playCollision(); } else if (key == 51) { musicPlayer.playRightGrid(); } else if (key == 52) { musicPlayer.playWrongGrid(); } else if (key == 53) { musicPlayer.playGameOver(); } key = 0; } } protected void keyPressed(int k) { key = k; }}
------解决方法--------------------------------------------------------
你这样写,每种声音只能播放一次,播完一次后流就读到末尾,再运行就出错了
------解决方法--------------------------------------------------------
reset一下就行了,
------解决方法--------------------------------------------------------
刚在模拟器上用LeoTang2说的reset测了下,效果不错。你可以测下真机上效果怎样。