package com.sdk.example;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.wole56.sdk.Video;

/**
 * 播放视频,支持本地和远程视频
 * 
 * @author susie
 * 
 */
public class PlayVideo extends Activity {
	private EditText localTxtV;
	private EditText remoteTxtV;
	private Button localBtn;
	private Button remoteBtn;
	private Button selectBtn;
	private static final int LIB = 1; // load video from lib
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.video_play);

		// init ui
		localTxtV = (EditText) findViewById(R.id.local_video_txt);
		remoteTxtV = (EditText) findViewById(R.id.remote_video_txt);
		localBtn = (Button) findViewById(R.id.play_local_btn);
		remoteBtn = (Button) findViewById(R.id.play_remote_btn);
		selectBtn = (Button) findViewById(R.id.select_video_btn);

		// set local video path
		// _localTxtV.setText("/mnt/sdcard/DCIM/Camera/20120726_152520.mp4");
		remoteTxtV
				.setText("http://f9.r.56.com/f9.c80.56.com/flvdownload/21/2/sc_mp4_134485112672hd_qvga.mp4");

		OnClickListener click = new OnClickListener() {

			@Override
			public void onClick(View view) {
				if (view == selectBtn) {
					Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
					intent.setType("video/*");
					startActivityForResult(intent, LIB);
				} else {
					String videoPath = "";
					if (view == localBtn) {
						videoPath = localTxtV.getText().toString().trim();
					} else {
						videoPath = remoteTxtV.getText().toString().trim();
					}
					Video.play(getApplicationContext(), videoPath);
				}
			}
		};

		localBtn.setOnClickListener(click);
		remoteBtn.setOnClickListener(click);
		selectBtn.setOnClickListener(click);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data == null) {
			return;
		}
		if (resultCode == Activity.RESULT_OK) {
			Uri videoUri = data.getData();

			switch (requestCode) {
			case LIB: {
				String videoPath = Video.getFilePath(PlayVideo.this, videoUri);
				localTxtV.setText(videoPath);
				break;
			}
			default:
				break;
			}
		}
	}
}
