player

Programming/안드로이드 2012.09.03 01:05 Posted by 파란크리스마스

gplayer.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!--
	<SurfaceView 
	    android:id="@+id/gplayer_surfaceview"
		android:layout_height="fill_parent" 
		android:layout_width="fill_parent">
	</SurfaceView>
	-->
	
    <com.grubin.main.GrubinVideoView
        android:id="@+id/gplayer_surfaceview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </com.grubin.main.GrubinVideoView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="top"
        android:orientation="vertical" >

        <ImageButton
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/btnBack"
            style="@style/MediaButton.Back"
            android:layout_gravity="right"
            android:layout_margin="20dip" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical" />

        <LinearLayout
            android:id="@+id/layoutController"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </LinearLayout>

</FrameLayout>

VideoController.java

package com.grubin.main;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class VideoController extends MediaController2 {

	private Button searchButton;
	
	private ImageButton mPushButton;
	
	private View.OnClickListener mPushListener;
	
	Context _context;

	private int screenWidth;

	private int screenHeight;
	
	public VideoController(Context context, boolean useFastForward, LinearLayout layout) {
		super(context, useFastForward, layout);
		_context = context;
	}
	
	public VideoController(Context context, AttributeSet attrs) {
		super(context, attrs);
		_context = context;
	}
	
	public VideoController(Context context, LinearLayout layout) {
		super(context, layout);
		_context = context;    
	}
	
	public void setScreenSize(int screenWidth, int screenHeight) {
		this.screenWidth = screenWidth;
		this.screenHeight = screenHeight;
	}

	@Override 
	 public void setAnchorView(View view) {
	     super.setAnchorView(view);
	     
	 		LinearLayout L1 = (LinearLayout) getChildAt(0);
			for (int i = 0; i < L1.getChildCount(); i++) {
				View v = L1.getChildAt(i);
				if (v instanceof LinearLayout) {
					LinearLayout buttons = (LinearLayout) v;
					buttons.addView(makePushButton(L1));
					break;
				}
			}
	     
	     /**
	      * 0 : 컨트롤러 전체
	      * 1 : 컨트롤 버튼
	      * 2 : 탐색 바
	      */
	     //this.getc
	     
	     /*
	     LinearLayout layout = (LinearLayout) getChildAt(0);
	     // LinearLayout button = (LinearLayout)layout.getChildAt(1);
	     // LinearLayout seek = (LinearLayout)layout.getChildAt(2);
	     
	     //view.getParent()
	     
	     LayoutInflater inf = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	     LinearLayout linear = (LinearLayout) inf.inflate(R.layout.gplayer_controller, null);
	     layout.addView(linear, 0);
	     */
	}


	/*
	@Override
	public void hide() {
	}
	*/
	
	
	/**
	 * Super ugly hack
	 */
	public void extend() {
		LinearLayout L1 = (LinearLayout) getChildAt(0);
		for (int i = 0; i < L1.getChildCount(); i++) {
			View v = L1.getChildAt(i);
			if (v instanceof LinearLayout) {
				LinearLayout buttons = (LinearLayout) v;
				buttons.addView(makePushButton(L1));
				break;
			}
		}
	}

	protected View makePushButton(ViewGroup parent) {
		LayoutInflater inflate = (LayoutInflater) LayoutInflater.from(_context);
		mPushButton = (ImageButton) inflate.inflate(R.layout.push_button, parent,	false);
		if (mPushListener != null)
			mPushButton.setOnClickListener(mPushListener);
		return mPushButton;
	}  

	public void setPushListener(View.OnClickListener push) {
		mPushListener = push;
		if (mPushButton != null) {
			mPushButton.setOnClickListener(mPushListener);
		}
	}
}

MainActivity.java

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gplayer);
		
		//
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		//
		surfaceView = (GrubinVideoView) findViewById(R.id.gplayer_surfaceview);
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.addCallback(this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

		mediaPlayer = new MediaPlayer();

		mediaPlayer.setOnCompletionListener(this);
		mediaPlayer.setOnErrorListener(this);
		mediaPlayer.setOnInfoListener(this);
		mediaPlayer.setOnPreparedListener(this);
		mediaPlayer.setOnSeekCompleteListener(this);
		mediaPlayer.setOnVideoSizeChangedListener(this);
		
		btnBack = (ImageButton) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) {
      	GPlayer.this.finish();
      } 
		});

		mediaController = (new VideoController(this, (LinearLayout)findViewById(R.id.layoutController)) {
			@Override 
	    public void hide() {
				btnBack.setVisibility(View.INVISIBLE);
				super.hide();
	    }
		});
		
		mediaController.setPushListener(new View.OnClickListener() { 
      public void onClick(View v) {
      	GPlayer.this.finish();
      } 
		});

		Intent intent = getIntent();
		playURI = intent.getStringExtra("url");
		try {
			mediaPlayer.setDataSource(playURI);
		} catch (IllegalArgumentException e) {
			Log.v(LOGTAG, e.getMessage());
			finish();
		} catch (IllegalStateException e) {
			Log.v(LOGTAG, e.getMessage());
			finish();
		} catch (IOException e) {
			Log.v(LOGTAG, e.getMessage());
			finish();
		}
		currentDisplay = getWindowManager().getDefaultDisplay();		
		
		
		//
		mediaController.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) {
      	GPlayer.this.finish();
      } 
		});
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		
		Log.i("xxx", "onTouchEvent=" + ev.getAction());
		
		if(ev.getAction() == MotionEvent.ACTION_DOWN) {
			
			/*
			Toast.makeText(this, "ev.getX()="+ev.getX() + "/" + btnBack.getLeft(), Toast.LENGTH_SHORT).show();
			*/
			
			/*
			Toast.makeText(this, "btnBack.isShown()="+btnBack.isShown(), Toast.LENGTH_SHORT).show();
			if (btnBack.isShown()) {  // if (btnBack.getVisibility() == View.VISIBLE) {
				btnBack.setVisibility(View.INVISIBLE);
			} else {
				btnBack.setVisibility(View.VISIBLE);
			}	
			*/
			
			if (mediaController.isShowing()) {
				mediaController.hide();
				Log.v(LOGTAG, "mediaController.hide()");
			} else {
				try {
					btnBack.setVisibility(View.VISIBLE);
					mediaController.show(10000);
				} catch (Exception e) {
					Log.e(LOGTAG,e.toString(),e);
				}
				Log.v(LOGTAG, "mediaController.show()");
			}
		}
		return false;
	}	
신고


 

티스토리 툴바