在在播放视频前增加等待画面和进度提示中实现了简单的播放器,最近将逐渐演进,成为名副其实的定制版本播放器。本文做一个简单的定制,实际还没有动VedioView类,只是在布局上为播放器增加一些外围的信息提示。算是定制的热身运动。
效果:
通过相对布局,在加载的时候提示loading字样的文字,在加载到视频后,提示片名,当前时间,片子的时长和当前播放的时长。
首先要在原来布局文件上,加入一些TextView,用于显示提示文字,另外,要更换成相对布局(RelativeLayout),这样才能很容易确定位置。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_width="fill_parent"
android:gravity="center" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView android:id="@+id/videoView" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<TextView android:id="@+id/timeText" android:focusable="false"
android:visibility="invisible" android:focusableInTouchMode="false"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="5.0dip" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView android:gravity="center" android:id="@+id/videoTitle"
android:focusable="false" android:text="Loading …"
android:focusableInTouchMode="false" android:visibility="visible"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="10.0dip" android:layout_marginRight="10.0dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView android:id="@+id/clockText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="5.0dip"
android:layout_marginRight="5.0dip" android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
</TextView>
</RelativeLayout>
<LinearLayout android:layout_width="fill_parent"
android:gravity="center" android:layout_height="fill_parent">
<ImageView android:id="@+id/image" android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
</FrameLayout>
代码上应该问题不大,就是将这些TextView,在加载完成前,不显示,加载后,再显示。
显示时间,有点儿特殊,比如片子的时长,或者当前进度的时间,都不是按照日期的时间。如果直接用DateFormat来做,将出现问题,比如时长是1分钟1秒,显示会变成8点01分01秒,这是因为中国处在东8区,因此比格林尼治时间早8小时。
那么怎么显示呢,也很简单:
private static String getTimeFormatValue(long time) {
return MessageFormat.format(
"{0,number,00}:{1,number,00}:{2,number,00}",
time / 1000 / 60 / 60, time / 1000 / 60 % 60, time / 1000 % 60);
}
源代码见:
留言列表