適用於 Adobe® Flash® Platform 的 ActionScript® 3.0 參考
首頁  |  隱藏套件和類別清單 |  套件  |  類別  |  新增內容  |  索引  |  附錄  |  為什麼顯示英文?
篩選: AIR 30.0 和更早版本, Flash Player 30.0 和更早版本, Flash Lite 4
Flex 4.6 和更早版本, Flash Pro CS6 和更早版本
隱藏篩選
flash.media 

Sound  - AS3

套件x

最上層
adobe.utils
air.desktop
air.net
air.update
air.update.events
coldfusion.air
coldfusion.air.errors
coldfusion.air.events
coldfusion.service
coldfusion.service.events
coldfusion.service.mxml
com.adobe.acm.solutions.authoring.domain.extensions
com.adobe.acm.solutions.ccr.domain.extensions
com.adobe.consulting.pst.vo
com.adobe.dct.component
com.adobe.dct.component.datadictionary
com.adobe.dct.component.datadictionaryElement
com.adobe.dct.component.dataElementsPanel
com.adobe.dct.component.toolbars
com.adobe.dct.event
com.adobe.dct.exp
com.adobe.dct.model
com.adobe.dct.service
com.adobe.dct.service.provider
com.adobe.dct.transfer
com.adobe.dct.util
com.adobe.dct.view
com.adobe.ep.taskmanagement.domain
com.adobe.ep.taskmanagement.event
com.adobe.ep.taskmanagement.filter
com.adobe.ep.taskmanagement.services
com.adobe.ep.taskmanagement.util
com.adobe.ep.ux.attachmentlist.component
com.adobe.ep.ux.attachmentlist.domain
com.adobe.ep.ux.attachmentlist.domain.events
com.adobe.ep.ux.attachmentlist.domain.renderers
com.adobe.ep.ux.attachmentlist.skin
com.adobe.ep.ux.attachmentlist.skin.renderers
com.adobe.ep.ux.content.event
com.adobe.ep.ux.content.factory
com.adobe.ep.ux.content.handlers
com.adobe.ep.ux.content.managers
com.adobe.ep.ux.content.model.asset
com.adobe.ep.ux.content.model.preview
com.adobe.ep.ux.content.model.relation
com.adobe.ep.ux.content.model.search
com.adobe.ep.ux.content.model.toolbar
com.adobe.ep.ux.content.search
com.adobe.ep.ux.content.services
com.adobe.ep.ux.content.services.load
com.adobe.ep.ux.content.services.permissions
com.adobe.ep.ux.content.services.preview
com.adobe.ep.ux.content.services.providers
com.adobe.ep.ux.content.services.query
com.adobe.ep.ux.content.services.relationships
com.adobe.ep.ux.content.services.search.lccontent
com.adobe.ep.ux.content.services.version
com.adobe.ep.ux.content.view
com.adobe.ep.ux.content.view.components.activate
com.adobe.ep.ux.content.view.components.grid
com.adobe.ep.ux.content.view.components.grid.hover
com.adobe.ep.ux.content.view.components.grid.hover.component
com.adobe.ep.ux.content.view.components.grid.renderers
com.adobe.ep.ux.content.view.components.relationships
com.adobe.ep.ux.content.view.components.review
com.adobe.ep.ux.content.view.components.search.renderers
com.adobe.ep.ux.content.view.components.searchpod
com.adobe.ep.ux.content.view.components.toolbar
com.adobe.ep.ux.content.view.components.toolbar.controlRenderers
com.adobe.ep.ux.content.view.components.version
com.adobe.ep.ux.documentsubmit.component
com.adobe.ep.ux.documentsubmit.domain
com.adobe.ep.ux.documentsubmit.skin
com.adobe.ep.ux.taskaction.component
com.adobe.ep.ux.taskaction.domain
com.adobe.ep.ux.taskaction.domain.events
com.adobe.ep.ux.taskaction.skin
com.adobe.ep.ux.taskdetails.component
com.adobe.ep.ux.taskdetails.domain
com.adobe.ep.ux.taskdetails.skin
com.adobe.ep.ux.tasklist.component
com.adobe.ep.ux.tasklist.domain
com.adobe.ep.ux.tasklist.skin
com.adobe.ep.ux.webdocumentviewer.domain
com.adobe.exm.expression
com.adobe.exm.expression.error
com.adobe.exm.expression.event
com.adobe.exm.expression.impl
com.adobe.fiber.runtime.lib
com.adobe.fiber.services
com.adobe.fiber.services.wrapper
com.adobe.fiber.styles
com.adobe.fiber.util
com.adobe.fiber.valueobjects
com.adobe.gravity.binding
com.adobe.gravity.context
com.adobe.gravity.flex.bundleloader
com.adobe.gravity.flex.progress
com.adobe.gravity.flex.serviceloader
com.adobe.gravity.framework
com.adobe.gravity.init
com.adobe.gravity.service.bundleloader
com.adobe.gravity.service.logging
com.adobe.gravity.service.manifest
com.adobe.gravity.service.progress
com.adobe.gravity.tracker
com.adobe.gravity.ui
com.adobe.gravity.utility
com.adobe.gravity.utility.async
com.adobe.gravity.utility.error
com.adobe.gravity.utility.events
com.adobe.gravity.utility.factory
com.adobe.gravity.utility.flex.async
com.adobe.gravity.utility.logging
com.adobe.gravity.utility.message
com.adobe.gravity.utility.sequence
com.adobe.gravity.utility.url
com.adobe.guides.control
com.adobe.guides.domain
com.adobe.guides.i18n
com.adobe.guides.spark.components.skins
com.adobe.guides.spark.components.skins.mx
com.adobe.guides.spark.headers.components
com.adobe.guides.spark.headers.skins
com.adobe.guides.spark.layouts.components
com.adobe.guides.spark.layouts.skins
com.adobe.guides.spark.navigators.components
com.adobe.guides.spark.navigators.renderers
com.adobe.guides.spark.navigators.skins
com.adobe.guides.spark.util
com.adobe.guides.spark.wrappers.components
com.adobe.guides.spark.wrappers.skins
com.adobe.guides.submit
com.adobe.icc.dc.domain
com.adobe.icc.dc.domain.factory
com.adobe.icc.editors.events
com.adobe.icc.editors.handlers
com.adobe.icc.editors.managers
com.adobe.icc.editors.model
com.adobe.icc.editors.model.config
com.adobe.icc.editors.model.el
com.adobe.icc.editors.model.el.operands
com.adobe.icc.editors.model.el.operators
com.adobe.icc.enum
com.adobe.icc.external.dc
com.adobe.icc.obj
com.adobe.icc.services
com.adobe.icc.services.category
com.adobe.icc.services.config
com.adobe.icc.services.download
com.adobe.icc.services.export
com.adobe.icc.services.external
com.adobe.icc.services.formbridge
com.adobe.icc.services.fragmentlayout
com.adobe.icc.services.layout
com.adobe.icc.services.letter
com.adobe.icc.services.locator
com.adobe.icc.services.module
com.adobe.icc.services.render
com.adobe.icc.services.submit
com.adobe.icc.services.user
com.adobe.icc.token
com.adobe.icc.vo
com.adobe.icc.vo.render
com.adobe.icomm.assetplacement.controller
com.adobe.icomm.assetplacement.controller.utils
com.adobe.icomm.assetplacement.data
com.adobe.icomm.assetplacement.model
com.adobe.livecycle.assetmanager.client
com.adobe.livecycle.assetmanager.client.event
com.adobe.livecycle.assetmanager.client.handler
com.adobe.livecycle.assetmanager.client.managers
com.adobe.livecycle.assetmanager.client.model
com.adobe.livecycle.assetmanager.client.model.cms
com.adobe.livecycle.assetmanager.client.service
com.adobe.livecycle.assetmanager.client.service.search
com.adobe.livecycle.assetmanager.client.service.search.cms
com.adobe.livecycle.assetmanager.client.utils
com.adobe.livecycle.content
com.adobe.livecycle.rca.model
com.adobe.livecycle.rca.model.constant
com.adobe.livecycle.rca.model.document
com.adobe.livecycle.rca.model.participant
com.adobe.livecycle.rca.model.reminder
com.adobe.livecycle.rca.model.stage
com.adobe.livecycle.rca.service
com.adobe.livecycle.rca.service.core
com.adobe.livecycle.rca.service.core.delegate
com.adobe.livecycle.rca.service.process
com.adobe.livecycle.rca.service.process.delegate
com.adobe.livecycle.rca.token
com.adobe.livecycle.ria.security.api
com.adobe.livecycle.ria.security.service
com.adobe.mosaic.layouts
com.adobe.mosaic.layouts.dragAndDrop
com.adobe.mosaic.layouts.interfaces
com.adobe.mosaic.mxml
com.adobe.mosaic.om.constants
com.adobe.mosaic.om.events
com.adobe.mosaic.om.impl
com.adobe.mosaic.om.interfaces
com.adobe.mosaic.skinning
com.adobe.mosaic.sparklib.editors
com.adobe.mosaic.sparklib.optionMenu
com.adobe.mosaic.sparklib.scrollableMenu
com.adobe.mosaic.sparklib.scrollableMenu.skins
com.adobe.mosaic.sparklib.tabLayout
com.adobe.mosaic.sparklib.tabLayout.events
com.adobe.mosaic.sparklib.tabLayout.layouts
com.adobe.mosaic.sparklib.tabLayout.skins
com.adobe.mosaic.sparklib.text
com.adobe.mosaic.sparklib.util
com.adobe.solutions.acm.authoring.presentation
com.adobe.solutions.acm.authoring.presentation.actionbar
com.adobe.solutions.acm.authoring.presentation.common
com.adobe.solutions.acm.authoring.presentation.events
com.adobe.solutions.acm.authoring.presentation.fragment
com.adobe.solutions.acm.authoring.presentation.letter
com.adobe.solutions.acm.authoring.presentation.letter.data
com.adobe.solutions.acm.authoring.presentation.preview
com.adobe.solutions.acm.authoring.presentation.rte
com.adobe.solutions.acm.ccr.presentation
com.adobe.solutions.acm.ccr.presentation.contentcapture
com.adobe.solutions.acm.ccr.presentation.contentcapture.events
com.adobe.solutions.acm.ccr.presentation.contentcapture.preview
com.adobe.solutions.acm.ccr.presentation.datacapture
com.adobe.solutions.acm.ccr.presentation.datacapture.renderers
com.adobe.solutions.acm.ccr.presentation.pdf
com.adobe.solutions.exm
com.adobe.solutions.exm.authoring
com.adobe.solutions.exm.authoring.components.controls
com.adobe.solutions.exm.authoring.components.toolbars
com.adobe.solutions.exm.authoring.domain
com.adobe.solutions.exm.authoring.domain.expression
com.adobe.solutions.exm.authoring.domain.impl
com.adobe.solutions.exm.authoring.domain.method
com.adobe.solutions.exm.authoring.domain.variable
com.adobe.solutions.exm.authoring.enum
com.adobe.solutions.exm.authoring.events
com.adobe.solutions.exm.authoring.model
com.adobe.solutions.exm.authoring.renderer
com.adobe.solutions.exm.authoring.view
com.adobe.solutions.exm.expression
com.adobe.solutions.exm.impl
com.adobe.solutions.exm.impl.method
com.adobe.solutions.exm.method
com.adobe.solutions.exm.mock
com.adobe.solutions.exm.mock.method
com.adobe.solutions.exm.runtime
com.adobe.solutions.exm.runtime.impl
com.adobe.solutions.exm.variable
com.adobe.solutions.prm.constant
com.adobe.solutions.prm.domain
com.adobe.solutions.prm.domain.factory
com.adobe.solutions.prm.domain.impl
com.adobe.solutions.prm.domain.manager
com.adobe.solutions.prm.presentation.asset
com.adobe.solutions.prm.presentation.constant
com.adobe.solutions.prm.presentation.document
com.adobe.solutions.prm.presentation.event
com.adobe.solutions.prm.presentation.file
com.adobe.solutions.prm.presentation.project
com.adobe.solutions.prm.presentation.team
com.adobe.solutions.prm.presentation.util
com.adobe.solutions.prm.service
com.adobe.solutions.prm.services.impl
com.adobe.solutions.prm.vo
com.adobe.solutions.rca.constant
com.adobe.solutions.rca.domain
com.adobe.solutions.rca.domain.common
com.adobe.solutions.rca.domain.factory
com.adobe.solutions.rca.domain.impl
com.adobe.solutions.rca.domain.impl.manager
com.adobe.solutions.rca.domain.manager
com.adobe.solutions.rca.presentation
com.adobe.solutions.rca.presentation.comment
com.adobe.solutions.rca.presentation.constant
com.adobe.solutions.rca.presentation.event
com.adobe.solutions.rca.presentation.gantt
com.adobe.solutions.rca.presentation.gantt.ruler
com.adobe.solutions.rca.presentation.template
com.adobe.solutions.rca.presentation.template.audit
com.adobe.solutions.rca.presentation.template.definition
com.adobe.solutions.rca.presentation.template.documents
com.adobe.solutions.rca.presentation.template.stages
com.adobe.solutions.rca.presentation.util
com.adobe.solutions.rca.service
com.adobe.solutions.rca.services.impl
com.adobe.solutions.rca.vo
com.adobe.solutions.rca.vo.um
com.adobe.viewsource
fl.accessibility
fl.containers
fl.controls
fl.controls.dataGridClasses
fl.controls.listClasses
fl.controls.progressBarClasses
fl.core
fl.data
fl.display
fl.events
fl.ik
fl.lang
fl.livepreview
fl.managers
fl.motion
fl.motion.easing
fl.rsl
fl.text
fl.transitions
fl.transitions.easing
fl.video
flash.accessibility
flash.concurrent
flash.crypto
flash.data
flash.desktop
flash.display
flash.display3D
flash.display3D.textures
flash.errors
flash.events
flash.external
flash.filesystem
flash.filters
flash.geom
flash.globalization
flash.html
flash.media
flash.net
flash.net.dns
flash.net.drm
flash.notifications
flash.permissions
flash.printing
flash.profiler
flash.sampler
flash.security
flash.sensors
flash.system
flash.text
flash.text.engine
flash.text.ime
flash.ui
flash.utils
flash.xml
flashx.textLayout
flashx.textLayout.compose
flashx.textLayout.container
flashx.textLayout.conversion
flashx.textLayout.edit
flashx.textLayout.elements
flashx.textLayout.events
flashx.textLayout.factory
flashx.textLayout.formats
flashx.textLayout.operations
flashx.textLayout.utils
flashx.undo
ga.controls
ga.layouts
ga.model
ga.uiComponents
ga.util
ga.views
ga.wrappers
lc.foundation
lc.foundation.domain
lc.foundation.events
lc.foundation.ui
lc.foundation.util
lc.preloader
lc.procmgmt
lc.procmgmt.commands
lc.procmgmt.domain
lc.procmgmt.events
lc.procmgmt.formbridge
lc.procmgmt.impl
lc.procmgmt.ui.attachments
lc.procmgmt.ui.controls
lc.procmgmt.ui.controls.card
lc.procmgmt.ui.controls.renderer
lc.procmgmt.ui.help
lc.procmgmt.ui.layout
lc.procmgmt.ui.presentationmodel
lc.procmgmt.ui.process
lc.procmgmt.ui.search
lc.procmgmt.ui.startpoint
lc.procmgmt.ui.task
lc.procmgmt.ui.task.form
lc.procmgmt.ui.task.form.commands
lc.procmgmt.ui.tracking
mx.accessibility
mx.automation
mx.automation.air
mx.automation.delegates
mx.automation.delegates.advancedDataGrid
mx.automation.delegates.charts
mx.automation.delegates.containers
mx.automation.delegates.controls
mx.automation.delegates.controls.dataGridClasses
mx.automation.delegates.controls.fileSystemClasses
mx.automation.delegates.core
mx.automation.delegates.flashflexkit
mx.automation.events
mx.binding
mx.binding.utils
mx.charts
mx.charts.chartClasses
mx.charts.effects
mx.charts.effects.effectClasses
mx.charts.events
mx.charts.renderers
mx.charts.series
mx.charts.series.items
mx.charts.series.renderData
mx.charts.styles
mx.collections
mx.collections.errors
mx.containers
mx.containers.accordionClasses
mx.containers.dividedBoxClasses
mx.containers.errors
mx.containers.utilityClasses
mx.controls
mx.controls.advancedDataGridClasses
mx.controls.dataGridClasses
mx.controls.listClasses
mx.controls.menuClasses
mx.controls.olapDataGridClasses
mx.controls.scrollClasses
mx.controls.sliderClasses
mx.controls.textClasses
mx.controls.treeClasses
mx.controls.videoClasses
mx.core
mx.core.windowClasses
mx.data
mx.data.crypto
mx.data.errors
mx.data.events
mx.data.messages
mx.data.mxml
mx.data.offline.errors
mx.data.utils
mx.effects
mx.effects.easing
mx.effects.effectClasses
mx.events
mx.filters
mx.flash
mx.formatters
mx.geom
mx.graphics
mx.graphics.codec
mx.graphics.shaderClasses
mx.logging
mx.logging.errors
mx.logging.targets
mx.managers
mx.messaging
mx.messaging.channels
mx.messaging.config
mx.messaging.errors
mx.messaging.events
mx.messaging.management
mx.messaging.messages
mx.modules
mx.netmon
mx.olap
mx.olap.aggregators
mx.preloaders
mx.printing
mx.resources
mx.rpc
mx.rpc.events
mx.rpc.http
mx.rpc.http.mxml
mx.rpc.livecycle
mx.rpc.mxml
mx.rpc.remoting
mx.rpc.remoting.mxml
mx.rpc.soap
mx.rpc.soap.mxml
mx.rpc.wsdl
mx.rpc.xml
mx.skins
mx.skins.halo
mx.skins.spark
mx.skins.wireframe
mx.skins.wireframe.windowChrome
mx.states
mx.styles
mx.utils
mx.validators
org.osmf.containers
org.osmf.display
org.osmf.elements
org.osmf.events
org.osmf.layout
org.osmf.logging
org.osmf.media
org.osmf.metadata
org.osmf.net
org.osmf.net.dvr
org.osmf.net.httpstreaming
org.osmf.net.metrics
org.osmf.net.qos
org.osmf.net.rtmpstreaming
org.osmf.net.rules
org.osmf.traits
org.osmf.utils
spark.accessibility
spark.automation.delegates
spark.automation.delegates.components
spark.automation.delegates.components.gridClasses
spark.automation.delegates.components.mediaClasses
spark.automation.delegates.components.supportClasses
spark.automation.delegates.skins.spark
spark.automation.events
spark.collections
spark.components
spark.components.calendarClasses
spark.components.gridClasses
spark.components.mediaClasses
spark.components.supportClasses
spark.components.windowClasses
spark.core
spark.effects
spark.effects.animation
spark.effects.easing
spark.effects.interpolation
spark.effects.supportClasses
spark.events
spark.filters
spark.formatters
spark.formatters.supportClasses
spark.globalization
spark.globalization.supportClasses
spark.layouts
spark.layouts.supportClasses
spark.managers
spark.modules
spark.preloaders
spark.primitives
spark.primitives.supportClasses
spark.skins
spark.skins.mobile
spark.skins.mobile.supportClasses
spark.skins.spark
spark.skins.spark.mediaClasses.fullScreen
spark.skins.spark.mediaClasses.normal
spark.skins.spark.windowChrome
spark.skins.wireframe
spark.skins.wireframe.mediaClasses
spark.skins.wireframe.mediaClasses.fullScreen
spark.transitions
spark.utils
spark.validators
spark.validators.supportClasses
xd.core.axm.enterprise.view.components
xd.core.axm.enterprise.view.skins
xd.core.axm.view.components
xd.core.axm.view.components.support

語言元素

全域常數
全域函數
運算子
陳述式、關鍵字和指令
特殊類型

附錄

新增內容
編譯器錯誤
編譯器警告
執行階段錯誤
移轉至 ActionScript 3
支援的字元集
僅限 MXML 標籤
移動 XML 元素
Timed Text 標籤
不建議元素清單
AccessibilityImplementation 常數
如何使用 ActionScript 範例
法律聲明
套件flash.media
類別public class Sound
繼承Sound Inheritance EventDispatcher Inheritance Object
子類別 SoundAsset

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

Sound 類別可讓您使用應用程式中的聲音。 Sound 類別可讓您建立 Sound 物件、將外部 MP3 檔載入該物件並進行播放、關閉聲音串流,以及存取有關聲音的資料,例如串流的位元組數目和 ID3 中繼資料。更詳細的聲音控制是透過聲音來源 (聲音的 SoundChannel 或 Microphone 物件),以及 SoundTransform 類別中控制電腦喇叭聲音輸出的屬性來進行。

在 Flash Player 10 和更新版本以及 AIR 1.5 和更新版本中,您也可以使用這個類別來處理動態產生的聲音。在這種情況下,Sound 物件會使用您指定給 sampleData 事件處理常式的函數來輪詢聲音資料。從您填入聲音資料的 ByteArray 物件中擷取聲音起,聲音便會播放。您可以使用 Sound.extract(),從 Sound 物件擷取聲音資料、操作此資料,然後再將它寫回串流來進行播放。

若要控制內嵌在 SWF 檔中的聲音,請使用 SoundMixer 類別中的屬性。

注意: ActionScript 3.0 的 Sound API 與 ActionScript 2.0 的不同。 在 ActionScript 3.0 中,您無法使用 Sound 物件並將它們排列在階層中,以控制其屬性。

使用此類別時,必須考慮下列安全性模型:

  • 如果呼叫的 檔在網路安全執行程序中,而要載入的聲音檔卻位於本機,則不允許載入並播放聲音。
  • 根據預設,如果呼叫的檔案位於本機而且嘗試載入並播放遠端聲音,則不允許載入並播放該聲音。使用者必須授與允許此存取類型的明確權限。
  • 處理聲音的特定作業會受到限制。 除非您實作跨網域原則檔,否則已載入聲音中的資料無法由不同網域中的 檔存取。 此限制下的聲音相關 API 為 Sound.id3SoundMixer.computeSpectrum()SoundMixer.bufferTimeSoundTransform 類別。

但在 Adobe AIR 中,application 安全執行程序中的內容 (使用 AIR 應用程式所安裝的內容) 沒有這些安全性限制。

如需安全性詳細資訊,請參閱「Flash Player 開發人員中心」主題:安全性

檢視範例

相關 API 元素



公用屬性
 屬性定義自
  bytesLoaded : uint
[唯讀] 會傳回此 Sound 物件中目前可用的位元組數目。
Sound
  bytesTotal : int
[唯讀] 會傳回此 Sound 物件中的位元組總數。
Sound
 Inheritedconstructor : Object
類別物件的參照或是特定物件實體的建構函數。
Object
  id3 : ID3Info
[唯讀] 提供存取屬於 MP3 檔一部份的中繼資料。
Sound
  isBuffering : Boolean
[唯讀] 會傳回外部 MP3 檔的緩衝狀態。
Sound
  isURLInaccessible : Boolean
[唯讀] 指出 Sound.url 屬性是否曾被截斷。
Sound
  length : Number
[唯讀] 目前聲音的長度,以毫秒為單位。
Sound
  url : String
[唯讀] 從中載入此聲音的 URL。
Sound
公用方法
 方法定義自
  
Sound(stream:URLRequest = null, context:SoundLoaderContext = null)
會建立新的 Sound 物件。
Sound
 Inherited
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
會在 EventDispatcher 物件註冊事件偵聽程式,以便讓偵聽程式收到事件的通知。
EventDispatcher
  
會關閉串流,因而導致資料的下載作業終止。
Sound
 Inherited
會將事件傳送到事件流程。
EventDispatcher
  
extract(target:ByteArray, length:Number, startPosition:Number = -1):Number
從 Sound 物件中擷取原始聲音資料。
Sound
 Inherited
會檢查 EventDispatcher 物件是否有對特定的事件類型註冊偵聽程式。
EventDispatcher
 Inherited
指出物件是否有已定義的指定屬性。
Object
 Inherited
指出 Object 類別的實體是否位於指定為參數的物件原型鏈中。
Object
  
load(stream:URLRequest, context:SoundLoaderContext = null):void
起始會從指定 URL 載入外部 MP3 檔的作業。
Sound
  
將 MP3 聲音資料從 ByteArray 物件載入 Sound 物件。
Sound
  
loadPCMFromByteArray(bytes:ByteArray, samples:uint, format:String = "float", stereo:Boolean = true, sampleRate:Number = 44100.0):void
將 PCM 32 位元浮點聲音資料從 ByteArray 物件載入 Sound 物件。
Sound
  
play(startTime:Number = 0, loops:int = 0, sndTransform:flash.media:SoundTransform = null):SoundChannel
會產生新的 SoundChannel 物件,以便播放聲音。
Sound
 Inherited
指出指定的屬性是否存在,以及是否可列舉。
Object
 Inherited
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
會從 EventDispatcher 物件移除偵聽程式。
EventDispatcher
 Inherited
為迴圈作業設定動態屬性的可用性。
Object
 Inherited
傳回代表此物件的字串,根據地區特定慣例進行格式化。
Object
 Inherited
會傳回指定之物件的字串形式。
Object
 Inherited
會傳回指定之物件的基本值。
Object
 Inherited
檢查此 EventDispatcher 物件是否已註冊事件偵聽程式,或者此物件的任何祖系已為特定事件類型註冊事件偵聽程式。
EventDispatcher
事件
 事件 摘要 定義自
 Inherited[廣播事件] 當 Flash Player 或 AIR 應用程式取得作業系統焦點並成為作用中時傳送。EventDispatcher
  在順利載入資料時傳送。Sound
 Inherited[廣播事件] 當 Flash Player 或 AIR 應用程式失去作業系統焦點並成為非作用中時傳送。EventDispatcher
  當 ID3 資料可用於 MP3 聲音時,由 Sound 物件傳送。Sound
  發生輸入/輸出錯誤,因而導致載入作業失敗時傳送。Sound
  在載入作業開始時傳送。Sound
  在載入作業進行中收到資料時傳送。Sound
  當執行階段要求新音效資料時傳送。Sound
屬性詳細資訊

bytesLoaded

屬性
bytesLoaded:uint  [唯讀]

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

會傳回此 Sound 物件中目前可用的位元組數目。 這個屬性通常僅適用於外部載入的檔案。



實作
    public function get bytesLoaded():uint

bytesTotal

屬性 
bytesTotal:int  [唯讀]

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

會傳回此 Sound 物件中的位元組總數。



實作
    public function get bytesTotal():int

id3

屬性 
id3:ID3Info  [唯讀]

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

提供存取屬於 MP3 檔一部份的中繼資料。

MP3 聲音檔案可包含 ID3 標籤,此標籤可提供關於檔案的中繼資料。 如果使用 Sound.load() 方法所載入的 MP3 聲音包含 ID3 標籤,您就可以查詢這些屬性, 只支援使用 UTF-8 字元集的 ID3 標籤。

Flash Player 9 和更新版本以及 AIR 都可支援 2.0 版 (特別是 2.3 和 2.4) 的 ID3 標籤。下表列出標準 2.0 版的 ID3 標籤,以及這些標籤所代表的內容類型。Sound.id3 屬性可透過 my_sound.id3.COMMmy_sound.id3.TIME 等格式存取這些標籤。第一份表格將說明可透過 2.0 版 ID3 屬性名稱或 ActionScript 屬性名稱存取的標籤。 第二份表格將說明受支援但在 ActionScript 中沒有預先定義屬性的 ID3 標籤。

ID3 2.0 標籤對應的 Sound 類別屬性
COMMSound.id3.comment
TALBSound.id3.album
TCONSound.id3.genre
TIT2Sound.id3.songName
TPE1Sound.id3.artist
TRCKSound.id3.track
TYERSound.id3.year

下表將說明受支援但在 Sound 類別中沒有預先定義屬性的 ID3 標籤。您可以透過呼叫 mySound.id3.TFLTmySound.id3.TIME 等格式,存取這些標籤。注意:Flash Lite 4 不會支援這些標籤。

屬性說明
TFLT檔案類型
時間時間
TIT1內容群組說明
TIT2標題/曲名/內容說明
TIT3字幕/說明技術指導
TKEY初始按鍵
TLAN語言
TLEN長度
TMED媒體類型
TOAL原始的專輯/影片/表演名稱
TOFN原始檔名
TOLY原始歌詞/文字作者
TOPE原始藝人/表演者
TORY原始發佈年份
TOWN檔案擁有者/授權者
TPE1領銜表演者/獨唱者
TPE2樂團/交響樂團/伴奏
TPE3指揮/表演者技術指導
TPE4解譯、合成或其他修改者
TPOS部分專輯
TPUB發行者
TRCK專輯的曲目編號/位置
TRDA錄音日期
TRSN網路電台名稱
TRSO網路電台擁有者
TSIZ大小
TSRCISRC (國際標準錄音碼)
TSSE編碼用的軟體/硬體和設定
TYER
WXXXURL 連結影格

使用此屬性時,必須顧及 Flash Player 安全性模型:

  • 與聲音檔位於相同安全執行程序中的 SWF 檔一定可以使用 Sound 物件的 id3 屬性。 若為其它安全執行程序中的檔案,就會進行安全性檢查。
  • 當您使用 Sound 類別的 load() 方法載入聲音時,可以指定 context 參數 (SoundLoaderContext 物件)。 如果您將 SoundLoaderContext 物件的 checkPolicyFile 屬性設定為 true,Flash Player 就會檢查載入聲音的來源伺服器是否有 URL 原則檔。如果原則檔存在而且允許從載入 SWF 檔的網域存取,檔案就可以存取 Sound 物件的 id3 屬性,否則就無法存取。

但在 Adobe AIR 中,application 安全執行程序中的內容 (使用 AIR 應用程式所安裝的內容) 沒有這些安全性限制。

如需安全性詳細資訊,請參閱「Flash Player 開發人員中心」主題:安全性



實作
    public function get id3():ID3Info

相關 API 元素


範例  ( 如何使用本範例 )
下列範例會從聲音檔案讀取 ID3 資訊,並將資訊顯示在文字欄位中。

建構函式會載入聲音檔案,但不將它設定為開始播放。 這裡假設該檔案位在 SWF 目錄中。 系統必須具有權限,才能讀取已載入聲音檔案的 ID3 標籤。 如果檔案中有 ID3 資訊,而且程式有讀取權限,就會觸發 Event.ID3 事件,而且會填入聲音檔案的 id3 屬性。 id3 屬性包含 ID3Info 物件,內含所有 ID3 資訊。

id3Handler() 方法中,檔案的 ID3 標籤儲存在 ID3Info 類別物件 id3 中。 範例會實體化一個文字欄位,以顯示 ID3 標籤的清單。 for 迴圈會重複執行所有 ID3 2.0 標籤,並將名稱和值附加到文字欄位的內容後面。 如果使用 ID3 資訊 (ID3Info) 屬性,也可以附加藝人、歌曲名稱和專輯。 ActionScript 3.0 與 Flash Player 9 和更新版本皆支援 ID3 2.0 標籤,尤其是 2.3 和 2.4。如果您以類似 for 迴圈的方式重複執行各個屬性,就只會顯示 ID3 2.0 標籤。 但是,舊版的資料也會儲存在歌曲的 id3 屬性中,而且可以使用 ID3 資訊類別屬性存取。 ID3 1.0 的標籤位於檔案末端,而 ID3 2.0 標籤則位於檔案開頭 (有時候,檔案裡可能同時有舊版本和新版本的標籤)。 如果檔案的編碼是在檔案開頭和末端同時有版本 1.0 和 2.0 的標籤,則 id3Handler() 方法會叫用兩次。 它會先讀取版本 2.0,再讀取版本 1.0。如果只有 ID3 1.0 的標籤,就可以透過 ID3 資訊屬性 (例如 id3.songname) 存取資訊。 對於 ID3 2.0,id3.TITS 屬性會使用新的標籤 (TITS) 擷取歌曲名稱。

請注意,這個範例沒有錯誤處理功能,如果 ID3 內容太長,結果可能會超出可檢視區域。

package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.media.ID3Info;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.events.Event;

    public class Sound_id3Example extends Sprite {
        private var snd:Sound = new Sound();       
        private var myTextField:TextField = new TextField();

        public function Sound_id3Example() {
            snd.addEventListener(Event.ID3, id3Handler);
            snd.load(new URLRequest("mySound.mp3"));
        }
    
        private function id3Handler(event:Event):void {
            var id3:ID3Info = snd.id3;

            myTextField.autoSize = TextFieldAutoSize.LEFT;
            myTextField.border = true;

            myTextField.appendText("Received ID3 Info: \n");
              
            for (var propName:String in id3) {
                myTextField.appendText(propName + " = " + id3[propName] + "\n");
            }
 
            myTextField.appendText("\n" + "Artist: " + id3.artist + "\n");
            myTextField.appendText("Song name: " + id3.songName + "\n");
            myTextField.appendText("Album: " + id3.album + "\n\n"); 
 
            this.addChild(myTextField);
        }
    }
}

isBuffering

屬性 
isBuffering:Boolean  [唯讀]

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

會傳回外部 MP3 檔的緩衝狀態。 如果此值為 true,物件會在等候更多資料期間,暫停任何播放動作。



實作
    public function get isBuffering():Boolean

isURLInaccessible

屬性 
isURLInaccessible:Boolean  [唯讀]

語言版本: ActionScript 3.0
執行階段版本: Flash Player 10.1, AIR 2

指出 Sound.url 屬性是否曾被截斷。當 isURLInaccessible 值為 true 時,Sound.url 值只能是載入聲音之最終 URL 的網域。例如,如果從 http://www.adobe.com/assets/hello.mp3 載入聲音,而 Sound.url 屬性的值為 http://www.adobe.com,屬性便會被截斷。只有下列項目都為 true 時,isURLInaccessible 值才會為 true

  • 載入聲音檔時,發生 HTTP 重新導向。
  • 呼叫 Sound.load() 的 SWF 檔案,是來自聲音檔最終 URL 以外的網域。
  • 呼叫 Sound.load() 的 SWF 檔案,沒有存取聲音檔的權限。授與存取聲音檔權限的方式和授與存取 Sound.id3 屬性權限的方式相同:建立原則檔案,並使用 SoundLoaderContext.checkPolicyFile 屬性。

注意:isURLInaccessible 屬性是 Flash Player 10.1 及 AIR 2.0 新增的屬性。不過當 Flash 執行階段支援它時,所有版本的 SWF 檔案都可以使用這個屬性。所以在「嚴謹模式」中使用某些編寫工具會造成編譯錯誤。若要解決錯誤,請使用間接語法 mySound["isURLInaccessible"] 或停用嚴謹模式。如果您使用 Flash Professional CS5 或 Flex SDK 4.1,您可以使用及編譯此 API,供 Flash Player 10.1 及 AIR 2 之前的執行階段版本使用。

對於 AIR 中的應用程式內容,這個屬性的值永遠都是 false



實作
    public function get isURLInaccessible():Boolean

相關 API 元素

length

屬性 
length:Number  [唯讀]

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

目前聲音的長度,以毫秒為單位。



實作
    public function get length():Number

url

屬性 
url:String  [唯讀]

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

從中載入此聲音的 URL。 這個屬性僅適用於使用 Sound.load() 方法載入的 Sound 物件。 若是與 SWF 檔元件庫中聲音資源相關聯的 Sound 物件,url 屬性值就是 null

當您首次呼叫 Sound.load() 時,url 屬性最初會具有 null 值,因為最終 URL 尚不明。 一旦 open 事件從 Sound 物件傳送時,url 屬性就會具有非 null 值。

url 屬性含有從中載入聲音的最終絕對 URL。 url 的值通常都會與傳遞給 Sound.load()stream 參數的值相同。 不過,如果您將相對 URL 傳遞給 Sound.load()url 屬性值就會表示為絕對 URL。此外,如果原始 URL 要求由 HTTP 伺服器重新導向,url 屬性值就會反映實際從中下載聲音檔的最終 URL。 這種絕對最終 URL 的回報方式就相當於 LoaderInfo.url 的行為。

在某些案例中,url 屬性的值會被截斷; 請參閱 isURLInaccessible 屬性,取得詳細資訊。



實作
    public function get url():String

相關 API 元素

建構函式詳細資料

Sound

()建構函式
public function Sound(stream:URLRequest = null, context:SoundLoaderContext = null)

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

會建立新的 Sound 物件。 如果您將有效的 URLRequest 物件傳遞給 Sound 建構函式,此建構函式就會自動為 Sound 物件呼叫 load() 函數。 如果您並未將有效的 URLRequest 物件傳遞給 Sound 建構函式,就必須自行為 Sound 物件呼叫 load() 函數,否則串流將不會載入該物件。

一旦針對 Sound 物件呼叫 load(),您就無法在稍後將不同的聲音檔載入該 Sound 物件。 若要載入不同的聲音檔,請建立新的 Sound 物件。

在 Flash Player 10 和更新版本以及 AIR 1.5 和更新版本中,您可以使用 sampleData 事件處理常式將聲音動態載入至 Sound 物件,而不需使用 load()

參數
stream:URLRequest (default = null) — 指向外部 MP3 檔案的 URL。
 
context:SoundLoaderContext (default = null) — 選擇性的 SoundLoaderContext 物件,可以定義緩衝時間 (MP3 資料在 Sound 物件緩衝區中保留的最小毫秒數),也可以指定應用程式是否應該在載入聲音之前先檢查跨網域原則檔案。
方法詳細資訊

close

()方法
public function close():void

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

會關閉串流,因而導致資料的下載作業終止。 在呼叫 close() 方法後,便無法從串流讀取任何資料。


擲回值
IOError — 串流無法關閉,或者該串流尚未開啟。

範例  ( 如何使用本範例 )

在下列範例中,當使用者按一下「停止」按鈕時,會呼叫 Sound.close() 方法,並停止聲音的串流處理。

建構函式會為「開始」和「停止」按鈕建立文字欄位。 當使用者按一下文字欄位時,會叫用 clickHandler() 方法。 它會處理聲音檔案的開始和停止播放。 請注意,依網路連線或使用者按「停止」按鈕的時間而定,檔案可能已經載入一大半,所以必須經過一段時間後,聲音檔案才會停止播放。 try...catch 區塊會用來捕捉關閉串流期間可能發生的任何 IO 錯誤。 例如,如果聲音是從本機目錄載入,且未經過串流處理,就會捕捉到錯誤 2029,訊息是「此 URLStream 物件沒有開啟的串流」。

 
package {
    import flash.display.Sprite;
    import flash.net.URLRequest;
    import flash.media.Sound;    
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.events.MouseEvent;
    import flash.errors.IOError;
    import flash.events.IOErrorEvent;

    public class Sound_closeExample extends Sprite {
        private var snd:Sound = new Sound();
        private var button:TextField = new TextField();
        private var req:URLRequest = new URLRequest("http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3");
        
        public function Sound_closeExample() {
            button.x = 10;
            button.y = 10;
            button.text = "START";
            button.border = true;
            button.background = true;
            button.selectable = false;
            button.autoSize = TextFieldAutoSize.LEFT;

            button.addEventListener(MouseEvent.CLICK, clickHandler);

            this.addChild(button);
        }

        private function clickHandler(e:MouseEvent):void {

            if(button.text == "START") {

                snd.load(req);
                snd.play();        

                snd.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

                button.text = "STOP";
            }
            else if(button.text == "STOP") {
                    
                try {
                    snd.close();
                    button.text = "Wait for loaded stream to finish.";
                }
                catch (error:IOError) {
                    button.text = "Couldn't close stream " + error.message;    
                }
            }
        }
        
        private function errorHandler(event:IOErrorEvent):void {
                button.text = "Couldn't load the file " + event.text;
        }
    }
}


extract

()方法 
public function extract(target:ByteArray, length:Number, startPosition:Number = -1):Number

語言版本: ActionScript 3.0
執行階段版本: Flash Player 10, AIR 1.5

從 Sound 物件中擷取原始聲音資料。

這個方法的設計是,透過您指定給不同 Sound 物件之 sampleData 事件的函數,來處理動態產生的音效。也就是,您可以使用此方法從 Sound 物件中擷取聲音資料。接下來,您可以將資料寫入另一個 Sound 物件用於串流處理動態音效的位元組陣列。

音效資料會放置在目標位元組陣列中,從位元組陣列的目前位置開始。音效資料一定會公開為 44100 Hz 立體聲。樣本類型為 32 位元浮點值,可使用 ByteArray.readFloat() 轉換為 Number。

參數

target:ByteArray — 放置已擷取聲音樣本的 ByteArray 物件。
 
length:Number — 要擷取的聲音樣本數目。樣本包含左聲道和右聲道,亦即兩個 32 位元浮點值。
 
startPosition:Number (default = -1) — 開始擷取的樣本。如果未指定值,第一次呼叫 Sound.extract() 會從聲音開頭開始,未使用 startPosition 值的後續呼叫會依序處理此檔案。

傳回值
Number — 寫入 target 參數指定之 ByteArray 的範例數目。

相關 API 元素


範例  ( 如何使用本範例 )

下列範例會載入 mp3 檔,並使用 Sound 類別的 extract() 方法存取音效資料。

mp3 資料會載入 sourceSnd Sound 物件。當應用程式載入 mp3 資料時,它會呼叫 loaded() 函數 (sourceSnd 物件其 complete 事件的事件處理常式)。第二個物件 outputSound 會用來播放修改的音效。outputSound 物件有一個 sampleData 事件偵聽程式,所以只要您呼叫此物件的 play() 方法,物件就會定期傳送 sampleData 事件。upOctave() 方法會根據來源音效資料傳回已修改音效資料的位元組陣列。它會在來源資料中每隔一個音效樣本間隔取樣,而傳回高一個八度的音效。sampleData 事件的事件偵聽程式會將傳回的位元組陣列寫入 outputSound 物件的 data 屬性。data 位元組陣列會附加到 outputSound 物件的輸出音效資料。

若要測試這個範例,請將 test.mp3 檔加入 SWF 檔所在的目錄。

var sourceSnd:Sound = new Sound();
var outputSnd:Sound = new Sound();
var urlReq:URLRequest = new URLRequest("test.mp3");

sourceSnd.load(urlReq);
sourceSnd.addEventListener(Event.COMPLETE, loaded);

function loaded(event:Event):void
{
    outputSnd.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
    outputSnd.play();
}

function processSound(event:SampleDataEvent):void
{
    var bytes:ByteArray = new ByteArray();
    sourceSnd.extract(bytes, 4096);
    event.data.writeBytes(upOctave(bytes));
}

function upOctave(bytes:ByteArray):ByteArray
{
    var returnBytes:ByteArray = new ByteArray();
    bytes.position = 0;
    while(bytes.bytesAvailable > 0)
    {
        returnBytes.writeFloat(bytes.readFloat());
        returnBytes.writeFloat(bytes.readFloat());
        if (bytes.bytesAvailable > 0)
        {
            bytes.position += 8;
        }
    }
    return returnBytes;
}

load

()方法 
public function load(stream:URLRequest, context:SoundLoaderContext = null):void

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

起始會從指定 URL 載入外部 MP3 檔的作業。 如果您將有效的 URLRequest 物件提供給 Sound 建構函式,此建構函式就會為您呼叫 Sound.load()。 如果您並未將有效的 URLRequest 物件傳遞給 Sound 建構函式,或者傳遞的是 null 值,就必須自行呼叫 Sound.load()

一旦針對 Sound 物件呼叫 load(),您就無法在稍後將不同的聲音檔載入該 Sound 物件。 若要載入不同的聲音檔,請建立新的 Sound 物件。

使用此方法時,必須考慮下列安全性模型:

  • 如果呼叫的 檔位於「具有檔案系統的本機」之安全執行程序中,且聲音位於網路安全執行程序中,則不允許呼叫 Sound.load()
  • 若要從本機信任或「具有網路連線的本機」安全執行程序進行存取,您需要透過 URL 原則檔向網站要求權限。
  • 您不可以連接到常用的保留連接埠。完整的封鎖連接埠清單,請參閱在「ActionScript 3.0 開發人員指南」中的「限制網路 API」
  • 您可以避免 SWF 檔使用這個方法,只要設定包含 SWF 內容之 HTML 頁面中的 objectembed 標籤的 allowNetworking 參數即可。

在 Flash Player 10 和更新版本中,如果您使用包含上載 (由 POST 主體中 "content-disposition" 檔頭的 "filename" 參數指示) 的多部分 Content-Type (例如 "multipart/form-data"),POST 作業會受限於上載所套用的安全性規則:

  • 必須執行 POST 作業,以回應使用者啟動的動作 (例如按下滑鼠或按下按鍵)。
  • 如果 POST 作業會跨越網域 (POST 目標與傳送 POST 要求的 SWF 檔不在同一部伺服器上),目標伺服器必須提供允許跨網域存取的 URL 原則。

此外,多部分的 Content-Type 語法必須有效 (符合 RFC2046 標準)。如果語法無效,POST 作業會受限於上載所套用的安全性規則。

在 Adobe AIR 中,application 安全執行程序中的內容 (使用 AIR 應用程式所安裝的內容) 沒有這些安全性限制。

如需安全性詳細資訊,請參閱「Flash Player 開發人員中心」主題:安全性

參數

stream:URLRequest — 指向外部 MP3 檔的 URL。
 
context:SoundLoaderContext (default = null) — 選擇性的 SoundLoaderContext 物件,可以定義緩衝時間 (MP3 資料在 Sound 物件緩衝區中保留的最小毫秒數),也可以指定應用程式是否應該在載入聲音之前先檢查跨網域原則檔案。


擲回值
IOError — 導致載入失敗的網路錯誤。
 
SecurityError — 本機不受信任的 檔可能無法與網際網路進行通訊。 您可以透過將此檔案重新分類為「具有網路連線的本機」或受信任,解決此問題。
 
SecurityError — 您不可以連接到常用的保留連接埠。完整的封鎖連接埠清單,請參閱在「ActionScript 3.0 開發人員指南」中的「限制網路 API」
 
IOError stream 物件的 digest 屬性不是 null。只有在載入 SWZ 檔 (Adobe 平台組件) 過程中呼叫 URLLoader.load() 方法時,才需設定 URLRequest 物件的 digest 屬性。

範例  ( 如何使用本範例 )

下列範例會顯示聲音檔案的載入進度。

建構函式中會建立 URLRequest 物件,以識別聲音檔案 (來自 Adobe 的 Podcast) 的位置。 檔案於 try...catch 區塊中載入,以捕捉載入檔案期間可能發生的任何錯誤。 如果發生 IO 錯誤,也會叫用 errorHandler() 方法,並將錯誤訊息寫入文字欄位,做為進度報告之用。 載入作業進行時,會傳送 ProgressEvent.PROGRESS 事件並呼叫 progressHandler() 方法。 這裡使用 ProgressEvent.PROGRESS 事件做為計算載入進度的計時器。

progressHandler() 方法會將使用 ProgressEvent 物件傳遞的 bytesLoaded 值除以 bytesTotal 值,得出聲音檔案的載入百分比。 接著,它會在文字欄位中顯示這些值 (請注意,如果檔案太小、已快取或位於本機目錄中,則進度可能不太明顯)。

package {
    import flash.display.Sprite;
    import flash.net.URLRequest;
    import flash.media.Sound;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.events.ProgressEvent;
    import flash.events.IOErrorEvent;
    
    public class Sound_loadExample extends Sprite {
        private var snd:Sound = new Sound();
        private var statusTextField:TextField  = new TextField();

        public function Sound_loadExample(){

            statusTextField.autoSize = TextFieldAutoSize.LEFT;
            var req:URLRequest = new URLRequest("http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3");
            
            try {
            snd.load(req);
            
            snd.play();
            }
            catch (err:Error) {
                trace(err.message);
            }
 
            snd.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            snd.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                     
            this.addChild(statusTextField);
        }
    
        private function progressHandler(event:ProgressEvent):void {
            var loadTime:Number = event.bytesLoaded / event.bytesTotal;
            var LoadPercent:uint = Math.round(100 * loadTime);
              
            statusTextField.text = "Sound file's size in bytes: " + event.bytesTotal + "\n" 
                                 + "Bytes being loaded: " + event.bytesLoaded + "\n" 
                                 + "Percentage of sound file that is loaded " + LoadPercent + "%.\n";
        }
 
        private function errorHandler(errorEvent:IOErrorEvent):void {
            statusTextField.text = "The sound could not be loaded: " + errorEvent.text;
        }
    }
}

loadCompressedDataFromByteArray

()方法 
public function loadCompressedDataFromByteArray(bytes:ByteArray, bytesLength:uint):void

語言版本: ActionScript 3.0
執行階段版本: AIR 3, Flash Player 11

將 MP3 聲音資料從 ByteArray 物件載入 Sound 物件。將從目前 ByteArray 位置讀取資料,而且結束時,資料會在指定的位元組長度結尾處離開 ByteArray 位置。如果 MP3 聲音資料包含 ID3 資料,將會在這個函數呼叫期間傳送 ID3 事件。如果 ByteArray 物件沒有足夠的資料,這個函數將會擲回例外狀況。

參數

bytes:ByteArray
 
bytesLength:uint

loadPCMFromByteArray

()方法 
public function loadPCMFromByteArray(bytes:ByteArray, samples:uint, format:String = "float", stereo:Boolean = true, sampleRate:Number = 44100.0):void

語言版本: ActionScript 3.0
執行階段版本: AIR 3, Flash Player 11

將 PCM 32 位元浮點聲音資料從 ByteArray 物件載入 Sound 物件。將從目前 ByteArray 位置讀取資料,而且結束時,資料會在指定的範例長度乘以 1 或 2 個聲道 (如果設定立體聲旗標) 的結尾處離開 ByteArray 位置。

從 Flash Player 11.8 開始,可傳遞至這個函數的音效資料數量受到限制。對於版本 21 以上的 SWF,如果傳遞至這個函數的音效資料數量超過 1800 秒,這個函數就會擲回例外。也就是說,樣本/sampleRate 應該小於或等於 1800。對於版本 21 之前的 SWF,如果傳入的音效資料數量超過 12000 秒,執行階段就會無訊息失敗。這僅供舊版相容性之用。

如果 ByteArray 物件沒有足夠的資料,這個函數就會擲回例外狀況。

參數

bytes:ByteArray
 
samples:uint
 
format:String (default = "float")
 
stereo:Boolean (default = true)
 
sampleRate:Number (default = 44100.0)

play

()方法 
public function play(startTime:Number = 0, loops:int = 0, sndTransform:flash.media:SoundTransform = null):SoundChannel

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

會產生新的 SoundChannel 物件,以便播放聲音。 這個方法會傳回 SoundChannel 物件,您可以存取此物件,以停止聲音並監視音量。(若要控制音量、左右相位和平衡,請存取指定給聲道的 SoundTransform 物件)。

參數

startTime:Number (default = 0) — 應該開始進行播放作業的初始位置,以毫秒為單位。
 
loops:int (default = 0) — 會定義聲音在聲道停止播放之前,重複回到 startTime 值的次數。
 
sndTransform:flash.media:SoundTransform (default = null) — 指定給聲道的初始 SoundTransform 物件。

傳回值
SoundChannel — 可讓您用來控制聲音的 SoundChannel 物件。 如果您沒有音效卡,或是沒有可用的聲道,這個方法就會傳回 null。 一次可用的最大聲道數目為 32。

相關 API 元素


範例  ( 如何使用本範例 )

在下列範例中,使用者可以在檔案載入後,使用圖形列選取聲音檔案的開始位置 (開始時間)。

建構函式會呼叫 Sound.load() 方法,開始載入聲音資料。 接著,它會呼叫 Sound.play() 方法,這個方法一等到有足夠的資料載入後就會開始播放聲音。 Sound.play() 方法會傳回 SoundChannel 物件,可用來控制聲音的播放。 文字欄位會顯示指示。 為確保已經載入使用者希望開始播放的聲音內容,所以會建立 bar Sprite 物件,並在檔案完成載入後顯示出來。 檔案成功載入時會傳送 Event.COMPLETE 事件,以觸發 completeHandler() 方法。 接著,completeHandler() 方法會建立橫列,並將它加入播放清單 (不用 Shape 物件,改用 Sprite 物件以支援互動性)。 當使用者按一下橫列時,會觸發 clickHandler() 方法。

clickHandler() 方法中,使用者按一下時的 x 座標位置 event.localX 會被用來決定檔案中使用者要開始播放的位置。 由於橫列的大小是 100 個像素,而且它從 x 座標 100 像素開始,因此很容易決定位置的百分比。 而且,因為檔案已經載入,所以聲音檔案的 length 屬性會有完整檔案的長度 (以毫秒為單位)。 使用聲音檔案的長度和在該行中的位置,可以決定聲音檔案的開始位置。 停止播放聲音後,聲音檔案會從選取的開始位置 (這已當成 startTime 參數傳遞到 play() 方法) 重新開始。

 
package {
    import flash.display.Sprite;
    import flash.display.Graphics;
    import flash.events.MouseEvent;
    import flash.media.Sound;;
    import flash.net.URLRequest;
    import flash.media.SoundChannel;
    import flash.events.ProgressEvent;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.events.IOErrorEvent;
    
    public class Sound_playExample1 extends Sprite {
        private var snd:Sound = new Sound();
        private var channel:SoundChannel = new SoundChannel();
        private var infoTextField:TextField = new TextField();
    
        public function Sound_playExample1() {

            var req:URLRequest = new URLRequest("MySound.mp3");
            
            infoTextField.autoSize = TextFieldAutoSize.LEFT;
            infoTextField.text = "Please wait for the file to be loaded.\n" 
                                + "Then select from the bar to decide where the file should start.";
            
            snd.load(req);               
            channel = snd.play();

            snd.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            snd.addEventListener(Event.COMPLETE, completeHandler);


            this.addChild(infoTextField);
        }

        private function completeHandler(event:Event):void {                
            infoTextField.text = "File is ready.";

            var bar:Sprite = new Sprite();

            bar.graphics.lineStyle(5, 0xFF0000);
            bar.graphics.moveTo(100, 100);
            bar.graphics.lineTo(200, 100);

            bar.addEventListener(MouseEvent.CLICK, clickHandler);

            this.addChild(bar);
        }
    
        private function clickHandler(event:MouseEvent):void {
            
                var position:uint = event.localX;
                var percent:uint = Math.round(position) - 100;   
                var cue:uint = (percent / 100) * snd.length;

                channel.stop();
                channel = snd.play(cue);    
        }

        private function errorHandler(errorEvent:IOErrorEvent):void {
            infoTextField.text = "The sound could not be loaded: " + errorEvent.text;
        }
    }
}

下列範例會根據使用者按一下或按兩下按鈕,決定將聲音播放一次或兩次。

建構函式會載入聲音,並建立簡單的矩形 button Sprite 物件 (不用 Shape 物件,改用 Sprite 物件以支援互動性)。 這裡假設該聲音檔案與 SWF 檔在相同目錄中 (這個範例沒有錯誤處理程式碼)。

範例中設定了兩個事件偵聽程式,以回應按一下滑鼠和按兩下滑鼠。 如果使用者按一下,就會叫用播放聲音的 clickHandler() 方法。 如果使用者按兩下按鈕,則會叫用播放兩次聲音檔案的 doubleClickHandler() 方法。 play() 方法的第二個引數會設定為 1,表示聲音會繞回聲音的開始時間,重新播放一次。 第一個引數 (開始時間) 設定為 0,表示檔案將從頭開始播放。

package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.media.Sound;
    import flash.net.URLRequest;

    public class Sound_playExample2 extends Sprite {
        private var button:Sprite = new Sprite(); 
        private var snd:Sound = new Sound();
            
        public function Sound_playExample2() {

            var req:URLRequest = new URLRequest("click.mp3");
            snd.load(req);               
     
            button.graphics.beginFill(0x00FF00);
            button.graphics.drawRect(10, 10, 50, 30);
            button.graphics.endFill();
     
            button.addEventListener(MouseEvent.CLICK, clickHandler);
            button.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);
     
            this.addChild(button);
        }

        private function clickHandler(event:MouseEvent):void {
            snd.play();            
        }

        private function doubleClickHandler(event:MouseEvent):void {
            snd.play(0, 2);        
        }
    }
}

下列範例會顯示聲音檔案的載入和播放進度。

在建構函式中,檔案於 try...catch 區塊中載入,以捕捉載入檔案期間可能發生的任何錯誤。 範例中為聲音物件加入偵聽程式,這個偵聽程式會呼叫 errorHandler() 方法來回應 IOErrorEvent 事件。 範例中為主應用程式加入了另一個偵聽程式,這個偵聽程式會回應 Event.ENTER_FRAME 事件,做為顯示播放進度的計時機制。 最後,會為聲道加入第三個偵聽程式,藉由呼叫 soundCompleteHandler() 方法來回應 Event.SOUND_COMPLETE 事件 (當聲音播放完畢時)。 soundCompleteHandler() 方法也會移除 Event.ENTER_FRAME 事件的事件偵聽程式。

enterFrameHandler() 方法會將使用 ProgressEvent 物件傳遞的 bytesLoaded 值除以 bytesTotal 值,得出聲音檔案的載入百分比。 聲音資料的播放百分比可以用聲道的 position 屬性值除以聲音資料的長度來決定。 但是,如果聲音資料還未完全載入,則聲音物件的 length 屬性只會顯示目前已載入的聲音資料大小。 範例中會計算完整聲音檔案最後的估計大小,方法是將目前聲音物件的 length 除以 bytesLoaded 屬性值,再除以 bytesTotal 屬性值。

請注意,如果檔案太小、已快取或位於本機目錄中,則載入進度可能不太明顯。 此外,從聲音資料開始載入到載入的資料開始播放,這中間的延遲時間是由 SoundLoaderContext.buffertime 屬性的值所決定,這個值預設為 1000 毫秒,而且可以重設。

package {
    import flash.display.Sprite;
    import flash.net.URLRequest;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    
    public class Sound_playExample3 extends Sprite {
        private var snd:Sound = new Sound();
        private var channel:SoundChannel;
        private var statusTextField:TextField  = new TextField();

        public function Sound_playExample3(){

            statusTextField.autoSize = TextFieldAutoSize.LEFT;

           var req:URLRequest = new URLRequest("http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3");
            
            try {
                snd.load(req);
            
                channel = snd.play();
            }
            catch (err:Error) {
                trace(err.message);
            }
                    
            snd.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
                    
            this.addChild(statusTextField);
        }
    
        private function enterFrameHandler(event:Event):void {    
            var loadTime:Number = snd.bytesLoaded / snd.bytesTotal;
            var loadPercent:uint = Math.round(100 * loadTime);
            var estimatedLength:int = Math.ceil(snd.length / (loadTime));
            var playbackPercent:uint = Math.round(100 * (channel.position / estimatedLength));
      
            statusTextField.text = "Sound file's size is " + snd.bytesTotal + " bytes.\n" 
                                   + "Bytes being loaded: " + snd.bytesLoaded + "\n" 
                                   + "Percentage of sound file that is loaded " + loadPercent + "%.\n"
                                   + "Sound playback is " + playbackPercent + "% complete.";     
        }
 
        private function errorHandler(errorEvent:IOErrorEvent):void {
            statusTextField.text = "The sound could not be loaded: " + errorEvent.text;
        }

        private function soundCompleteHandler(event:Event):void {
            statusTextField.text = "The sound has finished playing.";
            removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }
    }
}
事件詳細資訊

complete

事件
事件物件類型: flash.events.Event
屬性 Event.type = flash.events.Event.COMPLETE

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

在順利載入資料時傳送。

Event.COMPLETE 常數會定義 complete 事件物件的 type 屬性值。

這個事件具有下列屬性:

屬性
bubblesfalse
cancelablefalse;沒有要取消的預設行為指令。
currentTarget正主動使用事件偵聽程式處理 Event 物件的物件。
target已完成載入的網路物件。

相關 API 元素

id3

事件  
事件物件類型: flash.events.Event
屬性 Event.type = flash.events.Event.ID3

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

當 ID3 資料可用於 MP3 聲音時,由 Sound 物件傳送。

Event.ID3 常數會定義 id3 事件物件的 type 屬性值。

這個事件具有下列屬性:

屬性
bubblesfalse
cancelablefalse;沒有要取消的預設行為指令。
currentTarget正主動使用事件偵聽程式處理 Event 物件的物件。
target載入目前可使用 ID3 資料之 MP3 的 Sound 物件。 target 不會永遠都是顯示清單中註冊事件偵聽程式的物件。 請使用 currentTarget 屬性,存取目前正在處理該事件之顯示清單中的物件。

相關 API 元素

ioError

事件  
事件物件類型: flash.events.IOErrorEvent
屬性 IOErrorEvent.type = flash.events.IOErrorEvent.IO_ERROR

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

發生輸入/輸出錯誤,因而導致載入作業失敗時傳送。

定義 ioError 事件物件的 type 屬性值。

這個事件具有下列屬性:

屬性
bubblesfalse
cancelablefalse;沒有要取消的預設行為指令。
currentTarget正主動使用事件偵聽程式處理 Event 物件的物件。
errorID與特定錯誤相關聯的參考號碼 (僅限 AIR)。
target遭遇輸入/輸出錯誤的網路物件。
text要顯示為錯誤訊息的文字。

相關 API 元素

open

事件  
事件物件類型: flash.events.Event
屬性 Event.type = flash.events.Event.OPEN

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

在載入作業開始時傳送。

Event.OPEN 常數會定義 open 事件物件的 type 屬性值。

這個事件具有下列屬性:

屬性
bubblesfalse
cancelablefalse;沒有要取消的預設行為指令。
currentTarget正主動使用事件偵聽程式處理 Event 物件的物件。
target已開啟連線的網路物件。

相關 API 元素

progress

事件  
事件物件類型: flash.events.ProgressEvent
屬性 ProgressEvent.type = flash.events.ProgressEvent.PROGRESS

語言版本: ActionScript 3.0
執行階段版本: AIR 1.0, Flash Player 9, Flash Lite 4

在載入作業進行中收到資料時傳送。

定義 progress 事件物件的 type 屬性值。

這個事件具有下列屬性:

屬性
bubblesfalse
bytesLoaded在偵聽程式處理事件時,載入的項目或位元組數目。
bytesTotal如果載入程序成功時,最終將載入的項目或位元組總數。
cancelablefalse;沒有要取消的預設行為指令。
currentTarget正主動使用事件偵聽程式處理 Event 物件的物件。
target回報進度的物件。

相關 API 元素

sampleData

事件  
事件物件類型: flash.events.SampleDataEvent
屬性 SampleDataEvent.type = flash.events.SampleDataEvent.SAMPLE_DATA

語言版本: ActionScript 3.0
執行階段版本: Flash Player 10, AIR 1.5

當執行階段要求新音效資料時傳送。

定義 SampleDataEvent 事件物件的 type 屬性值。

這個事件具有下列屬性:

屬性
bubblesfalse
cancelablefalse;沒有要取消的預設行為指令。
position提供音效資料的來源點。

範例  ( 如何使用本範例 )

下列範例會播放簡單的正弦波。
var mySound:Sound = new Sound();
function sineWaveGenerator(event:SampleDataEvent):void {
    for ( var c:int=0; c<8192; c++ ) {
        event.data.writeFloat(Math.sin((Number(c+event.position)/Math.PI/2))*0.25);
        event.data.writeFloat(Math.sin((Number(c+event.position)/Math.PI/2))*0.25);
    }
}

mySound.addEventListener(SampleDataEvent.SAMPLE_DATA,sineWaveGenerator);
mySound.play();

相關 API 元素

SoundExample.as

下列範例會顯示在開啟並播放 MP3 檔時,所發生之聲音事件的相關資訊。 若要執行此範例,請將名為 MySound.mp3 的檔案與 SWF 檔放置於相同的目錄中。
package {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;

    public class SoundExample extends Sprite {
        private var url:String = "MySound.mp3";
        private var song:SoundChannel;

        public function SoundExample() {
            var request:URLRequest = new URLRequest(url);
            var soundFactory:Sound = new Sound();
            soundFactory.addEventListener(Event.COMPLETE, completeHandler);
            soundFactory.addEventListener(Event.ID3, id3Handler);
            soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            soundFactory.load(request);
            song = soundFactory.play();
        }

        private function completeHandler(event:Event):void {
            trace("completeHandler: " + event);
        }

        private function id3Handler(event:Event):void {
            trace("id3Handler: " + event);
        }

        private function ioErrorHandler(event:Event):void {
            trace("ioErrorHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            trace("progressHandler: " + event);
        }
    }
}




[ X ]為什麼顯示英文?
「ActionScript 3.0 參考」的內容是以英文顯示

並非所有「ActionScript 3.0 參考」的內容都翻譯為所有語言。當語言元素未翻譯時,就會以英文顯示。例如,ga.controls.HelpBox 類別並沒有翻譯為任何語言。因此在參考的繁體中文版本中,ga.controls.HelpBox 類別就會以英文顯示。