Jquery Win7中Chrome的外部接口行为
我的网站上有一个mp3player swf。 当mp3文件完全加载到我的swf中时,为了显示页面并开始播放音乐,我添加了一个动作脚本函数来发送一个调用外部接口 当mp3文件已完全加载时 作为 我的html jquery函数Jquery Win7中Chrome的外部接口行为,jquery,flash,google-chrome,Jquery,Flash,Google Chrome,我的网站上有一个mp3player swf。 当mp3文件完全加载到我的swf中时,为了显示页面并开始播放音乐,我添加了一个动作脚本函数来发送一个调用外部接口 当mp3文件已完全加载时 作为 我的html jquery函数 function JSCallback() { // Finish load swf and hide overlay $('#loader-overlay').fadeOut('slow'); } 此方法在Internet Explorer 8、Firefox、Chrom
function JSCallback()
{
// Finish load swf and hide overlay
$('#loader-overlay').fadeOut('slow');
}
此方法在Internet Explorer 8、Firefox、Chrome和XP上的Opera中运行良好。
在Firefox上的Win7中,IE 10运行良好,而chrome则在音乐完全加载并显示页面之前开始播放音乐。
你知道怎么解决这个问题吗?谢谢
全班
package xxx.xxx.xxx.sound {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.text.TextField;
import flash.external.ExternalInterface;
public class MP3Player extends MovieClip {
// Location of the config xml file
public static const CONFIG_XML_URL:String = "xml/config.xml";
// URL of the mp3 file
public static var MP3_URL:String = "mp3/track.mp3";
// Main color for the mp3 player
public static var MAIN_COLOR:uint = 0xFF0000;
// An XMLLoader to load the configuration file
private var xmlLoader:XMLLoader;
// Sound object to be played
private var mp3:Sound = new Sound();
// A sound channel to play the sound object
private var channel:SoundChannel;
// Holds the pause position
private var pausePos:Number;
// A byte array to read spectrum
private var bytes:ByteArray = new ByteArray();
// Indicates whether the mp3 player is playing or not.
private var _isPlaying:Boolean = false;
// Holds the previous label color
private var _prevColor:uint;
public function MP3Player() {
// Initialize the player
init();
}
/**
* Initializes the player.
*/
private function init():void {
// Use as a button
useHandCursor = true;
buttonMode = true;
mouseChildren = false;
equalizer.alpha = 0;
// Add necessary event listeners
addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true);
addEventListener(MouseEvent.MOUSE_OVER, onMouseOver, false, 0, true);
addEventListener(MouseEvent.MOUSE_OUT, onMouseOut, false, 0, true);
loadConfig();
}
/**
* Sets the color of the player to the given parameter.
*/
public function setColor(color:uint):void {
MAIN_COLOR = color;
// Change the color of the equalizer
var colorTransform:ColorTransform = equalizer.transform.colorTransform;
colorTransform.color = MAIN_COLOR;
equalizer.transform.colorTransform = colorTransform;
}
/**
* Loads the configuration file to the memory.
*/
private function loadConfig() {
// Start loading the config.xml file
xmlLoader = new XMLLoader(CONFIG_XML_URL);
xmlLoader.addEventListener(XMLLoader.XML_LOADED, onXMLLoaded);
xmlLoader.load();
}
/**
* This method is called when the xml file is loaded to the memory.
*/
private function onXMLLoaded(evt:Event):void {
// Get configuration parameters
var xml:XML = xmlLoader.getXML();
MP3_URL = xml.@mp3URL;
MAIN_COLOR = xml.@color;
// Change the color of the equalizer
equalizer.alpha = 1;
setColor(MAIN_COLOR);
// Start loading the mp3
loadMP3();
}
/**
* Start loading the mp3 file.
*/
private function loadMP3():void {
mp3.load(new URLRequest(MP3_URL));
mp3.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
channel = mp3.play();
channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete, false, 0, true);
_isPlaying = true;
// Load complete function
mp3.addEventListener(Event.COMPLETE, onLoadComplete);
}
/**
* This method is called whenever a new enter frame event occurs.
*/
private function onEnterFrame(evt:Event):void {
try {
SoundMixer.computeSpectrum(bytes, true, 0);
} catch (e:Error) {
}
equalizer.update(bytes);
}
/**
* This method is called when playing the sound is finished.
*/
private function onSoundComplete(evt:Event):void {
// Loop
channel = mp3.play();
channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete, false, 0, true);
_isPlaying = true;
}
/**
* This method is called when the mouse is over the mp3 player.
*/
private function onMouseOver(evt:MouseEvent):void {
// Change the color of the equalizer
_prevColor = label.textColor;
label.textColor = MAIN_COLOR;
}
/**
* This method is called when the mouse leaves the mp3 player.
*/
private function onMouseOut(evt:MouseEvent):void {
label.textColor = _prevColor;
}
/**
* This method is called when the mouse is clicked on the mp3 player.
*/
private function onMouseDown(evt:MouseEvent):void {
if (_isPlaying) {
pausePos = channel.position;
channel.stop();
_isPlaying = false;
label.text = "MUSIC OFF";
} else {
channel = mp3.play(pausePos);
channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete, false, 0, true);
_isPlaying = true;
label.text = "MUSIC ON";
}
}
/**
* This method is called when load complete and sent call to js.
*/
private function onLoadComplete(e:Event):void {
// This will call a JavaScript function
//ExternalInterface.call('loadedCallback');
if (ExternalInterface.available)
{
ExternalInterface.call("JSCallback");
}
}
/**
* This method is called if an IO error occurs.
*/
private function onIOError(evt:IOErrorEvent):void {
}
}
}
您的问题是,在方法
loadMP3()
中,mp3.play()
在mp3.load()
之后立即调用。这意味着音乐必须尽快开始。对于声音类,“尽快”不是“在mp3充满电后”,而是“只要我有足够的数据”
如果要将音乐延迟到文件完全加载之后,则必须从loadMP3()
中删除play()
,然后将其添加到onLoadComplete()
[……]
你能告诉我们你的代码中调用mp3的load()和play()方法的部分吗?请记住,在使用
Sound
类时,您不需要在开始播放之前完全下载文件。我发布了完整的类。我想改变这个类,我发现已经做了,但不幸的是,我不能得到我想要的。如果你能帮忙,我将不胜感激。thanksOk这可能是一个很好的解决方案,但是如果我想在音乐开始播放时使用ExternalInterface调用,而不是在声音满负荷时使用ExternalInterface调用,我该怎么做呢?感谢使用ExternalInterface.addCallback()
,您可以创建一个可以由JS脚本调用的方法。请在AS3参考中查找:
package xxx.xxx.xxx.sound {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.text.TextField;
import flash.external.ExternalInterface;
public class MP3Player extends MovieClip {
// Location of the config xml file
public static const CONFIG_XML_URL:String = "xml/config.xml";
// URL of the mp3 file
public static var MP3_URL:String = "mp3/track.mp3";
// Main color for the mp3 player
public static var MAIN_COLOR:uint = 0xFF0000;
// An XMLLoader to load the configuration file
private var xmlLoader:XMLLoader;
// Sound object to be played
private var mp3:Sound = new Sound();
// A sound channel to play the sound object
private var channel:SoundChannel;
// Holds the pause position
private var pausePos:Number;
// A byte array to read spectrum
private var bytes:ByteArray = new ByteArray();
// Indicates whether the mp3 player is playing or not.
private var _isPlaying:Boolean = false;
// Holds the previous label color
private var _prevColor:uint;
public function MP3Player() {
// Initialize the player
init();
}
/**
* Initializes the player.
*/
private function init():void {
// Use as a button
useHandCursor = true;
buttonMode = true;
mouseChildren = false;
equalizer.alpha = 0;
// Add necessary event listeners
addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true);
addEventListener(MouseEvent.MOUSE_OVER, onMouseOver, false, 0, true);
addEventListener(MouseEvent.MOUSE_OUT, onMouseOut, false, 0, true);
loadConfig();
}
/**
* Sets the color of the player to the given parameter.
*/
public function setColor(color:uint):void {
MAIN_COLOR = color;
// Change the color of the equalizer
var colorTransform:ColorTransform = equalizer.transform.colorTransform;
colorTransform.color = MAIN_COLOR;
equalizer.transform.colorTransform = colorTransform;
}
/**
* Loads the configuration file to the memory.
*/
private function loadConfig() {
// Start loading the config.xml file
xmlLoader = new XMLLoader(CONFIG_XML_URL);
xmlLoader.addEventListener(XMLLoader.XML_LOADED, onXMLLoaded);
xmlLoader.load();
}
/**
* This method is called when the xml file is loaded to the memory.
*/
private function onXMLLoaded(evt:Event):void {
// Get configuration parameters
var xml:XML = xmlLoader.getXML();
MP3_URL = xml.@mp3URL;
MAIN_COLOR = xml.@color;
// Change the color of the equalizer
equalizer.alpha = 1;
setColor(MAIN_COLOR);
// Start loading the mp3
loadMP3();
}
/**
* Start loading the mp3 file.
*/
private function loadMP3():void {
mp3.load(new URLRequest(MP3_URL));
mp3.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
channel = mp3.play();
channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete, false, 0, true);
_isPlaying = true;
// Load complete function
mp3.addEventListener(Event.COMPLETE, onLoadComplete);
}
/**
* This method is called whenever a new enter frame event occurs.
*/
private function onEnterFrame(evt:Event):void {
try {
SoundMixer.computeSpectrum(bytes, true, 0);
} catch (e:Error) {
}
equalizer.update(bytes);
}
/**
* This method is called when playing the sound is finished.
*/
private function onSoundComplete(evt:Event):void {
// Loop
channel = mp3.play();
channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete, false, 0, true);
_isPlaying = true;
}
/**
* This method is called when the mouse is over the mp3 player.
*/
private function onMouseOver(evt:MouseEvent):void {
// Change the color of the equalizer
_prevColor = label.textColor;
label.textColor = MAIN_COLOR;
}
/**
* This method is called when the mouse leaves the mp3 player.
*/
private function onMouseOut(evt:MouseEvent):void {
label.textColor = _prevColor;
}
/**
* This method is called when the mouse is clicked on the mp3 player.
*/
private function onMouseDown(evt:MouseEvent):void {
if (_isPlaying) {
pausePos = channel.position;
channel.stop();
_isPlaying = false;
label.text = "MUSIC OFF";
} else {
channel = mp3.play(pausePos);
channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete, false, 0, true);
_isPlaying = true;
label.text = "MUSIC ON";
}
}
/**
* This method is called when load complete and sent call to js.
*/
private function onLoadComplete(e:Event):void {
// This will call a JavaScript function
//ExternalInterface.call('loadedCallback');
if (ExternalInterface.available)
{
ExternalInterface.call("JSCallback");
}
}
/**
* This method is called if an IO error occurs.
*/
private function onIOError(evt:IOErrorEvent):void {
}
}
}
private function loadMP3():void {
mp3.load(new URLRequest(MP3_URL));
mp3.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
/* // removed
channel = mp3.play();
channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete, false, 0, true);
_isPlaying = true;
/**/
// Load complete function
mp3.addEventListener(Event.COMPLETE, onLoadComplete);
}
private function onLoadComplete(e:Event):void {
channel = mp3.play();
channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete, false, 0, true);
_isPlaying = true;
if (ExternalInterface.available)
{
ExternalInterface.call("JSCallback");
}
}