package tw.edu.hust.bn98107;

import java.io.IOException;

import tw.edu.hust.bn98107.R;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class BallGameActivity extends Activity implements Callback {
	
	private SurfaceHolder sHolder;
	private MediaPlayer mp3;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        final GameSurfaceView gv = new GameSurfaceView(this);
        sHolder = gv.getHolder();
        sHolder.addCallback(this);
        setContentView(gv);
        //
		new Thread() {
			public void run() {
				// TODO Auto-generated method stub
				while (true) {
					gv.moveBall(sHolder);
					try {
						sleep(30);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}.start();
		//
		mp3=MediaPlayer.create(this,R.raw.d3);
		mp3.start();
    }
    
    
    
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}
	
	
	//
	public void onPause() {
		super.onPause();
		if (mp3 != null ) 
			mp3.pause();
	}
	//
	boolean bStopped = false;
	int pos;
	public void onStop() {
		super.onStop();
		if (mp3 != null) {
			pos = mp3.getCurrentPosition();
			mp3.stop();
			bStopped = true;
		}
	}
	//
	public void onResume() {
		super.onResume();
		mp3.start();
	}
	//
	public void onStart() {
		super.onStart();
		if (bStopped) {
			try {
				mp3.prepare();
				mp3.seekTo(pos);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}