ITeye博客

Android 调用系统播放器,调用系统Camera

Java-Object · 2014-10-17 15:12 · Android技术 · 编程语言 · Markdown

转载自:http://blog.csdn.net/yudajun/article/details/7752226

1、调用系统音乐播放器

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. private void playAudio(String audioPath){
  2. Intent intent = new Intent();
  3. intent.setAction(android.content.Intent.ACTION_VIEW);
  4. intent.setDataAndType(Uri.parse(audioPath), "audio/mp3");
  5. intent.setComponent(new ComponentName("com.android.music","com.android.music.MediaPlaybackActivity"));
  6. startActivity(intent);
  7. }

或者

[java] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. Intent it = new Intent(Intent.ACTION_VIEW);
  2. it.setDataAndType(Uri.parse("/sdcard/111.mp3"), "audio/mp3");
  3. startActivity(it);

 

 

2、调用系统视频播放器

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. private void playVideo(String videoPath){
  2. Intent intent = new Intent(Intent.ACTION_VIEW);
  3. String strend="";
  4. if(videoPath.toLowerCase().endsWith(".mp4")){
  5. strend="mp4";
  6. }
  7. else if(videoPath.toLowerCase().endsWith(".3gp")){
  8. strend="3gp";
  9. }
  10. else if(videoPath.toLowerCase().endsWith(".mov")){
  11. strend="mov";
  12. }
  13. else if(videoPath.toLowerCase().endsWith(".wmv")){
  14. strend="wmv";
  15. }
  16. intent.setDataAndType(Uri.parse(videoPath), "video/"+strend);
  17. startActivity(intent);
  18. }

 

 

或者

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. Intent it = new Intent(Intent.ACTION_VIEW);
  2. it.setDataAndType(Uri.parse("/sdcard/1122.mp4"), "video/mp4");
  3. startActivity(it);

 

 

播放来自网络多媒体文件

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. String extension = MimeTypeMap.getFileExtensionFromUrl(url);
  2. String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
  3. Intent mediaIntent = new Intent(Intent.ACTION_VIEW);
  4. mediaIntent.setDataAndType(Uri.parse(url), mimeType);
  5. startActivity(mediaIntent);

 

 

调用系统Camera 

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. public class SysCamera extends Activity {
  2. public static final int MEDIA_TYPE_IMAGE = 1;
  3. public static final int MEDIA_TYPE_VIDEO = 2;
  4. private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
  5. private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
  6. private Uri fileUri;
  7. Button btvideo,btphoto;
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.syscamera);
  12. btphoto=(Button)findViewById(R.id.sysPhoto);
  13. btvideo=(Button)findViewById(R.id.sysVideo);
  14. btphoto.setOnClickListener(new Monitor());
  15. btvideo.setOnClickListener(new Monitor());
  16. }
  17. private void PhotoIntent(){
  18. // create Intent to take a picture
  19. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  20. fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
  21. intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
  22. // start the image capture Intent
  23. startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
  24. }
  25. private void VideoIntent(){
  26. //create new Intent
  27. Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
  28. fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);  // create a file to save the video
  29. intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name
  30. intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
  31. // start the Video Capture Intent
  32. startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
  33. }
  34. /** Create a file Uri for saving an image or video */
  35. private static Uri getOutputMediaFileUri(int type){
  36. return Uri.fromFile(getOutputMediaFile(type));
  37. }
  38. /** Create a File for saving an image or video */
  39. private static File getOutputMediaFile(int type){
  40. if (Environment.getExternalStorageState() == null){
  41. return null;
  42. }
  43. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
  44. Environment.DIRECTORY_PICTURES), "MyCameraApp");
  45. // Create the storage directory if it does not exist
  46. if (! mediaStorageDir.exists()){
  47. if (! mediaStorageDir.mkdirs()){
  48. Log.d("MyCameraApp", "failed to create directory");
  49. return null;
  50. }
  51. }
  52. // Create a media file name
  53. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  54. File mediaFile;
  55. if (type == MEDIA_TYPE_IMAGE){
  56. mediaFile = new File(mediaStorageDir.getPath() + File.separator +
  57. "IMG_"+ timeStamp + ".jpg");
  58. } else if(type == MEDIA_TYPE_VIDEO) {
  59. mediaFile = new File(mediaStorageDir.getPath() + File.separator +
  60. "VID_"+ timeStamp + ".mp4");
  61. } else {
  62. return null;
  63. }
  64. return mediaFile;
  65. }
  66. @Override
  67. public void onBackPressed() {
  68. Intent in=new Intent(this, MyRecorderActivity.class);
  69. startActivity(in);
  70. super.onBackPressed();
  71. }
  72. @Override
  73. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  74. if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
  75. if (resultCode == RESULT_OK) {
  76. // Image captured and saved to fileUri specified in the Intent
  77. Toast.makeText(this, "Image saved to:\n" +
  78. data.getData(), Toast.LENGTH_LONG).show();
  79. } else if (resultCode == RESULT_CANCELED) {
  80. // User cancelled the image capture
  81. } else {
  82. // Image capture failed, advise user
  83. }
  84. }
  85. if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
  86. if (resultCode == RESULT_OK) {
  87. // Video captured and saved to fileUri specified in the Intent
  88. Toast.makeText(this, "Video saved to:\n" +
  89. data.getData(), Toast.LENGTH_LONG).show();
  90. } else if (resultCode == RESULT_CANCELED) {
  91. // User cancelled the video capture
  92. } else {
  93. // Video capture failed, advise user
  94. }
  95. }
  96. }
  97. class Monitor implements OnClickListener{
  98. @Override
  99. public void onClick(View v) {
  100. switch(v.getId()){
  101. case R.id.sysPhoto:
  102. PhotoIntent();
  103. break;
  104. case R.id.sysVideo:
  105. VideoIntent();
  106. break;
  107. }
  108. }
  109. }
  110. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。