Android(java)学习笔记243:多媒体之视频播放器

1. 这里我们还是利用案例演示视频播放器的使用:

(1)首先,我们看看布局文件activity_main.xml,如下:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.himi.videoplayer.MainActivity" > <VideoView
android:id="@+id/vv"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <LinearLayout
android:id="@+id/ll_controller"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" > <LinearLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">" /> <Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="□" /> <Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="||" />
</LinearLayout> <SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout> </
RelativeLayout>

这里VideoView是google封装好的视频播放的控件,与之配套有很多API,直接就可以调用,这里我们希望视频播放时候,屏幕横着,同时这里VideoView很强大,不仅可以播放本地视频,也可以播放网络视频,这里我们测试播放网络视频,利用Tomcat服务器测试,如下:
开启Tomcat服务器,在Tomcat服务器的文件根目录存放文件oppo.mp4,如下图:

Android(java)学习笔记243:多媒体之视频播放器

同时,配置一下AndroidMainfest.xml文件,如下:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.himi.videoplayer"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:screenOrientation="landscape"
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen "  :  播放时设置全屏

android:screenOrientation="landscape" :播放时候设置横屏

不要忘记 添加网络访问的权限: <uses-permission android:name="android.permission.INTERNET"/>

(2)接下来进入MainActivity,如下:

 package com.himi.videoplayer;

 import java.util.Timer;
import java.util.TimerTask; import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.VideoView; public class MainActivity extends Activity {
private SeekBar seekBar1;
private LinearLayout ll_controller;
private VideoView vv;
private Timer timer;// 计时器
private TimerTask task;//计时器任务 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); vv = (VideoView) findViewById(R.id.vv);
ll_controller = (LinearLayout) findViewById(R.id.ll_controller);
seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { public void onStopTrackingTouch(SeekBar seekBar) {
vv.seekTo(seekBar.getProgress());
} public void onStartTrackingTouch(SeekBar seekBar) { } public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) { }
}); vv.setVideoPath("http://49.123.72.40:8080/oppo.mp4");
vv.start(); timer = new Timer();
task = new TimerTask() { @Override
public void run() {
// 每隔一秒执行下run方法 int max = vv.getDuration();// 获取视频的总时长
seekBar1.setMax(max); int process = vv.getCurrentPosition();
seekBar1.setProgress(process); }
};
timer.schedule(task, 0, 1000);//第二参数为表示等待多长时间,第一次启动任务;这里为0,表示马上启动
} @Override
protected void onDestroy() {
timer.cancel();
task.cancel();
timer = null;
task = null;
super.onDestroy();
} @Override
public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (ll_controller.getVisibility() == View.VISIBLE) { ll_controller.setVisibility(View.INVISIBLE);// 隐藏
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1.0f);
ta.setDuration(200);
ll_controller.startAnimation(ta);
} else if (ll_controller.getVisibility() == View.INVISIBLE) { ll_controller.setVisibility(View.VISIBLE);// 显示ll_controller.setVisibility(View.INVISIBLE);//隐藏
final TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0);
ta.setDuration(200);
ll_controller.startAnimation(ta); new Thread() {
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
runOnUiThread(new Runnable() { public void run() {
ll_controller.startAnimation(ta); }
});
};
}.start(); } }
return super.onTouchEvent(event);
} }
上一篇:实战中学习TCPIP模型——互联网层


下一篇:PyQT制作视频播放器