Javaオーディオの再生は、ローカル環境に依存する必要があるため、Javaはオーディオ処理ではほとんど利点がありません。つまり、Javaシステムを開発する際のオーディオ再生要因を考慮しません。初期のJava 1.1バージョンでは、後にjavax.soundパッケージはありませんでした。
残念ながら、グラフィックプログラムの開発において、当社のプログラムは必然的にバックグラウンドミュージック、効果サウンドなどを使用して、画像操作に協力する必要があります。悲しいかな、これは本当にマスターサンが私たちに演奏した冗談です。幸いなことに、マスターサンは彼の目を開けて、javax.soundパッケージを提供してくれました。
しかし、その後の問題は、javax.soundパッケージの使用では、Javaマルチメディアツールクラスの一般的な問題と同様に、非常に完全なリリースメカニズムがないことです。 Windows開発を行うと、Mediaplayerを何度も呼び出すことは大したことではないかもしれませんが、Javaでは、オーディオプログラムが繰り返し実行された場合、メモリの累積損失を経験するのが非常に簡単であるため、Java.lang.outofmemoryErrorが投げられます。
これはすでに「許容できる」という問題ですか?これを考慮して、私のルーンフレームワークのフレームワーク開発では、関連するメソッドをサウンドの下に二次的に統合し、最も単純なコードで最も完全なオーディオコントロールクラスを作成するよう努めています。 LoonFramework-Gameがまだ大きな成功を収めていないので、まず誰もがそれらを見るための方法のいくつかを抜粋します!
ネットワークリソースコールに対応して、当社のURIクラスはLoonFrameworkで確立されています。基本的なコンテンツは次のとおりです。
(StreamHelperがLoonframework独自のストリーミングコントロールクラスである場合、Gethttpstreamメソッドを自分で交換してください。)
Package org.loon.framework.game.net; import org.loon.framework.game.helper.streamhelper;/** * // ** * <p> * title:title:loonframework * </p> * <p> *説明:loonframework専用URI(統一されたリソース識別子) <p> company:loonframework * </p> * * @author chenpeng * @email:[email protected] * @version 0.1 */public class uri ... {// Transport Protocol Type Public Static Final int _l_uri_http = 1; public static final int _l_uri_udp = 2;プライベートストリング_uri; private int _type; /** * // ** * Destructor、URIおよびType * * @Param URI * @Param Type */public URI(String URI、int Type)... {_uri = new String(uri); _type = type; }/** * // ** Destructor、uri * * @param type uri */public uri(string uri)... {_uri = new String(uri); _type = uri._l_uri_http; }/*** // *** URIがあるリソースのバイト配列を返します。 * * @return */ public byte [] getData()... {if(_uri == null)... {return null; } returchhelper.gethttpstream(_uri); } public string geturi()... {return _uri; } public int getType()... {return _type; }} loonframeworkフレームワークでは、基本的なSoundDataクラスがカスタマイズされ、オーディオデータソースが均一に管理されています。 Package org.loon.framework.game.sound; Import org.loon.framework.game.helper.streamhelper; Import org.loon.framework.game.net.uri;/ * // ** * <p> * <p> * title:loonframework * </p> * <p> *フレームワーク) * </p> * <p> * Copyright:Copyright(c)2007 * </p> * company:loonframework * </p> * * @author chenpeng * @email:[email protected] * @version 0.1 */public class sounddata ... {private byte [] _data;プライベートブール_loop; private int _type; public static final int _l_soundtype_midi = 1; public static final int _l_soundtype_wav = 2; /** * // ** Destructor、URI、Type、loop * * @param uri * @param type * @param loop */public sounddata(uri uri、int type、boolean loop)... {if(uri!= null)... {_data = uri.getdata(); } _type = type; _loop = loop; }/** * // ** Destructor、データ、タイプ、ループ * * * @param Type * @param loop */public sounddata(byte [] data、int type、boolean loop)... {if(data!= null && data.length> 0)... {__data = byte [late length]; // direct byte array system.arraycopy(data、0、_data、0、_data.length); } _type = type; _loop = loop; }/** * // ** Destructor、resname、type、loop * @param resname * @param type * @param loop */public sounddata(string resname、int type、boolean loop)... {this(stramehelper.getdatasource(resname)、type、loop); } public byte [] getData()... {return _data; } public boolean getloop()... {return _loop; } public void setloop(boolean loop)... {_loop = loop; } public int getType()... {return _type; }}
LoonFrameworkは、オーディオ再生関連の方法とSoundPlayをカプセル化します。プログラマーは、javax.soundの内部詳細を無視し、SoundPlayを直接呼び出して関連する操作を完了できます。
Package org.loon.framework.game.sound; import java.io.bytearrayinputStream; Import javax.sound.midi.metaeventlistener; Import javax.sound.midi.metamessage; Import javax.sound.midi.midisystem; Import.sound.midisysment; javax.sound.midi.sequencer; import javax.sound.sampled.audiofileformat; import javax.sound.sampled.audiosystem; import javax.sound.sampled.clip; import javax.sound.saund.sampled.dataline; import inmolon.lamework.game.game.-net <p>タイトル:Loonframework * </p> * <p> *説明:サウンドファイル操作を実行するために使用されます(ルーンフレームワークのいくつかの方法のみ、詳細についてはルーンフレームワークゲームフレームワークを参照) * </p> * <p> *著作権:著作権(c)2007 * </p> * <p> @email:[email protected] * @version 0.1 */public class soundplayはmetaeventlistener、runnable ... {private int _sleeptime;プライベートクリップ_audio;プライベートシーケンサー_midi;プライベートブール_loop; private int _soundType;プライベートブール_playing;プライベートスレッド_thread = null;プライベートブール_isrun = false; /** * // ** Destructor、SoundPlayを初期化 * */public SoundPlay()... {_loop = false; _SoundType = 0; _Sleeptime = 1000; _playing = false; } //サウンドファイルのパブリックブールロード(soundDataデータ)... {reset(); if(data == null || data.getData()== null)... {return false; } return init(data.getData()、data.getType()、data.getloop()); }/** * // ** * URLファイルの直接再生 * * @param uri * @param ftype * @param loop * @return */public boolean load(uri uri、int ftype、boolean loop)... {// Data Reset(); if(uri == null)... {return false; } // sounddata sounddata data = new sounddata(uri、ftype、loop); if(data == null || data.getData()== null)... {return false; } return init(data.getData()、data.getType()、data.getloop()); }/** * // ** *サウンド関連データを初期化 * * @paramデータ * @param ftype * @param loop * @return */private boolean init(byte [] data、int ftype、boolean loop)... {boolean result = fals; bytearrayinputStream bis = null; try ... {bis = new bytearrayinputStream(data); } catch(例外e)... {bis = null; } if(bis == null)... {return false; } // Judging Type Switch(FTYPE)... {// MIDI CASE SOUNDDATA._L_SOUNDTYPE_MIDI:// MIDIが存在しない場合(_midi == null)... {try ... {// get sequencer _midi = midisystem.getEncerser(); _midi.open(); } catch(例外Ex)... {_midi = null; } if(_midi!= null)... {_midi.addmetaeventlistener(this); }} // MIDIがまだ取得されていない場合(_midi!= null)... {// recreate sequence sequence sc = null; try ... {sc = midisystem.get sequence(bis); } catch(例外e)... {sc = null; } if(sc!= null)... {try ... {_midi.set sequence(sc); // loop_loop = loop; //結果= trueをロードするかどうかを取得します。 } catch(Exception EE)... {} // sound type_soundType = soundData._L_SOUNDTYPE_MIDIを取得します; }} try ... {bis.close(); } catch(例外ee)... {} break; // WAV CASE SOUNDDATA._L_SOUNDTYPE_WAV:audiofileformat type = null; // audio try ... {type = audiosystem.getaudiofileformat(bis); } catch(例外e)... {type = null; } //クローズストリームtry ... {bis.close(); } catch(例外Ex)... {} if(type == null)... {return false; } //指定された情報dataline.info di = new dataline.info(lip.class、type.getFormat())に基づいてデータ行の情報オブジェクトを作成します。 //クリップに変換するtry ... {_audio =(clip)audiosystem.getline(di); } catch(Exception e)... {} //ファイルを再生する... {_audio.open(type.getFormat()、data、0、data.length); _loop = loop; result = true; } catch(Exception E)... {} //ファイルタイプを取得_soundType = soundData._L_SOUNDTYPE_WAV;壊す; } return result; } public boolean play(sounddata data)... {if(!load(data))... {return false; } return play(); } public boolean play()... {switch(_soundtype)... {case sounddata._l_soundtype_midi:try ... {_midi.start(); _playing = true; _soundType = soundData._L_SOUNDTYPE_MIDI; } catch(例外ee)... {} break; case sounddata._l_soundtype_wav:if(_audio!= null)... {if(_loop)... {// set loop _audio.setlooppoints(0、-1); _audio.setFrameposition(0); _audio.loop(clip.loop_continuourly); } else ... {//再生位置を0 _audio.setframeposition(0)に強制します。 _audio.start(); } _playing = true; } 壊す; } return _playing; }/*** // ***自動再生、停止後にループが終了します。 * * @param data * @return */ public boolean autoplay(sounddata data)... {if(!load(data))... {return false; } autoplay()を返します。 }/*** // ***自動再生、停止後にループが終了します。 * * @return */ public boolean autoplay()... {_isrun = true; _thread = newスレッド(this); _thread.start(); _playingを返します。 }/***// ***再生を停止*/public void stop()... {if(_audio!= null && _audio.isactive())... {try ... {_audio.stop(); } catch(例外e)... {}} if(_midi!= null)... {_midi.stop(); } _playing = false; _isrun = false; }/*** // ***データのリリース**/public void reset()... {stop(); _loop = false; _SoundType = 0; if(_midi!= null)... {_midi.close(); _midi = null; } if(_audio!= null && _audio.isopen())... {_audio.close(); _audio = null; } _isrun = false; _thread = null; }/***// ***Metamessage*/public void meta(metamessage meta)... {// midi loopを再生するかどうかを判断します。 _midi.isopen())... {_midi.setMicroSeCondposition(0); _midi.start(); }}} public void run()... {while(_isrun)... {play(); //再生タイプは一意であるため、これを決定するために1つの_playing結果のみが返されます。 if(_midi!= null)... {_playing = _midi.isrunning(); } if(_audio!= null)... {_playing = _audio.isrunning(); } //再生が停止した場合(!_playing)... {// release reset(); } try ... {thread.sleep(_Sleeptime); } catch(arternedexception e)... {e.printstacktrace(); }}} public int getSleeptime()... {return _sleeptime; }/*** // ***オートプレイスレッドループ時間を設定します。 * * @param time */ public void setsleeptime(int time)... {_sleeptime = time; }}
この時点で、私たちが直面する必要があるのは、複雑なJavax.soundに再び対処することなく、エンティティとしてカプセル化されたSoundDataデータとSoundPlay操作のみです。
通話方法は次のとおりです。
パッケージorg.test; import org.loon.framework.game.helper.streamhelper; Import org.loon.framework.game.net.uri; Import org.loon.framework.game.sound.sounddata; import org.loon.framework.game.sound.sound.soundplay; loonframework </p> * <p>説明:SoundPlay Playback Test </p> * <p> Copyright:Copyright(c)2007 </p> * <p> company:loonframework </p> * @author chenpeng * @email:[email protected] * @version 0.1 */public class soundplaytest ... ftype).. {sounddata data = null; switch(ftype).. {//ルーンフレームワークの下でURIを介してネットワークから音楽を再生するケース0:data = new uri( "http://looframework.sourceforge.net/midi/who is the big Hero.mid")、sounddata._l_soundtype_midi、false);壊す; //ローカルリソースケースの下で音楽ファイルのバイト[]オブジェクトを介して音楽を再生します1:byte [] bytes = stramehelper.getResourcedata( "/midi/who is the big hero.mid"); data = new SoundData(BYTES、SOUNDDATA._L_SOUNDTYPE_MIDI、FALSE);壊す; //音楽ファイルパスを介して音楽を再生します。壊す; } soundplay play = new SoundPlay(); //オートプレイとプレイ方法の違いは、Autoplayがプレイ後に自動的にリソースを停止およびリリースすることであり、プレイを手動で中止する必要があることです。 //play.play(data); play.autoplay(data); } public static void main(string [] args).. {selectplay(2); }}
Loonframeworkゲームが完全に発表された後、より詳細な方法について説明します。
さらに:StreamHelperは他のルーンフレームワークメソッドに関連付けられているため、まだ与えられていません。 inputstreamからbyte []は、次のように記述できます。
// ISは取得された入力bytearrayoutputStream bytearrayoutputStream = new bytearrayoutputStream(); // byte [] byte [] arraybyte = null; try ... {//各転送のサイズは4096バイト[] bytes = new byte [4096]; bytes = new byte [is.abailable()]; int read; while((read = is.read(bytes))> = 0)... {bytearrayoutputStream.write(bytes、0、read); } arraybyte = bytearrayoutputstream.tobytearray(); } catch(ioException e)... {null; }最後に... {try ... {if(bytearrayoutputStream!= null)... {bytearrayoutputStream.close(); bytearrayoutputStream = null; } if(is!= null)... {is.close(); IS = null; }} catch(ioexception e)... {}}