这个怎么样:
我想格式化flex中的videoPlayer组件显示的currentTime,如:8230.999 to something like 01:59:59:999 which is "hours:minutes:seconds:milliseconds"
我使用不同的代码集,但它们无法使其工作,因为currentTime也不是正确的毫秒时间,因为它将浮动的3位数点加到秒;
所以代替:2000ms it outputs 2.000
像我这样的人无法理解!
thanx任何帮助:)
###更新我还有毫秒问题。 这是当前的MXML:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
protected function convert_clickHandler(event:MouseEvent):void
{
var val:Number = new Number(inPut.text); //inPut.text = 1000.001
//val = val * 1000;
outPut.text = timeFormat(val);
}
public static function timeFormat(value:Number):String
{
var milliseconds:Number = value % 1000;
var seconds:Number = Math.floor((value/1000) % 60);
var minutes:Number = Math.floor((value/60000) % 60);
var hours:Number = Math.floor((value/3600000) % 24);
var s_miliseconds:String = (milliseconds<10 ? "00" : (milliseconds<100 ? "0" : ""))+ String(milliseconds);
var s_seconds:String = seconds < 10 ? "0" + String(seconds) : String(seconds);
var s_minutes:String = minutes < 10 ? "0" + String(minutes) : String(minutes);
var s_hours:String = hours < 10 ? "0" + String(hours) : String(hours);
return s_hours + ":" + s_minutes + ":" + s_seconds + '.'+s_miliseconds;
// returns 00:00:01.000.0009999999999763531 should return 00:00:01.001
// I still have problem with milliseconds
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:TextInput x="240" y="72" id="inPut" text="1000.001"/>
<s:TextInput x="240" y="140" id="outPut"/>
<s:Button x="274" y="107" label="convert" id="convert" click="convert_clickHandler(event)"/>
</s:Application>
答案 0 :(得分:2)
Thanx在Glycerine和Zaren的帮助下,结合他们的建议,我想出了这个课程:
public class Format
{
public static function currentTime(value:Number):String //example value in mis: 1000
{
value = value * 1000; // convert video currentTime 1.000 to 1000
var milliseconds:Number = Math.round(((value/1000) % 1) * 1000);
var seconds:Number = Math.floor((value/1000) % 60);
var minutes:Number = Math.floor((value/60000) % 60);
var hours:Number = Math.floor((value/3600000) % 24);
var s_miliseconds:String = (milliseconds<10 ? "00" : (milliseconds<100 ? "0" : ""))+ String(milliseconds);
var s_seconds:String = seconds < 10 ? "0" + String(seconds) : String(seconds);
var s_minutes:String = minutes < 10 ? "0" + String(minutes) : String(minutes);
var s_hours:String = hours < 10 ? "0" + String(hours) : String(hours);
return s_hours + ":" + s_minutes + ":" + s_seconds + '.'+s_miliseconds;
}
}
我这样用:
// caculate playhead time
var value:Number = new Number(video_player_monitor.currentTime);
imghri_playhead.text = Format.currentTime(value);
希望它可以帮助别人。再次,谢谢你们的支持; - )
答案 1 :(得分:1)
我做了这个。一个完整的课程。
package com.strangemother.utils
{
public class Time extends Object
{
public function Time()
{
}
public static function timeFormat(value:int):String
{
var seconds:Number = value % 60;
var minutes:Number = Math.floor((value % 3600 ) /60);
var minuteString:String = minutes < 10 ? "0" + String(minutes) + ":" : String(minutes) + ":";
var secondString:String = seconds < 10 ? "0" + String(seconds) : String(seconds);
return minuteString + secondString;
}
}
}
这项工作 - 用Google搜索。
var secondsPerMinute = 60;
var minutesPerHour = 60;
function convertSecondsToHHMMSS(intSecondsToConvert) {
var hours = convertHours(intSecondsToConvert);
var minutes = getRemainingMinutes(intSecondsToConvert);
minutes = (minutes == 60) ? "00" : minutes;
var seconds = getRemainingSeconds(intSecondsToConvert);
return hours+" : "+minutes+" : "+seconds;
}
function convertHours(intSeconds) {
var minutes = convertMinutes(intSeconds);
var hours = Math.floor(minutes/minutesPerHour);
return hours;
}
function convertMinutes(intSeconds) {
return Math.floor(intSeconds/secondsPerMinute);
}
function getRemainingSeconds(intTotalSeconds) {
return (intTotalSeconds%secondsPerMinute);
}
function getRemainingMinutes(intSeconds) {
var intTotalMinutes = convertMinutes(intSeconds);
return (intTotalMinutes%minutesPerHour);
}
trace(convertSecondsToHHMMSS(13345));
答案 2 :(得分:0)
我倾向于使用Date格式化程序来生成正确的输出......
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:Script>
<![CDATA[
public var seconds:Number = 10000;
private function formatTime(event:MouseEvent):void
{
var stamp:Date = new Date(0, 0, 0, 0, 0, seconds, 0)
txtTime.text = fmtTime.format(stamp);
}
]]>
</mx:Script>
<mx:DateFormatter id="fmtTime" formatString="JJ:NN:SS"/>
<mx:Text id="txtTime" text=""/>
<mx:Button click="formatTime(event)"/>
</mx:Application>
我希望能帮到你。
答案 3 :(得分:0)
我需要将字符串“00:02:31:76”转换为秒数。如果有人有兴趣,可以参加以下课程:
包{
public class ConvertStringToTime {
public static function processTimeString(timeString:String):Number {
var timePieceArray:Array = timeString.split(":");
var milliseconds:Number = Number(timePieceArray[3])/100;
var seconds:Number = Number(timePieceArray[2]);
var minutesInSeconds:Number = Number(timePieceArray[1])*60;
var hoursInSeconds:Number = Number(timePieceArray[0])*60*60;
var timeInSeconds:Number = hoursInSeconds + minutesInSeconds + seconds + milliseconds
return timeInSeconds;
}
}
}
然后,如果我想使用它,我可以打电话,例如: 迹(ConvertStringToTime.processTimeString( “00:02:03:25”));
答案 4 :(得分:0)
为什么不在这里:
var time:String = getTimeString(4200000);trace( time );
function getTimeString($milliSeconds:int) : String
{
var seconds:int = ($milliSeconds /1000)%60;
var minutes:int = (($milliSeconds /1000)/60)%60;
var hours:int = $milliSeconds /1000/60/60;
var h:String = hours<10 ? '0' + hours : ''+hours;
var m:String = minutes<10 ? '0' + minutes : ''+minutes;
var s:String = seconds<10 ? '0' + seconds : ''+seconds;
return h+':'+m+':'+s;
}