13_拍照、录像和音频
Goolgle 从Android OS 2.0版本开始提供照相API功能,可以开发控制照相功能,更在Android OS 2.3版本支持多个相机功能,让相机应用程序可以选择和配置相机。简要介绍拍照、录像和音频。
1. 拍照
1 package cn.eoe.system.camera; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.graphics.Bitmap; 6 import android.os.Bundle; 7 import android.provider.MediaStore; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.ImageView; 12 13 public class Main extends Activity implements OnClickListener { 14 private ImageView imageView; 15 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.main); 20 21 Button btnTakePicture = (Button) findViewById(R.id.btnTakePicture); 22 btnTakePicture.setOnClickListener(this); 23 24 imageView = (ImageView) findViewById(R.id.imageview); 25 26 } 27 28 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 29 if (requestCode == 1) { 30 // 点了拍照按钮,是一个小的对号。 31 if (resultCode == Activity.RESULT_OK) { 32 // 获得图像的字节流 33 Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data"); 34 imageView.setImageBitmap(cameraBitmap); 35 36 } 37 } 38 } 39 40 // 窗口返回,所以使用startActivityForResult 41 public void onClick(View view) { 42 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 43 startActivityForResult(intent, 1); 44 } 45 46 }
2. 录像
1 package cn.eoe.record.video; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.database.Cursor; 6 import android.net.Uri; 7 import android.os.Bundle; 8 import android.provider.MediaStore; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.MediaController; 13 import android.widget.VideoView; 14 15 public class Main extends Activity implements OnClickListener { 16 public VideoView videoView; 17 18 @Override 19 public void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.main); 22 Button btnTakePicture = (Button) findViewById(R.id.btnTakePicture); 23 btnTakePicture.setOnClickListener(this); 24 25 videoView = (VideoView) findViewById(R.id.videoview); 26 27 } 28 29 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 30 if (requestCode == 1) { 31 if (resultCode == Activity.RESULT_OK) { 32 // 视频比较大,不可能放在Intent里面。视频的路径存在系统数据库里面,返回了一个URI 33 // 通过URI找到视频存放路径 34 Uri uri = data.getData(); 35 Cursor cursor = this.getContentResolver().query(uri, null, 36 null, null, null); 37 38 if (cursor.moveToFirst()) { 39 String videoPath = cursor.getString(cursor 40 .getColumnIndex("_data")); 41 videoView.setVideoURI(Uri.parse(videoPath)); 42 videoView.setMediaController(new MediaController(this)); 43 videoView.start(); 44 } 45 } 46 } 47 } 48 49 public void onClick(View view) { 50 Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 51 startActivityForResult(intent, 1); 52 53 } 54 }
3. 音频
1 package cn.eoe.play.music; 2 3 import android.app.Activity; 4 import android.media.MediaPlayer; 5 import android.media.MediaPlayer.OnCompletionListener; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 11 public class Main extends Activity implements OnClickListener, 12 OnCompletionListener { 13 private MediaPlayer mediaPlayer; 14 private Button btnPause; 15 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.main); 20 21 Button btnStart1 = (Button) findViewById(R.id.btnStart1); 22 Button btnStart2 = (Button) findViewById(R.id.btnStart2); 23 24 Button btnStop = (Button) findViewById(R.id.btnStop); 25 26 btnPause = (Button) findViewById(R.id.btnPause); 27 28 btnStart1.setOnClickListener(this); 29 btnStart2.setOnClickListener(this); 30 btnStop.setOnClickListener(this); 31 btnPause.setOnClickListener(this); 32 33 } 34 35 public void onCompletion(MediaPlayer mp) { 36 mp.release(); 37 setTitle("资源已经释放"); 38 39 } 40 41 public void onClick(View view) { 42 try { 43 switch (view.getId()) { 44 case R.id.btnStart1: // 播放音频资源 45 mediaPlayer = MediaPlayer.create(this, R.raw.music); 46 mediaPlayer.setOnCompletionListener(this); 47 48 mediaPlayer.start(); 49 break; 50 51 case R.id.btnStart2:// 播放SD卡上的音频文件(/sdcard/music.mp3) 52 mediaPlayer = new MediaPlayer(); 53 mediaPlayer.setDataSource("/sdcard/music.mp3"); 54 55 mediaPlayer.prepare(); 56 mediaPlayer.start(); 57 break; 58 59 case R.id.btnStop: 60 if (mediaPlayer != null) { 61 if (mediaPlayer.isPlaying()) 62 mediaPlayer.stop(); 63 } 64 break; 65 66 case R.id.btnPause: 67 if (mediaPlayer != null) { 68 if ("播放".equals(btnPause.getText().toString())) { 69 mediaPlayer.start(); 70 btnPause.setText("暂停"); 71 72 } else if ("暂停".equals(btnPause.getText().toString())) { 73 mediaPlayer.pause(); 74 btnPause.setText("播放"); 75 } 76 } 77 break; 78 79 default: 80 break; 81 } 82 } catch (Exception e) { 83 // TODO: handle exception 84 } 85 } 86 }