用于 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 的标签
Motion 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 中,将无法采用声音对象并在层次结构中对其进行排列以控制其属性。

在使用此类时,请考虑以下安全模型:

  • 如果执行调用的 文件在网络沙箱中,并且要加载的声音文件是本地文件,则不允许加载和播放声音。
  • 默认情况下,若执行调用的文件是本地文件并且试图加载和播放远程声音,则不允许加载和播放声音。用户必须授予明确许可才能允许此类访问。
  • 某些处理声音的操作受到限制。除非您实现了一个跨域策略文件,否则处于其它域的 文件无法访问已加载声音中的数据。受此限制约束的与声音相关的 API 为 Sound.id3SoundMixer.computeSpectrum()SoundMixer.bufferTimeSoundTransform 类。

但在 Adobe AIR 中,application 安全沙箱中的内容(使用 AIR 应用程序安装的内容)不受这些安全限制的约束。

有关安全性的详细信息,请参阅 Flash Player 开发人员中心主题:安全性

查看示例

相关 API 元素



公共属性
 属性由以下参数定义
  bytesLoaded : uint
[只读] 返回此声音对象中当前可用的字节数。
Sound
  bytesTotal : int
[只读] 返回此声音对象中总的字节数。
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
  
将 ByteArray 对象中的 MP3 声音数据加载到 Sound 对象中。
Sound
  
loadPCMFromByteArray(bytes:ByteArray, samples:uint, format:String = "float", stereo:Boolean = true, sampleRate:Number = 44100.0):void
将 ByteArray 对象中的 PCM 32 位浮点声音数据加载到 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
  在存在可用于 MP3 声音的 ID3 数据时由 Sound 对象调度。Sound
  在出现输入/输出错误并由此导致加载操作失败时调度。Sound
  在加载操作开始时调度。Sound
  在加载操作进行过程中接收到数据时调度。Sound
  在运行时请求新音频数据时调度。Sound
属性详细信息

bytesLoaded

属性
bytesLoaded:uint  [只读]

语言版本: ActionScript 3.0
运行时版本: AIR 1.0, Flash Player 9, Flash Lite 4

返回此声音对象中当前可用的字节数。此属性通常只对从外部加载的文件有用。



实现
    public function get bytesLoaded():uint

bytesTotal

属性 
bytesTotal:int  [只读]

语言版本: ActionScript 3.0
运行时版本: AIR 1.0, Flash Player 9, Flash Lite 4

返回此声音对象中总的字节数。



实现
    public function get bytesTotal():int

id3

属性 
id3:ID3Info  [只读]

语言版本: ActionScript 3.0
运行时版本: AIR 1.0, Flash Player 9, Flash Lite 4

提供对作为 MP3 文件一部分的元数据的访问。

MP3 声音文件可以包含 ID3 标签,ID3 标签提供有关该文件的元数据。如果使用 Sound.load() 方法加载的 MP3 声音包含 ID3 标签,则可以查询这些属性。只支持使用 UTF-8 字符集的 ID3 标签。

Flash Player 9 和更高版本以及 AIR 支持 ID3 2.0 标签,特别是 2.3 和 2.4 标签。下表列出了标准 ID3 2.0 标签以及该标签所表示的内容类型。Sound.id3 属性通过 my_sound.id3.COMMmy_sound.id3.TIME 等格式提供对这些标记的访问权限。第一个表描述了可通过 ID3 2.0 属性名称或 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文件类型
TIME时间
TIT1内容组说明
TIT2标题/歌曲名称/内容说明
TIT3副标题/说明精选
TKEY初始密钥
TLAN语言
TLEN长度
TMED媒体类型
TOAL原始唱片/影片/演出标题
TOFN原始文件名
TOLY原词作者/乐谱作者
TOPE原始艺术家/表演者
TORY原始发行年份
TOWN文件所有者/获得授权者
TPE1主要表演者/独奏(独唱)
TPE2乐队/管弦乐队/伴奏
TPE3指挥/主要演奏者
TPE4翻译、混录员或以其它方式进行修改的人员
TPOS歌曲集部分
TPUB发行者
TRCK歌曲集中的曲目编号/位置
TRDA录制日期
TRSNInternet 无线电台名称
TRSOInternet 无线电台所有者
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,则可以对在 Flash Player 10.1 和 AIR 2 之前发布的运行时使用和编译此 API。

对于 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 未知。只要从 Sound 对象中调度了 open 事件,url 属性就会具有非空值。

url 属性包含已从其中加载了声音的最终、绝对 URL。url 的值通常与传递给 Sound.load()stream 参数的值相同。但是,如果已将一个相对 URL 传递给 Sound.load(),则 url 属性的值将表示绝对 URL。此外,如果 HTTP 服务器重定向原始 URL 请求,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) — 一个可选的 SoundLoader 上下文对象,可以定义缓冲时间(MP3 数据保留到 Sound 对象的缓冲区中所需的最小毫秒数)并且可以指定加载声音前应用程序是否应该检查跨域策略文件。
方法详细信息

close

()方法
public function close():void

语言版本: ActionScript 3.0
运行时版本: AIR 1.0, Flash Player 9, Flash Lite 4

关闭该流,从而停止所有数据的下载。调用 close() 方法之后,将无法从流中读取数据。


引发
IOError — 无法关闭该流,或者该流未打开。

示例  ( 如何使用本示例 )

在下面的示例中,当用户单击 Stop 按钮时,将调用 Sound.close() 方法并停止声音流。

在构造函数中,为 Start 和 Stop 按钮创建了一个文本字段。当用户单击文本字段时,将调用 clickHandler() 方法。它处理声音文件的启动和停止操作。请注意,根据网络连接情况或用户单击 Stop 按钮的时间,可能已加载了大部分文件,并且可能需要经过一段时间后才能停止播放声音文件。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 对象提取原始声音数据。

当您通过将分配给 sampleData 事件的函数用于不同的 Sound 对象,从而使用动态生成的音频时,可以使用此方法。即,您可以使用此方法从 Sound 对象中提取声音数据。然后,可以将此数据写入另一 Sound 对象用于对动态音频进行流式处理的字节数组。

将音频数据放在目标字节数组中(从该字节数组的当前位置开始)。音频数据始终公开为 44100 Hz 立体声。样本类型是一个 32 位浮点值,可以使用 ByteArray.readFloat() 将该值转换为一个数字。

参数

target:ByteArray — 用于放置提取的声音样本的 ByteArray 对象。
 
length:Number — 要提取的声音样本数。样本包含左声道和右声道,即 2 个 32 位浮点值。
 
startPosition:Number (default = -1) — 开始提取处的样本。如果未指定值,则对 Sound.extract() 的第一个调用将在声音的开头启动;未指定 startPosition 值的后续调用将按顺序通过该文件。

返回
Number — 在 target 参数中指定的 ByteArray 中写入的范例数量。

相关 API 元素


示例  ( 如何使用本示例 )

下面的示例加载一个 mp3 文件并使用 Sound 类的 extract() 方法来访问音频数据。

此 mp3 数据将加载到 sourceSnd Sound 对象中。当应用程序加载 mp3 数据时,它将调用 loaded() 函数(sourceSnd 对象的 complete 事件的事件处理函数)。第二个 Sound 对象 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 文件的过程。如果为 Sound 构造函数提供有效的 URLRequest 对象,该构造函数将为您调用 Sound.load()。只有在未将有效的 URLRequest 对象传递到 Sound 构造函数或传递了 null 值的情况下,才需要自己调用 Sound.load()

一旦对某个 Sound 对象调用了 load(),就不能再将另一个声音文件加载到该 Sound 对象中。要加载另一个声音文件,请创建新的 Sound 对象。

在使用此方法时,请考虑以下安全模型:

  • 如果执行调用的 文件在只能与本地文件系统的内容交互的沙箱中,并且声音在网络沙箱中,则不允许调用 Sound.load()
  • 从受信任的本地沙箱或只能与远程内容交互的沙箱进行访问需要通过 URL 策略文件获得网站的许可。
  • 无法连接到常用的保留端口。有关已阻止的端口的完整列表,请参阅《ActionScript 3.0 开发人员指南》中的“限制网络 API”。
  • 可以在包含 SWF 内容的 HTML 页中设置 objectembed 标签的 allowNetworking 参数,从而防止 SWF 文件使用此方法。

在 Flash Player 10 和更高版本中,如果使用包含上载(由 POST 体内的“content-disposition”标头中的“filename”参数表示)的 multipart Content-Type(例如“multipart/form-data”),则 POST 操作会受应用于上载的安全规则的约束:

  • 必须执行 POST 操作以响应用户启动的操作(如鼠标单击或按键)。
  • 如果 POST 操作是跨域的(POST 目标与发送 POST 请求的 SWF 文件不在同一台服务器上),则目标服务器必须提供一个允许跨域访问的 URL 策略文件。

另外,对于任何 multipart Content-Type,语法必须有效(根据 RFC2046 标准)。如果语法无效,则 POST 操作受应用于上载的安全规则约束。

在 Adobe AIR 中,application 安全沙箱中的内容(使用 AIR 应用程序安装的内容)不受这些安全限制的约束。

有关安全性的详细信息,请参阅 Flash Player 开发人员中心主题:安全性

参数

stream:URLRequest — 指向外部 MP3 文件的 URL。
 
context:SoundLoaderContext (default = null) — 一个可选的 SoundLoader 上下文对象,可以定义缓冲时间(MP3 数据保留到 Sound 对象的缓冲区中所需的最小毫秒数)并且可以指定加载声音前应用程序是否应该检查跨域策略文件。


引发
IOError — 网络错误导致加载失败。
 
SecurityError — 本地不受信任的 文件可能无法与 Internet 进行通信。可通过将此文件重新分类为只能与远程内容交互或受信任来解决此问题。
 
SecurityError — 无法连接到常用的保留端口。有关已阻止的端口的完整列表,请参阅《ActionScript 3.0 开发人员指南》中的“限制网络 API”。
 
IOError stream 对象的 digest 属性不是 null。如果在加载 SWZ 文件(一个 Adobe 平台组件)时调用 URLLoader.load() 方法,仅应设置 URLRequest 对象的 digest 属性。

示例  ( 如何使用本示例 )

下面的示例显示了声音文件的加载进度。

在构造函数中,创建了一个 URLRequest 对象以确定声音文件(来自 Adobe 的播客)的位置。将在 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

将 ByteArray 对象中的 MP3 声音数据加载到 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

将 ByteArray 对象中的 PCM 32 位浮点声音数据加载到 Sound 对象中。将从当前 ByteArray 位置读取数据,并在完成后将 ByteArray 位置保留在乘以 1 个信道或 2 个信道的指定采样长度的结尾处(如果一次完成立体声标志设置)。

从 Flash Player 11.8 开始,可传递给此函数的音频数据量是有限的。对于 SWF 版本 >=21,如果传递给此函数的音频数据量超过 1800 秒,此函数将引发异常。也即采样/采样率应小于等于 1800。对于 SWF 21(不含)以下的版本,如果传递的音频数据量超过 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() 方法随后创建图形栏,并将其添加到显示列表中。(使用 Sprite 对象来支持交互功能,而不是形状对象。)当用户单击图形栏时,将触发 clickHandler() 方法。

clickHandler() 方法中,使用用户单击的 x 坐标位置 event.localX 来确定用户希望从中开始播放文件的位置。由于图形栏为 100 个像素并且从 x 坐标 100 个像素处开始,因此很容易确定位置的百分比。另外,由于加载了文件,声音文件的 length 属性将具有完整文件的长度(以毫秒为单位)。通过使用声音文件的长度和行中的位置,可以确定声音文件的起始位置。停止播放声音后,声音文件将在选定的起始位置重新开始播放,此位置是作为 play() 方法的 startTime 参数传递的。

 
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 对象。(使用 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

在存在可用于 MP3 声音的 ID3 数据时由 Sound 对象调度。

Event.ID3 常量定义 id3 事件对象的 type 属性值。

此事件具有以下属性:

属性
bubblesfalse
cancelablefalse;没有要取消的默认行为。
currentTarget当前正在使用某个事件侦听器处理 Event 对象的对象。
target加载 MP3 的 Sound 对象,ID3 数据现在可用于此对象。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 类显示为英语。