android surfaceView实例

MainActivity.java
package com.ct13;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

	private Button btn_play, btn_pause, btn_stop, btn_hight, btn_low;
	private SurfaceView surfaceView;
	protected AudioManager audioManager;
	private MediaPlayer mediaPlayer;
	protected static int position = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setWindow();
		setContentView(R.layout.activity_main);

		findViews();
		setListeners();
		audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
		mediaPlayer = new MediaPlayer();
		setSurfaceView();
	}

	private void setWindow() {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		MainActivity.this.getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	private void findViews() {
		// TODO Auto-generated method stub
		btn_play = (Button) this.findViewById(R.id.btn_play);
		btn_pause = (Button) this.findViewById(R.id.btn_pause);
		btn_stop = (Button) this.findViewById(R.id.btn_stop);
		btn_hight = (Button) this.findViewById(R.id.btn_hight);
		btn_low = (Button) this.findViewById(R.id.btn_low);
		surfaceView = (SurfaceView)findViewById(R.id.surface);
	}

	private void setListeners() {
		// TODO Auto-generated method stub
		btn_play.setOnClickListener(this);
		btn_pause.setOnClickListener(this);
		btn_stop.setOnClickListener(this);
		btn_hight.setOnClickListener(this);
		btn_low.setOnClickListener(this);
	}

	private void setSurfaceView() {
		// TODO Auto-generated method stub
		surfaceView.getHolder().setFixedSize(320, 320);
		surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		surfaceView.getHolder().addCallback(new SurceCallBack());

	}

	private boolean isPlaying() {
		return mediaPlayer != null && mediaPlayer.isPlaying();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_play:
			if (isPlaying()) {
				return;
			} else {
				playMedia();
			}
			break;
		case R.id.btn_pause:
			if (isPlaying()) {
				position = mediaPlayer.getCurrentPosition();
				mediaPlayer.pause();
			}
			break;
		case R.id.btn_stop:
			if (isPlaying()) {
				mediaPlayer.stop();
				position = 0;
			} else {
				return;
			}
			break;
		case R.id.btn_hight:
			int volume2 = audioManager
					.getStreamVolume(AudioManager.STREAM_MUSIC);
			if (volume2 > 0) {
				audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
						AudioManager.ADJUST_RAISE, 0);
			} else {
				return;
			}
			break;
		case R.id.btn_low:
			int volume = audioManager
					.getStreamVolume(AudioManager.STREAM_MUSIC);
			if (volume > 0) {
				audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
						AudioManager.ADJUST_LOWER, 0);
			} else {
				return;
			}
			break;

		default:
			break;
		}

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		if (mediaPlayer != null && mediaPlayer.isPlaying()) {
			outState.putInt("position", mediaPlayer.getCurrentPosition());
		}
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
		if (savedInstanceState.containsKey("position")) {
			position = savedInstanceState.getInt("position");
		}
	}

	// 视频播放
	private void playMedia() {
		try {
			File file = new File(Environment.getExternalStorageDirectory(),
					"can_you_feel_my_world.mp4");
			mediaPlayer.reset();
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mediaPlayer.setDataSource(file.getAbsolutePath());
			mediaPlayer.setDisplay(surfaceView.getHolder());
			mediaPlayer.prepare();
			mediaPlayer.start();
			mediaPlayer.seekTo(position);
			position = 0;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private class SurceCallBack implements SurfaceHolder.Callback {

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			if (position > 0) {
				try {
					playMedia();
					position = 0;
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			// TODO Auto-generated method stub

		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.stop();
			}
			mediaPlayer.release();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}



activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="0.15" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.85"
        android:background="@android:color/background_dark"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_play"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="开唱"
            android:textSize="12dp" />

        <Button
            android:id="@+id/btn_pause"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="暂停"
            android:textSize="12dp" />

        <Button
            android:id="@+id/btn_stop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止"
            android:textSize="12dp" />

        <Button
            android:id="@+id/btn_low"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.9"
            android:text="调低音量"
            android:textSize="10dp" />

        <Button
            android:id="@+id/btn_hight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.9"
            android:text="调高音量"
            android:textSize="10dp" />
    </LinearLayout>

</LinearLayout>



manifest.xml:

    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

你可能感兴趣的:(SurfaceView,SurfaceView音量)