2014年1月2日 星期四

全螢幕具快慢轉簡易PLAYER

全螢幕具快慢轉簡易PLAYER



 


由於工作需要,所以要開發可控制快慢轉PLAYER程式,歡迎有興趣同好,一起來C/P一下。


 









package com.example.framelayout_player;

import java.text.MessageFormat;
import java.util.Timer;
import java.util.TimerTask;

import com.example.framelayout_player.R;
import com.example.framelayout_player.MainActivity;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;

///////////////////////////////////////
import android.view.View;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.view.ViewGroup.MarginLayoutParams;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
public class MainActivity extends Activity {
FrameLayout FrameLayout1;
DisplayMetrics metrics;
VideoView VideoView1;
int width,height;

////////////////
FrameLayout FrameLayout2;
PopupWindow popupWindow;
int bt_width;
int bt_height;
TextView tv_Popup;
ImageView[] IV_Popup;
SeekBar SeekBar_Popup;
Bitmap bmp;
////////////////
int intfouce;
int intlastenter;
int intfastforward;
//////////////
Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////////////////////////////////
requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
metrics = new DisplayMetrics();//抓取螢幕大小
getWindowManager().getDefaultDisplay().getMetrics(metrics);

/////////////////////////////////////////
FrameLayout1 = new FrameLayout(this);
FrameLayout.LayoutParams lparam1 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels);
FrameLayout1.setLayoutParams(lparam1);
this.setContentView(FrameLayout1);

////////////////////////////////////////
VideoView1=new VideoView(this){
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(width, height);
}
};

////////////////////////////////////
VideoView1.setVideoURI(Uri.parse("/sdcard/jash.mp4"));
//MediaController mediaController = new MediaController(this);
//VideoView1.setMediaController(mediaController);

////////////////////////////////////////////////////
FrameLayout.LayoutParams layoutParams_VideoView = new FrameLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels);
layoutParams_VideoView.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams_VideoView.leftMargin = 0;//X
layoutParams_VideoView.topMargin = 0;//Y
VideoView1.setLayoutParams(layoutParams_VideoView);
width=metrics.widthPixels;
height=metrics.heightPixels;
VideoView1.getHolder().setFixedSize(width, height);
VideoView1.requestLayout();
VideoView1.invalidate();

/////////////////////////////////////////////////
FrameLayout2 = new FrameLayout(this);
FrameLayout.LayoutParams lparam2 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels/4);
lparam2.gravity = Gravity.LEFT|Gravity.TOP;
lparam2.leftMargin = 0;//X
lparam2.topMargin = metrics.heightPixels/4*3;//Y
FrameLayout2.setLayoutParams(lparam2);

/////////////////////////////////////////
bt_width=metrics.widthPixels/10;
bt_height=metrics.heightPixels/8;

IV_Popup=new ImageView[4];
IV_Popup[0]=new ImageView(this);
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.open);
IV_Popup[0].setImageBitmap(bmp);
//IV_Popup[0].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams0 = new FrameLayout.LayoutParams(bt_width, bt_height);
layoutParams0.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams0.leftMargin = 0;
layoutParams0.topMargin = 0;
IV_Popup[0].setLayoutParams(layoutParams0);
FrameLayout2.addView(IV_Popup[0]);

IV_Popup[1]=new ImageView(this);
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.left02);
IV_Popup[1].setImageBitmap(bmp);
//IV_Popup[1].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams1 = new FrameLayout.LayoutParams(bt_width, bt_height);
layoutParams1.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams1.leftMargin = (bt_width*2);
layoutParams1.topMargin = 0;
IV_Popup[1].setLayoutParams(layoutParams1);
FrameLayout2.addView(IV_Popup[1]);

IV_Popup[2]=new ImageView(this);
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.stop03);
IV_Popup[2].setImageBitmap(bmp);
//IV_Popup[2].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams2 = new FrameLayout.LayoutParams(bt_width, bt_height);
layoutParams2.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams2.leftMargin = (bt_width*4);
layoutParams2.topMargin = 0;
IV_Popup[2].setLayoutParams(layoutParams2);
FrameLayout2.addView(IV_Popup[2]);

IV_Popup[3]=new ImageView(this);
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.right02);
IV_Popup[3].setImageBitmap(bmp);
//IV_Popup[3].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams3 = new FrameLayout.LayoutParams(bt_width, bt_height);
layoutParams3.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams3.leftMargin = (bt_width*6);
layoutParams3.topMargin = 0;
IV_Popup[3].setLayoutParams(layoutParams3);
FrameLayout2.addView(IV_Popup[3]);

tv_Popup=new TextView(this);
tv_Popup.setText("時間顯示:");
tv_Popup.setWidth(bt_width*2);
tv_Popup.setHeight(bt_height);
FrameLayout.LayoutParams layoutParams_tv = new FrameLayout.LayoutParams(bt_width*2, bt_height);
layoutParams_tv.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams_tv.leftMargin = (bt_width*8);
layoutParams_tv.topMargin = 0;
tv_Popup.setLayoutParams(layoutParams_tv);
FrameLayout2.addView(tv_Popup);

SeekBar_Popup=new SeekBar(this);
//ShapeDrawable thumb = new ShapeDrawable(new OvalShape());
//thumb.setIntrinsicHeight(30);
//thumb.setIntrinsicWidth(80);
//SeekBar_Popup.setThumb(thumb);
SeekBar_Popup.setMax(200);
SeekBar_Popup.setVisibility(View.VISIBLE);
//SeekBar_Popup.setBackgroundColor(Color.BLUE);
SeekBar_Popup.setBackgroundColor(Color.GRAY);
FrameLayout.LayoutParams layoutParams_SeekBar = new FrameLayout.LayoutParams(metrics.widthPixels, bt_height/2);
layoutParams_SeekBar.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams_SeekBar.leftMargin = 0;
layoutParams_SeekBar.topMargin = bt_height/2*3;
SeekBar_Popup.setLayoutParams(layoutParams_SeekBar);
SeekBar_Popup.setProgress(100);
FrameLayout2.addView(SeekBar_Popup);

intfouce=2;//預設第一個按鈕有 駐點
intlastenter=2;
intfastforward=-1;
/////////////////////////////////////////
FrameLayout1.addView(VideoView1);
VideoView1.start();
if (VideoView1.isPlaying())
{
IV_Popup[2].setImageResource(R.drawable.stop03);
}
else
{
IV_Popup[2].setImageResource(R.drawable.start03);
}
timer = new Timer(true);
timer.schedule(new timerTask(), 1000, 1000);
}

public boolean onTouchEvent(MotionEvent e)
{
if(e.getAction()==MotionEvent.ACTION_UP)
{
//String StrToast="Touch Event...";
//Toast.makeText(this, StrToast, Toast.LENGTH_SHORT).show();
////////////////////////////////////////////////
if(popupWindow ==null)
{
popupWindow = new PopupWindow(MainActivity.this);
//popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setWidth(metrics.widthPixels);
popupWindow.setHeight(metrics.heightPixels/4);
popupWindow.setOutsideTouchable(true);
//popupWindow.setFocusable(true);
popupWindow.setContentView(FrameLayout2);
popupWindow.showAtLocation(FrameLayout1, Gravity.LEFT| Gravity.TOP, 0, (metrics.heightPixels/4*3));
}
else
{
popupWindow.dismiss();
popupWindow=null;
}
}
return true;
}
public boolean onKeyDown(int keycode, KeyEvent e){
String str;
str="";
int check_enter;
/////////////////////////////////////////
//把所有按鈕都設定為 沒駐點
IV_Popup[0].setImageResource(R.drawable.open);
IV_Popup[1].setImageResource(R.drawable.left02);
if (VideoView1.isPlaying())
{
IV_Popup[2].setImageResource(R.drawable.stop02);
}
else
{
IV_Popup[2].setImageResource(R.drawable.start02);
}
IV_Popup[3].setImageResource(R.drawable.right02);
check_enter=0;

switch(keycode)
{
case KeyEvent.KEYCODE_DPAD_UP:
switch(intfouce)
{
case 0:
intfouce=2;
SeekBar_Popup.setBackgroundColor(Color.GRAY);
break;
case 1:
case 2:
case 3:
intfouce=0;
SeekBar_Popup.setBackgroundColor(Color.BLUE);
break;
}
str = "上";
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
switch(intfouce)
{
case 0:
intfouce=2;
SeekBar_Popup.setBackgroundColor(Color.GRAY);
break;
case 1:
case 2:
case 3:
intfouce=0;
SeekBar_Popup.setBackgroundColor(Color.BLUE);
break;
}
str = "下";
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
switch(intfouce)
{
case 0:
//intfouce=1;
VideoView1.pause();
SeekBar_Popup.setProgress((SeekBar_Popup.getProgress()-1));
VideoView1.seekTo((int) (SeekBar_Popup.getProgress() * 1.0/ SeekBar_Popup.getMax() * VideoView1.getDuration()));
VideoView1.start();
break;
case 1:
intfouce=3;
break;
case 2:
intfouce=1;
break;
case 3:
intfouce=2;
break;
}
str = "左";
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
switch(intfouce)
{
case 0:
//intfouce=1;
VideoView1.pause();
SeekBar_Popup.setProgress((SeekBar_Popup.getProgress()+1));
VideoView1.seekTo((int) (SeekBar_Popup.getProgress() * 1.0/ SeekBar_Popup.getMax() * VideoView1.getDuration()));
VideoView1.start();
break;
case 1:
intfouce=2;
break;
case 2:
intfouce=3;
break;
case 3:
intfouce=1;
break;
}
str = "右";
break;
case KeyEvent.KEYCODE_ENTER:
//依照駐點所在位置改變圖形
check_enter=1;
switch(intfouce)
{
case 0:
IV_Popup[0].setImageResource(R.drawable.open);
break;
case 1:
IV_Popup[1].setImageResource(R.drawable.left03);
intfastforward=1;
break;
case 2:
if (VideoView1.isPlaying())
{
IV_Popup[2].setImageResource(R.drawable.start03);
VideoView1.pause();
}
else
{
IV_Popup[2].setImageResource(R.drawable.stop03);
VideoView1.start();
}
intfastforward=-1;
break;
case 3:
IV_Popup[3].setImageResource(R.drawable.right03);
intfastforward=2;
break;
}
intlastenter=intfouce;
str = "ENTER";
break;
default:
str = "exit";
this.finish();
}

/////////////////////////////////////////
//依照駐點所在位置改變圖形
if(check_enter==0)
{
//////////////////////////////////////////////
switch(intfouce)
{
case 0:
IV_Popup[0].setImageResource(R.drawable.open);
break;
case 1:
IV_Popup[1].setImageResource(R.drawable.left04);
break;
case 2:
if (VideoView1.isPlaying())
{
IV_Popup[2].setImageResource(R.drawable.stop04);
}
else
{
IV_Popup[2].setImageResource(R.drawable.start04);
}
break;
case 3:
IV_Popup[3].setImageResource(R.drawable.right04);
break;
}

switch(intlastenter)
{
case 0:
if(intfouce==intlastenter)
{
IV_Popup[0].setImageResource(R.drawable.open);
}
else
{
IV_Popup[0].setImageResource(R.drawable.open);
}
break;
case 1:
if(intfouce==intlastenter)
{
IV_Popup[1].setImageResource(R.drawable.left03);
}
else
{
IV_Popup[1].setImageResource(R.drawable.left01);
}
break;
case 2:
if(intfouce==intlastenter)
{
if (VideoView1.isPlaying())
{
IV_Popup[2].setImageResource(R.drawable.stop03);
}
else
{
IV_Popup[2].setImageResource(R.drawable.start03);
}

}
else
{
if (VideoView1.isPlaying())
{
IV_Popup[2].setImageResource(R.drawable.start01);
}
else
{
IV_Popup[2].setImageResource(R.drawable.stop01);
}

}
break;
case 3:
if(intfouce==intlastenter)
{
IV_Popup[3].setImageResource(R.drawable.right03);
}
else
{
IV_Popup[3].setImageResource(R.drawable.right01);
}
break;
}
}
//Toast.makeText(this, str, Toast.LENGTH_SHORT).show();

return super.onKeyDown(keycode,e);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public 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);
}
public class timerTask extends TimerTask{
public void run()
{
int value;
if (VideoView1.isPlaying())
{
if(intfastforward==1)//left
{

VideoView1.pause();
value=(SeekBar_Popup.getProgress()-10);
if(value<=0)
value=0;
SeekBar_Popup.setProgress(value);
VideoView1.seekTo((int) (SeekBar_Popup.getProgress() * 1.0/ SeekBar_Popup.getMax() * VideoView1.getDuration()));
VideoView1.start();
}
if(intfastforward==2)//right
{
VideoView1.pause();
value=(SeekBar_Popup.getProgress()+10);
if(value>=200)
value=200;
SeekBar_Popup.setProgress(value);
VideoView1.seekTo((int) (SeekBar_Popup.getProgress() * 1.0/ SeekBar_Popup.getMax() * VideoView1.getDuration()));
VideoView1.start();
}
float position = VideoView1.getCurrentPosition();
int duration = VideoView1.getDuration();
//tv_Popup.setText(getTimeFormatValue((int) position)+ " / "+ getTimeFormatValue(duration));//設定時間資訊
SeekBar_Popup.setProgress((int) (position/ duration * SeekBar_Popup.getMax()));//設定拉霸位置
}
}
}
}


 


沒有留言:

張貼留言