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


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

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

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     tools:context="com.himi.videoplayer.MainActivity" >
     6 
     7     <VideoView
     8         android:id="@+id/vv"
     9         android:layout_width="match_parent"
    10         android:layout_height="match_parent" />
    11 
    12     <LinearLayout
    13         android:id="@+id/ll_controller"
    14         android:visibility="invisible"
    15         android:layout_width="match_parent"
    16         android:layout_height="wrap_content"
    17         android:layout_alignParentBottom="true" 
    18         android:orientation="vertical" >
    19 
    20         <LinearLayout
    21             android:gravity="center_horizontal"
    22             android:layout_width="match_parent"
    23             android:layout_height="wrap_content"       
    24             android:orientation="horizontal">
    25 
    26             <Button
    27                 android:id="@+id/play"
    28                 android:layout_width="wrap_content"
    29                 android:layout_height="wrap_content"
    30                 android:text=">" />
    31 
    32             <Button
    33                 android:id="@+id/pause"
    34                 android:layout_width="wrap_content"
    35                 android:layout_height="wrap_content"
    36                 android:text="□" />
    37 
    38             <Button
    39                 android:id="@+id/stop"
    40                 android:layout_width="wrap_content"
    41                 android:layout_height="wrap_content"
    42                 android:text="||" />
    43         </LinearLayout>
    44 
    45         <SeekBar
    46             android:id="@+id/seekBar1"
    47             android:layout_width="match_parent"
    48             android:layout_height="wrap_content" />
    49     </LinearLayout>
    50 
    51 </RelativeLayout>

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

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

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.himi.videoplayer"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-sdk
     8         android:minSdkVersion="15"
     9         android:targetSdkVersion="17" />
    10     <uses-permission android:name="android.permission.INTERNET"/>
    11 
    12     <application
    13         android:allowBackup="true"
    14         android:icon="@drawable/ic_launcher"
    15         android:label="@string/app_name"
    16         android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
    17         <activity
    18             android:screenOrientation="landscape"
    19             android:name=".MainActivity"
    20             android:label="@string/app_name" >
    21             <intent-filter>
    22                 <action android:name="android.intent.action.MAIN" />
    23 
    24                 <category android:name="android.intent.category.LAUNCHER" />
    25             </intent-filter>
    26         </activity>
    27     </application>
    28 
    29 </manifest>

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

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

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

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

      1 package com.himi.videoplayer;
      2 
      3 import java.util.Timer;
      4 import java.util.TimerTask;
      5 
      6 import android.app.Activity;
      7 import android.os.Bundle;
      8 import android.view.MotionEvent;
      9 import android.view.View;
     10 import android.view.animation.Animation;
     11 import android.view.animation.TranslateAnimation;
     12 import android.widget.LinearLayout;
     13 import android.widget.SeekBar;
     14 import android.widget.SeekBar.OnSeekBarChangeListener;
     15 import android.widget.VideoView;
     16 
     17 public class MainActivity extends Activity {
     18     private SeekBar seekBar1;
     19     private LinearLayout ll_controller;
     20     private VideoView vv;
     21     private Timer timer;// 计时器
     22     private TimerTask task;//计时器任务
     23 
     24     @Override
     25     protected void onCreate(Bundle savedInstanceState) {
     26         super.onCreate(savedInstanceState);
     27         setContentView(R.layout.activity_main);
     28 
     29         vv = (VideoView) findViewById(R.id.vv);
     30         ll_controller = (LinearLayout) findViewById(R.id.ll_controller);
     31         seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
     32         seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
     33 
     34             public void onStopTrackingTouch(SeekBar seekBar) {
     35                 vv.seekTo(seekBar.getProgress());
     36             }
     37 
     38             public void onStartTrackingTouch(SeekBar seekBar) {
     39 
     40             }
     41 
     42             public void onProgressChanged(SeekBar seekBar, int progress,
     43                     boolean fromUser) {
     44 
     45             }
     46         });
     47 
     48         vv.setVideoPath("http://49.123.72.40:8080/oppo.mp4");
     49         vv.start();
     50 
     51         timer = new Timer();
     52         task = new TimerTask() {
     53 
     54             @Override
     55             public void run() {
     56                 // 每隔一秒执行下run方法
     57 
     58                 int max = vv.getDuration();// 获取视频的总时长
     59                 seekBar1.setMax(max);
     60 
     61                 int process = vv.getCurrentPosition();
     62                 seekBar1.setProgress(process);
     63 
     64             }
     65         };
     66         timer.schedule(task, 0, 1000);//第二参数为表示等待多长时间,第一次启动任务;这里为0,表示马上启动
     67     }
     68 
     69     @Override
     70     protected void onDestroy() {
     71         timer.cancel();
     72         task.cancel();
     73         timer = null;
     74         task = null;
     75         super.onDestroy();
     76     }
     77 
     78     @Override
     79     public boolean onTouchEvent(MotionEvent event) {
     80 
     81         if (event.getAction() == MotionEvent.ACTION_DOWN) {
     82 
     83             if (ll_controller.getVisibility() == View.VISIBLE) {
     84 
     85                 ll_controller.setVisibility(View.INVISIBLE);// 隐藏
     86                 TranslateAnimation ta = new TranslateAnimation(
     87                         Animation.RELATIVE_TO_SELF, 0,
     88                         Animation.RELATIVE_TO_SELF, 0,
     89                         Animation.RELATIVE_TO_SELF, 0,
     90                         Animation.RELATIVE_TO_SELF, 1.0f);
     91                 ta.setDuration(200);
     92                 ll_controller.startAnimation(ta);
     93             } else if (ll_controller.getVisibility() == View.INVISIBLE) {
     94 
     95                 ll_controller.setVisibility(View.VISIBLE);// 显示ll_controller.setVisibility(View.INVISIBLE);//隐藏
     96                 final TranslateAnimation ta = new TranslateAnimation(
     97                         Animation.RELATIVE_TO_SELF, 0,
     98                         Animation.RELATIVE_TO_SELF, 0,
     99                         Animation.RELATIVE_TO_SELF, 1.0f,
    100                         Animation.RELATIVE_TO_SELF, 0);
    101                 ta.setDuration(200);
    102                 ll_controller.startAnimation(ta);
    103 
    104                 new Thread() {
    105                     public void run() {
    106                         try {
    107                             Thread.sleep(2000);
    108                         } catch (InterruptedException e) {
    109                             // TODO 自动生成的 catch 块
    110                             e.printStackTrace();
    111                         }
    112                         runOnUiThread(new Runnable() {
    113 
    114                             public void run() {
    115                                 ll_controller.startAnimation(ta);
    116 
    117                             }
    118                         });
    119                     };
    120                 }.start();
    121 
    122             }
    123 
    124         }
    125         return super.onTouchEvent(event);
    126     }
    127 
    128 }
  • 相关阅读:
    1. Two Sum
    100. Same Tree
    101. Symmetric Tree
    103. Binary Tree Zigzag Level Order Traversal
    102. Binary Tree Level Order Traversal
    STL的一些技巧函数使用
    104. Maximum Depth of Binary Tree
    apache开源项目--solr
    apache开源项目--HBase
    apache开源项目--ZooKeeper
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4805341.html
Copyright © 2020-2023  润新知