โดยAurélien da Campo (Adolio)
ไปที่โฟลเดอร์สาธิตกำหนดค่าโครงการและเรียกใช้หรือถ้าคุณได้รับรีลีสล่าสุดควรมีการสาธิตไบนารีในไฟล์เก็บถาวร
คลาสหลักในการลงทะเบียนแทร็กและอินสแตนซ์เสียง ตัวอย่างต่อไปนี้ลงทะเบียนสองแทร็ก (MP3 & WAV) ตั้งค่าตัวเลือกการตัดแต่งและการสุ่มตัวอย่าง จากนั้นสามเสียงจะถูกสร้างอินสแตนซ์
// Embedded sounds (Note that WAV files requires a mime type)
[ Embed( source = "../media/sound/engine.mp3" ) ] public static const EngineSoundMp3 : Class ;
[ Embed( source = "../media/sound/engine.wav" , mimeType = "application/octet-stream" ) ] public static const EngineSoundWav : Class ;
private var _soundManager : SoundManager ;
public function SoundManagerExample()
{
// Create Sound Manager
_soundManager = new SoundManager();
_soundManager.maxChannelCapacity = 8; // Limit number of simultaneous playing sounds (for this sound manager only)
_soundManager.isPoolingEnabled = true; // Enable pooling to re-used already instantiated sounds and reduce GC impacts
// Register sounds
var engineSoundMp3Config:TrackConfiguration = _soundManager.registerTrack( "Engine 1" , new Mp3Track(new EngineSoundMp3())); // Register a MP3 track
var engineSoundWavConfig:TrackConfiguration = _soundManager.registerTrack( "Engine 2" , new WavTrack(new EngineSoundWav())); // Register a WAV track
// Trimming configuration
engineSoundMp3Config.findTrim(0.01); // Find start & end trim durations to remove silences inherent to mp3 format (with a silent threshold of 0.01)
// Sampling rate configuration
engineSoundWavConfig.sampling = 4096; // This defines how much samples are read per sampling request. The value must be between 2048 (included) and 8192 (included).
// Base volume configuration for mastering
engineSoundMp3Config.baseVolume = 0.5; // Custom mastering at track configuration level, the sound can now be player at 1.0 but will still be influenced by this mastering.
// Play sounds
var engine1_once:SoundInstance = _soundManager.play( "Engine 1" , 1.0); // Play once
var engine1_twice:SoundInstance = _soundManager.play( "Engine 1" , 1.0, 0, 1); // Play twice (repeat once)
var engine2_infinite:SoundInstance = _soundManager.play( "Engine 2" , 1.0, 0, -1); // Play infinite
// Control master volume
_soundManager.volume = 0.8;
}อินสแตนซ์ของเสียงที่สามารถควบคุมได้ ตัวอย่างต่อไปนี้แสดงวิธีฟังเหตุการณ์เสียงวิธีควบคุมเสียงและวิธีรับข้อมูลต่าง ๆ เกี่ยวกับมัน
var si : SoundInstance = _soundManager . play ( "Engine 1" , 1.0 , 1337 , 5 ) ; // Play at max volume, starting at 1,337 sec., 6 times (looping 5 times)
//-----------------------------------------------------------------------------
//-- Events
//-----------------------------------------------------------------------------
si . started . add ( function (si : SoundInstance) : void { trace( "Sound started. " + si.type); } ) ;
si . stopped . add ( function (si : SoundInstance) : void { trace( "Sound stopped. " + si.type); } ) ;
si . paused . add ( function (si : SoundInstance) : void { trace( "Sound paused. " + si.type); } ) ;
si . resumed . add ( function (si : SoundInstance) : void { trace( "Sound resumed. " + si.type); } ) ;
si . completed . add ( function (si : SoundInstance) : void { trace( "Sound completed. " + si.type); } ) ;
si . destroyed . add ( function (si : SoundInstance) : void { trace( "Sound destroyed. " + si.type); } ) ;
//-----------------------------------------------------------------------------
//-- Controls
//-----------------------------------------------------------------------------
// Execution controls
si . pause () ;
si . resume () ;
si . stop () ;
si . play ( 0.5 , 0 , 1.0 ) ; // (Re)play twice (repeat once)
// Position
si . position = 1337 ; // In milliseconds
si . positionRatio = 0.5 ; // This includes the loops so here it will play only the second loop!
// Mute
si . isMuted = true ;
si . isMuted = false ;
// Volume
si . volume = 0 ;
si . volume = 0.8 ;
// Pan
si . pan = - 0.5 ; // Half left
// Pitch
si . pitch = 0.8 ; // 20% slower
si . pitch = 1.2 ; // 20% faster
si . pitch = 1.0 ; // Back to default
//-----------------------------------------------------------------------------
//-- Status info
//-----------------------------------------------------------------------------
trace ( "Type: " + si . type ) ; // Type
trace ( "Volume: " + si . volume ) ; // Volume
trace ( "Mixed volume: " + si . mixedVolume) ; // Equals to _soundManager.volume * volume
trace ( "Pan: " + si . pan ) ; // Pan
trace ( "Pitch: " + si . pitch) ; // Pitch
trace ( "Sound length: " + si . length ) ; // One loop length in milliseconds (trimmed)
trace ( "Sound total length: " + si . totalLength) ; // Total length in milliseconds (including loops & trim durations)
trace ( "Loops: " + si . loops ) ; // Total loops
trace ( "Current loop: " + si . currentLoop) ; // Current loop (-1 = infinite, 0 = first play, 1 = second play, etc.)
trace ( "Remaining loops: " + si . loopsRemaining) ; // Remaining loops
trace ( "Remaining time: " + si . remainingTime) ; // Remaining time in milliseconds
trace ( "Current position: " + si . position ) ; // Current position in milliseconds
trace ( "Current position (ratio): " + si . positionRatio) ; // Current position (ratio, between 0..1)
trace ( "Is started: " + si . isStarted) ; // Is started?
trace ( "Is playing: " + si . isPlaying ) ; // Is playing?
trace ( "Is paused: " + si . isPaused) ; // Is paused?
trace ( "Is muted: " + si . isMuted) ; // Is muted?
trace ( "Is destroyed: " + si . isDestroyed) ; // Is destroyed? การรวมกลุ่มสามารถลดการใช้หน่วยความจำได้อย่างมีนัยสำคัญโดยใช้ Sound Instances ที่มีการสร้างอินสแตนซ์อีกครั้ง สิ่งนี้ควรลดความล่าช้าที่เกิดจากเส้นทาง Garbage Collector ดังนั้นความสามารถนี้อาจมีประโยชน์ค่อนข้างมากเมื่อเล่นเสียงที่เหมือนกันมากมายในแอปพลิเคชันของคุณ (เช่นเสียงฝีเท้าของผู้เล่นลูกศรที่กองทัพของนักธนู ฯลฯ )
ความสามารถในการรวมกลุ่มสามารถเปิดใช้งานได้ด้วยวิธีนี้:
_soundManager . isPoolingEnabled = true ; สำคัญ : เมื่อ Sound Instance เสร็จสิ้นมันจะถูกส่งกลับไปยังสระว่ายน้ำโดยอัตโนมัติ ระวังที่จะไม่เก็บข้อมูล Sound Instances ที่เสร็จสมบูรณ์เพราะพวกเขาอาจถูกนำกลับมาใช้ใหม่อีกครั้งในไม่ช้า
เสียงวนรอบที่ไม่มีที่สิ้นสุด (เช่นเสียงวนรอบที่ไร้รอยต่อ) สามารถส่งคืนไปยังสระด้วยตนเองได้โดยเรียกวิธีการต่อไปนี้:
_soundManager . releaseSoundInstanceToPool(mySound) ; สำคัญ : อย่าทำลาย Sound Instances เมื่อความสามารถในการรวมกำลังทำงานอยู่ สิ่งนี้จะทำลายกลไกการรีไซเคิลวัตถุ
TrackConfiguration.findTrim()src ใน classpath ของคุณหรือคัดลอกวางเนื้อหาของโฟลเดอร์ src ในโฟลเดอร์ต้นทางของคุณหรือ
.swc ที่ให้ไว้ในแต่ละรุ่นอย่าลืมที่จะรวมการพึ่งพา (ดูด้านล่าง)
libs )