2011-12-28 3 views
0

모든 것을 제어하는 ​​외부 HTML 인터페이스가있는 플래시 MP3 플레이어가 있는데 한 가지 문제를 제외하고는 제대로 작동합니다. 경과를 표시하는 데 더 좋은 방법이 있다고 생각합니다. 시각!AS3/JavaScript MP3 플레이어 경과 시간 표시

현재 AS3의 ExternalInterface를 통해 경과 시간 값을 JavaScript로 보내고 HTML을 업데이트하고 있습니다. Flash Player는 사운드를 재생할 수있는 1x1xpx 파일입니다. 문제는 (AS3) setInterval()을 사용하여 매 2 초마다 실행하고 그 값을 업데이트한다는 것입니다.

setInterval()과 관련이 없으며 타이머로 Flash 플레이어를 사용하지 않는 것이 더 좋은 방법 일 것이라고 생각합니다. (이 방법은 보이지만 going!)

스테이지에서 아무 것도 사용하지 않기 때문에 Event.ENTER_FRAME을 사용하지 않으려 고 시도했습니다.

+1

Timer over setInterval : http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html#includeExamplesSummary –

답변

1

Lars Blåsjö와 마찬가지로 setInterval() 메서드 대신 Timer 클래스를 사용할 수 있습니다. 이전의 questionTimer 개체를 통합하기 위해 만든 이전 예제를 수정했습니다.

Main.as (문서 클래스) :

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.events.TimerEvent; 
    import flash.external.ExternalInterface; 
    import flash.media.Sound; 
    import flash.media.SoundChannel; 
    import flash.net.URLRequest; 
    import flash.utils.Timer; 

    public class Main extends Sprite 
    { 
     private var _sound:Sound; 
     private var _soundChannel:SoundChannel; 
     private var _onTimer:String; 
     private var _timer:Timer; 
     private var _currentSoundName:String; 

     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 

     }// end function 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 

      if (ExternalInterface.available) 
      { 
       ExternalInterface.addCallback("loadSound", loadSound); 
       ExternalInterface.addCallback("stopSounds", stopSounds); 

      }// end if 

      _onTimer = loaderInfo.parameters["onTimer"] as String; 

     }// end function 

     private function loadSound(name:String, url:String):void 
     { 
      _currentSoundName = name; 
      _sound = new Sound(); 
      _sound.load(new URLRequest(url)); 
      if (_soundChannel) _soundChannel.stop(); 
      _soundChannel = _sound.play(); 

      if (_onTimer) 
      { 
       _timer = new Timer(100); 
       _timer.addEventListener(TimerEvent.TIMER, onTimer); 
       _timer.start(); 

      }// end function 

     }// end function 

     private function stopSounds():void 
     { 
      _soundChannel.stop(); 
      if(_timer) _timer.stop(); 

     }// end function 

     private function onTimer(e:Event):void 
     { 
      var event:Object = 
      { 
       soundChannel : 
       { 
        position: _soundChannel.position 
       }, 
       sound : 
       { 
        name : _currentSoundName, 
        length : _sound.length 
       } 
      } 

      ExternalInterface.call(_onTimer, event); 

     }// end function 

    }// end class 

}// end package 

sounds.json :

{ "sounds" : { 
    "sound": [ 
     { "name": "Sound 1", "url": "sounds/sound1.mp3" }, 
     { "name": "Sound 2", "url": "sounds/sound2.mp3" }, 
     { "name": "Sound 3", "url": "sounds/sound3.mp3" } 
    ] 
}} 

index.html을 :

다음의 예를 살펴 보자
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"/> 
    <title>SoundPlayer</title> 
    <meta name="description" content="" /> 
    <script src="js/swfobject.js"></script> 
    <script src="js/jquery.min.js"></script> 
    <script> 
     var flashvars = { 
      onTimer : "onTimer" 
     }; 
     var params = { 
      menu: "false", 
      scale: "noScale", 
      allowFullscreen: "true", 
      allowScriptAccess: "always", 
      bgcolor: "", 
      wmode: "direct" // can cause issues with FP settings & webcam 
     }; 
     var attributes = { 
      id:"SoundPlayer" 
     }; 
     swfobject.embedSWF(
      "SoundPlayer.swf", 
      "altContent", "0", "0", "10.0.0", 
      "expressInstall.swf", 
      flashvars, params, attributes); 
    </script> 
    <style> 
     html, body { margin-left:25px; height:100%; overflow:hidden; font-family: arial; font-size:15px;} 
     body { margin:0; } 
     .sound-component 
      { margin-top:25px; width:500px; height:100px; background-color:#fafafa; 
       border: 1px solid #e1e1e1; border-radius: 20px; } 
     .sound-component span { padding-left:20px; padding-top:16px; } 
     .sound-component .suffix 
      { width:130px; display:inline-block; font-weight:bold; } 
     .sound-component .name 
      { width: 399px; height: 49px; border-bottom: 1px solid #e1e1e1; 
       border-right: 1px solid #e1e1e1; } 
     .sound-component .position-length 
      { width: 399px; height: 49px; border-right: 1px solid #e1e1e1; } 
     .sound-component .play-stop-button 
      { float:right; width:100px; height:60px; text-align:center; padding-top:40px; 
       font-weight:bold; font-size:18px; color:#c8c8c8; } 
     .sound-component .play-stop-button:hover { color:#323232; } 
    </style> 
</head> 
<body> 
    <script> 

    $(document).ready(function() { 

     // get the flash object 
     var soundPlayer = $("#SoundPlayer").get(0); 

     // load the json file with the list of sounds 
     $.getJSON('json/sounds.json', function(data) { 

      // loop through each sound in the list in the loaded json file 
      $.each(data.sounds.sound, function(i, sound) { 

       // create a html component for the current sound 
       var soundComponent = $(

        '<div class="sound-component">' + 
         '<div class="play-stop-button">Play</div>' + 
         '<div class="name">' + 
          '<span class="suffix">Name: </span>' + 
          '<span class="value"></span>' + 
         '</div>' + 
         '<div class="position-length">' + 
          '<span class="suffix">Position/Length: </span>' + 
          '<span class="value">#</span>' + 
         '</div>' + 
        '</div>' 
       ); 

       $(soundComponent).find(".name .value").text(sound.name); 

       $(soundComponent).find(".play-stop-button").click(function() { 

        var text = $(this).find(".play-stop-button").text(); 

        soundPlayer.loadSound(sound.name, sound.url); 

       }); 

       $(soundComponent).appendTo("body"); 

      }); 

     }); 

    }); 

    function onTimer(event) 
    { 
     $(".sound-component").each(function(i) { 

      var name = $(this).find(".name .value").text(); 

      if(event.sound.name == name) 
      { 
       var position = parseInt(event.soundChannel.position).toFixed(0); 
       var length = parseInt(event.sound.length).toFixed(0); 

       $(this).find(".position-length .value").text(
        position + "/" + length 
       ); 
      } 
      else 
      { 
       $(this).find(".position-length .value").text("#"); 
      } 

     }); 

    } 

    </script> 

    <div id="altContent"> 
     <h1>SoundPlayer</h1> 
     <p><a href="http://www.adobe.com/go/getflashplayer">Get Adobe Flash player</a></p> 
    </div> 

</body> 
</html> 


enter image description here

너무 많은 세부 사항으로가는없이


이, 요점은 다음 53,691,363,210는


index.html 파일의 스크린 샷이 실행되는 및 사운드가 재생되는 플래시 객체가 ExternalInterface.call() 메서드를 사용하여 부모 컨테이너에서 이벤트 핸들러를 호출합니다. Timer 개체가 전달하는 모든 TimerEvent.Timer 이벤트가 호출됩니다. 이벤트 핸들러를 호출하면 재생중인 현재 사운드 (예 : 사운드 이름, 사운드 위치 등)에 대한 정보가 들어있는 이벤트 객체를 파싱합니다.