Sound.loadCompressedDataFromByteArray
前不久Adobe发布了Flash Player 11的release版本, Flash Player 11 带来了很多新特性, 最最红火的就是Stage3D了,但是这里讲的是这个版本的另一个新特性, 直接播放MP3数据 Sound.loadCompressedDataFromByteArray.
以往我们要播放一个mp3,必须要有一个路径才能播放, 每次想要将声音文件缓存起来,就不方便.现在Flash 提供了直接播放bytearray的API,那么就方便多了。
Sound.loadCompressedDataFromByteArray带来的便利
- 直接播放bytearray,而不再需要url地址.
- 可以方便的播放音频片段,而不必加载完整的mp3文件.
- 结合URLStream类,可以方便的实现边听边存功能.
- 可以对保存的音乐进行加密,从而保护版权. 目前只想到以上几点,不过结合这个API肯定能做更多的事。
使用FlahsPlayer11的新特性注意事项
一定要在编译器选项里添加 -swf-version=13 . 否则会运行时报错,找不到相关的API。
在FlashDevelop里设置:
Project菜单 => Properties..子菜单, 打开 Properties面板。
Compiler Options选项卡 => Additional Compiler Options。
输入 -swf-version=13 , 一路确定着出来就行了。
FlashDevelop里设置Flashplayer11编译器选项
在FlashBuilder里设置:
Project菜单 => Properties..子菜单, 打开 Properties面板。
Flex编译器选项 => 附加的编译器参数。
添加 -swf-version=13 ,一路确定着出来就行了。
FlashBuilder里设置Flashplayer11编译器选项
示例
请确保声卡驱动已经安装好,并且能播放声音。
![]()
源代码
package
{
import fl.controls.TextInput;
import fl.events.ComponentEvent;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.net.URLStream;
import flash.text.TextField;
import flash.utils.ByteArray;
import flash.utils.setTimeout;
/**
* Sound.loadCompressedDataFromByteArray
* @author lite3
* @link http://www.litefeel.com
*/
[SWF(width = 600, height = 170)]
public class Main extends Sprite
{
private const SOUND_URL:String = "http://www.sqcbbs.net/data/attachment/forum/music/suyan.mp3";
private var sound:Sound;
private var stream:URLStream;
private var isPlaying:Boolean;
private var sc:SoundChannel;
private var txt:TextField;
private var input:TextInput;
public function Main():void
{
initUI();
sound = new Sound();
stream = new URLStream();
stream.addEventListener(Event.COMPLETE, streamHandler);
stream.addEventListener(ProgressEvent.PROGRESS, streamHandler);
stream.addEventListener(IOErrorEvent.IO_ERROR, streamHandler);
stream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, streamHandler);
var url:String = loaderInfo.parameters.soundURL ? loaderInfo.parameters.soundURL : SOUND_URL;
input.text = url;
loadSound(null);
input.addEventListener(ComponentEvent.ENTER, loadSound);
}
private function loadSound(e:ComponentEvent):void
{
if (!input.text) return;
if (sc != null)
{
sc.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
sc.stop();
sc = null;
}
try {
stream.close();
}catch (e:Error) { }
stream.load(new URLRequest(input.text));
showText("开始加载");
}
private function streamHandler(e:Event):void
{
switch(e.type)
{
case Event.COMPLETE :
showText("加载完成,播放中...");
var byte:ByteArray = new ByteArray();
stream.readBytes(byte, 4000, stream.bytesAvailable/10);
sound.loadCompressedDataFromByteArray(byte, byte.length/2);
playSound();
break;
case ProgressEvent.PROGRESS :
showText("加载进度:" + ProgressEvent(e).bytesLoaded + "/" + ProgressEvent(e).bytesTotal);
break;
case IOErrorEvent.IO_ERROR :
showText("加载失败");
break;
case SecurityErrorEvent.SECURITY_ERROR :
showText("安全沙箱限制,不能从目标网站加载数据");
break;
}
}
private function playSound():void
{
isPlaying = true;
showText("isPlaying: " +isPlaying);
sc = sound.play();
sc.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
private function soundCompleteHandler(e:Event):void
{
isPlaying = false;
showText("isPlaying: " +isPlaying);
setTimeout(playSound, 1000);
}
private function showText(text:String):void
{
txt.text = text;
}
private function initUI():void
{
txt = new TextField();
txt.mouseEnabled = false;
txt.text = "Sound.loadCompressedDataFromByteArray For Flash Player 11";
txt.width = 600;
txt.height = txt.textHeight + 4;
txt.mouseEnabled = false;
txt.autoSize = "center";
txt.y = 10;
addChild(txt);
txt = new TextField();
txt.text = "请输入MP3地址并回车,以更换MP3.";
txt.mouseEnabled = false;
txt.width = txt.textWidth + 4;
txt.height = txt.textHeight + 4;
txt.x = 10;
txt.y = 100;
addChild(txt);
input = new TextInput();
input.width = 550;
input.x = 10;
input.y = 120;
addChild(input);
txt = new TextField();
txt.mouseEnabled = false;
txt.width = 300;
txt.height = 40;
txt.x = 100;
txt.y = 50;
addChild(txt);
}
}
}
Sound.loadCompressedDataFromByteArray的更多相关文章
- java sound初探
网上关于java sound的正规资源讲解的非常好,本文不再给出示例,主要提供一些好的资源,并说说我的一些理解,用于形成对java sound的整体认识. 一.几个词汇 TTS:text-to-spe ...
- TeamViewer 12.0.71503 Patch By.Sound
TeamViewer - the All-In-One Software for Remote Support and Online Meetings - Remote control any com ...
- Principles of measurement of sound intensity
Introduction In accordance with the definition of instantaneous sound intensity as the product of th ...
- ASM:《X86汇编语言-从实模式到保护模式》越计卷:实模式下对DMA和Sound Blaster声卡的控制
说实话越计卷作者用了16页(我还是删过的),来讲怎么控制声卡,其实真正归纳起来就那么几点. ★PART1:直接存储访问 1. 总线控制设备(bus master) 在硬件技术不发达的早期,处理器是最重 ...
- System Sounds: Alerts and Sound Effects
#include <AudioToolbox/AudioToolbox.h> #include <CoreFoundation/CoreFoundation.h> // Def ...
- Sound Generator 原理
Sound Generator 原理 旨在简单的阐述声音如何通过单片机模块来产生. 声音 声音的种类有千千万,但归根到底还是属于波.在对声音进行模拟转数字的时候,采样越高, 声音被还原的越逼真. 声音 ...
- Opensuse enable sound and mic card
Install application pavucontrol Run pavucontrol You will see the configuration about sound card and ...
- I2S (Inter—IC Sound) 总线
I2S I2S(Inter—IC Sound)总线, 又称 集成电路内置音频总线,是飞利浦公司为数字音频设备之间的音频数据传输而制定的一种总线标准,该总线专责于音频设备之间的数据传输,广泛应用于各种多 ...
- 微软Hololens学院教程-Hologram 220-空间声音(Spatial sound )【本文是老版本,与最新的微软教程有出入】
这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦! 原文链接https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...
随机推荐
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...
- LightOJ - 1179 Josephus Problem(约瑟夫环)
题目链接:https://vjudge.net/contest/28079#problem/G 题目大意:约瑟夫环问题,给你n和k(分别代表总人数和每次要数到k),求最后一个人的位置. 解题思路:因为 ...
- LeetCode741. Cherry Pickup
https://leetcode.com/problems/cherry-pickup/description/ In a N x N grid representing a field of che ...
- 高屋建瓴之WebMail攻与防
0x01:前言 随着互联网的快速发展,我们的生活与互联网的联系愈加的紧密.各种快捷方便的信息化通信工具渐渐取代了传统的通信方式.微博.QQ.MSN.微信.陌陌, …这样的社交软件和平台已经成为了我们生 ...
- Python 3.6安装教程
0x01 安装Python 1.1 说明 目前,Python有两个版本,一个是2.x版,一个是3.x版,这两个版本是不兼容的. 本教程安装的是python-3.6.1-amd64版本. Python官 ...
- **[权限控制] 利用CI钩子实现权限认证
http://codeigniter.org.cn/forums/thread-10877-1-1.html 一直没找到CI的权限认证扩展,以前好像找到过一个老外的扩展,不过不怎么好用,现在记不清了, ...
- 阿里云ecs 服务器配置
阿里云ecs 7.0+安装mysql 5.6 http://jingyan.baidu.com/article/454316ab67bd02f7a7c03af4.html 安装jdk yum -y i ...
- Python函数系列-迭代器,生成器
一 迭代器 一 迭代的概念 #迭代器即迭代的工具,那什么是迭代呢?#迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 while True: #只是单纯地重复,因而不 ...
- java面试题一
个人的一点参考总结,如有雷同,纯属巧合! 1.hashmap的实现原理以及hashtable的线程安全是怎么实现的?HashMap其实也是一个线性的数组实现的,所以可以理解为其存储数据的容器就是一个线 ...
- 剖析ironic
关键技术 在安装操作系统时需要存储介质来存储系统镜像.需要控制物理机开关机,在网络部署环境中还需要预启动环境. PXE (预启动环境) IPMI(电源管理) iSCSI(存储) 什么是PXE PXE( ...