Android学习随笔(14)------手机多媒体

一时失言乱红尘 2022-06-03 00:30 315阅读 0赞

学习流程来自《第一行代码》(第二版)

通知

创建通知的步骤

  1. 需要一个NotificationManager来对通知进行管理,可调用Context的getSystemService(Context.NOTIFICATION_SERVICE)方法
  2. 需要Builder构造器来创建Notification对象,利用NotificationCompat类(support-v4)的构造器来创建Notification对象
  3. 在Builder之后利用 .setXxxx()来设置通知,最后 .build();
  4. 利用NotifcationManager的notify()方法让通知显示出来,manager.notify(1, notification); 第一个参数是id,为每个通知独有的标记(如果想要同一个App显示多条通知,id需不同,若相同则只会显示最近发送的那条通知)

通知的基本用法

布局文件activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
  2. <Button android:id="@+id/send_notice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Send Notice" />
  3. </LinearLayout>

显示一个按钮,点击按钮即发送一条通知

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. Button sendNotice = (Button) findViewById(R.id.send_notice);
  6. sendNotice.setOnClickListener(new View.OnClickListener() {
  7. @Override
  8. public void onClick(View view) {
  9. NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //用于确定获取系统哪个服务 通知服务
  10. Notification notification = new NotificationCompat.Builder(MainActivity.this)
  11. .setContentTitle("This is content title")
  12. .setContentText("This is content text")
  13. .setWhen(System.currentTimeMillis())
  14. .setSmallIcon(R.mipmap.ic_launcher)
  15. .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
  16. .build(); // Builder建立通知结构 build建立
  17. manager.notify(1, notification);
  18. }
  19. });
  20. }

Exler

但是这个通知是无法响应点击事件的。这里就需要为通知设置一个PendingIntent。

新建一个空活动NotificationActivity,利用它来显示点击通知后跳转显示的信息

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
  2. <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="24sp" android:text="This is notification layout" />
  3. </RelativeLayout>
  4. @Override
  5. public void onClick(View view) {
  6. Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
  7. PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
  8. NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //用于确定获取系统哪个服务 通知服务
  9. Notification notification = new NotificationCompat.Builder(MainActivity.this) // Builder建立通知结构 build建立
  10. .setContentTitle("This is content title")
  11. .setContentText("This is content text")
  12. .setWhen(System.currentTimeMillis())
  13. .setSmallIcon(R.mipmap.ic_launcher)
  14. .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
  15. .setContentIntent(pi)
  16. .setAutoCancel(true) // 设置自动取消
  17. .build();
  18. manager.notify(1, notification);
  19. }

取消通知的第二种方法是在NotificationActivity中

  1. NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.cancel(1); // 取消该通知的id

扩展

  1. // .setVibrate() 设置手机震动 静止时长,振动时长...需要申请权限 <uses-permission android:name="android.permission.VIBRATE" />
  2. // .setSound(Uri.fromFile(new File("/storage/emulated/0/cloudmusic/Music/dgpy.mp3"))) 设置播放声音的文件位置
  3. // .setLights(Color.GREEN, 1000, 1000) 设置呼吸灯
  4. // .setDefaults(NotificationCompat.DEFAULT_ALL) 使用默认效果
  5. // .setStyle(new NotificationCompat.BigTextStyle().bigText("Learn how to build notifications, send and sync data, and" +
  6. //"use voice actions. Get the official Android IDE and developer tools to build apps for Android.")) 用于显示长文本
  7. //.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round)))在通知处显示一张图片
  8. //.setPriority(NotificationCompat.PRIORITY_MAX) 设置通知优先级

调用摄像头和相册

调用摄像头

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
  2. <Button android:id="@+id/take_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Take Photo"/>
  3. <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/>
  4. </LinearLayout>

点击按钮打开摄像头,拍照在ImageView中显示。

MainActivity.java :

  1. public class MainActivity extends AppCompatActivity {
  2. public static final int TAKE_PHOTO = 1;
  3. private ImageView picture;
  4. private Uri imageUri;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. Button takePhoto = (Button) findViewById(R.id.take_photo);
  10. picture = (ImageView) findViewById(R.id.picture);
  11. takePhoto.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View v) {
  14. File outputImage = new File(getExternalCacheDir(), "output_image.jpg"); // 创建File对象,用于储存拍照后的图片 存放在SD卡的应用关联缓存数据的位置/sdcard/Android/data/<package name>/cache
  15. try { // 创建outputImage.jpg 如果存在删除重新创建
  16. if (outputImage.exists()) {
  17. outputImage.delete();
  18. }
  19. outputImage.createNewFile();
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. if (Build.VERSION.SDK_INT >= 24) { // 调用FileProvider的getUriForFile()方法 将File对象转换成封装过的Uri对象
  24. imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.administrator.cameraalbumtest.fileprovider", outputImage); // Android7.0开始直接使用本地Uri被认为是不安全的(FileUriExposedException),FileProvider是一种特殊的内容提供器选择性的将封装过的Uri共享给外部。(Context context,String authority,File file)
  25. } else { // 如果运行设备版本低于7.0 将File对象转换成Uri对象(标识着output_image.jpg图片的本地真实路径)
  26. imageUri = Uri.fromFile(outputImage);
  27. }
  28. Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); //调用相机
  29. intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); // 指定图片输出地址
  30. startActivityForResult(intent, TAKE_PHOTO);
  31. }
  32. });
  33. }
  34. @Override
  35. protected void onActivityResult(int requestCode, int resultCode, Intent data) { // 拍照完后有结果返回
  36. switch (requestCode) {
  37. case TAKE_PHOTO:
  38. if (resultCode == RESULT_OK) {
  39. try { // 显示照片
  40. Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); // 根据返回
  41. picture.setImageBitmap(bitmap);
  42. } catch (FileNotFoundException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. break;
  47. default:
  48. break;
  49. }
  50. }
  51. }

因为FileProvider是一个内容提供器所以需要对其进行注册

  1. <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.example.administrator.cameraalbumtest.fileprovider" android:exported="false" android:grantUriPermissions="true">
  2. <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
  3. </provider> <!--exported:要求必须为false,为true,grantUriPermissions:true,表示授予 URI 临时访问权限-->

android:name 属性的值是固定的
android:authorities 属性的值必须要和FileProvider.getUriFile()方法中的第二个参数一样(String authority)
在provider标签内部使用meta-data来指定Uri的共享路径,引用了一个@xml/file_paths资源
需要对这个资源进行创建。

在res目录下新建一个xml文件夹,添加一个file_paths.xml文件

  1. <paths xmlns:android="http://schemas.android.com/apk/res/android">
  2. <external-path name="my_images" path="" />
  3. </paths>

files-path代表的根目录: Context.getFilesDir()
external-path代表的根目录: Environment.getExternalStorageDirectory()
cache-path代表的根目录: getCacheDir()
name可以随便填写
path表示共享的具体路径,path=”“,它代表根目录,也就是说你可以向其它的应用共享根目录及其子目录下任何一个文件了。如果你path设为path=”pictures”,那么它代表着根目录下的pictures目录(/storage/emulated/0/pictures),如果你向其它应用分享pictures目录范围之外的文件是不行的。
4.4系统之前访问SD卡的应用关联目录是要声明权限的

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

Exler

Exler

Exler
因为是虚拟机运行的所以没有拍照效果。

从相册中选取图片

  1. <Button
  2. android:id="@+id/choose_from_album"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="Choose From Album" />

添加一个按钮,点击打开相册,选择图片然后显示在我们的应用上。

  1. public class MainActivity extends AppCompatActivity {
  2. public static final int CHOOSE_PHOTO = 2;
  3. private ImageView picture;
  4. private Uri imageUri;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. Button chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);
  10. picture = (ImageView) findViewById(R.id.picture);
  11. chooseFromAlbum.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View v) {
  14. if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
  15. ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
  16. } else {
  17. openAlbum();
  18. }
  19. }
  20. });
  21. }
  22. private void openAlbum() {
  23. Intent intent = new Intent("android.intent.action.GET_CONTENT");
  24. intent.setType("image/*"); // 选择图片
  25. startActivityForResult(intent, CHOOSE_PHOTO); // 打开相册
  26. }
  27. @Override
  28. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  29. switch (requestCode) {
  30. case 1:
  31. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  32. openAlbum();
  33. } else {
  34. Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
  35. }
  36. break;
  37. default:
  38. }
  39. }
  40. @Override
  41. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  42. switch (requestCode) {
  43. case CHOOSE_PHOTO:
  44. Log.d("admin", "CHOOSE_PHOTO"+requestCode);
  45. if (resultCode == RESULT_OK) { // 判断手机系统版本
  46. if (Build.VERSION.SDK_INT >= 19) {
  47. handleImageOnKitKat(data);
  48. } else {
  49. handleImageBeforeKitKat(data);
  50. }
  51. }
  52. break;
  53. default:
  54. break;
  55. }
  56. }
  57. @TargetApi(19)
  58. private void handleImageOnKitKat(Intent data) {
  59. String imagePath = null;
  60. Uri uri = data.getData();
  61. if (DocumentsContract.isDocumentUri(this, uri)) { // 如果是document类型的Uri,则通过document id处理
  62. String docId = DocumentsContract.getDocumentId(uri);
  63. if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
  64. String id = docId.split(":")[1]; // 解析出数字格式的id
  65. String selection = MediaStore.Images.Media._ID + "=" + id;
  66. imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
  67. Log.d("admin", "documents" + imagePath);
  68. } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
  69. Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
  70. imagePath = getImagePath(contentUri, null);
  71. Log.d("admin", "downloads" + imagePath);
  72. }
  73. } else if ("content".equalsIgnoreCase(uri.getScheme())) { // 如果是content类型的Uri,用普通方式处理
  74. imagePath = getImagePath(uri, null);
  75. Log.d("admin", "content" + imagePath);
  76. } else if ("file".equalsIgnoreCase(uri.getScheme())) { // 如果是file类型的Uri,直接获取图片路径即可
  77. imagePath = uri.getPath();
  78. Log.d("admin", "file1" + imagePath);
  79. }
  80. displayImage(imagePath);
  81. Log.d("admin", imagePath);
  82. }
  83. private void handleImageBeforeKitKat(Intent data) {
  84. Uri uri = data.getData();
  85. String imagePath = getImagePath(uri, null);
  86. displayImage(imagePath);
  87. }
  88. private String getImagePath(Uri uri, String selection) {
  89. String path = null;
  90. Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
  91. if (cursor != null) {
  92. if (cursor.moveToFirst()) {
  93. path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
  94. }
  95. cursor.close();
  96. }
  97. return path;
  98. }
  99. private void displayImage(String imagePath) {
  100. if (imagePath != null) {
  101. Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
  102. picture.setImageBitmap(bitmap);
  103. } else {
  104. Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show();
  105. }
  106. }
  107. }

因为是在相册中选取,所以先添加了一个运行时权限的申请(读写SD卡的权限),获得权限后会去调用openAlbum()方法,利用Intent来打开相册。
从android4.4开始,选取相册中的图片不再返回真实的Uri,而是一个经过封装的Uri,所以要对其进行解析。

Exler

Exler

Exler

播放多媒体文件

播放音频

在Android中播放音频文件一般都是使用MediaPlayer类来实现的。
具体步骤 :

  1. 创建MediaPlayer对象
  2. 调用setDataSource()方法来设置音频文件的目录
  3. 再调用prepare()方法使MediaPlayer进入到准备状态
  4. 调用start()方法播放,pause()方法暂停,reset()方法停止(再次播放会从头开始)



需要从SD卡中读取音频文件

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

播放视频

播放视频有两种实现方法,一种是利用VideoView来实现,另一种是利用MediaPlayer和SurfaceView来实现

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. private VideoView videoView;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. videoView = (VideoView) findViewById(R.id.video_view);
  8. Button play = (Button) findViewById(R.id.play);
  9. Button pause = (Button) findViewById(R.id.pause);
  10. Button replay = (Button) findViewById(R.id.replay);
  11. play.setOnClickListener(this);
  12. pause.setOnClickListener(this);
  13. replay.setOnClickListener(this);
  14. if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
  15. ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
  16. } else {
  17. initVideoPlayer();
  18. }
  19. }
  20. private void initVideoPlayer() {
  21. File file = new File(Environment.getExternalStorageDirectory(), "movie.mp4");
  22. videoView.setVideoPath(file.getPath()); // 指定视频文件的路径
  23. }
  24. @Override
  25. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  26. switch (requestCode) {
  27. case 1:
  28. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  29. initVideoPlayer();
  30. } else {
  31. Toast.makeText(MainActivity.this, "拒绝权限将无法使用程序", Toast.LENGTH_SHORT).show();
  32. finish();
  33. }
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. @Override
  40. public void onClick(View v) {
  41. switch (v.getId()) {
  42. case R.id.play:
  43. if (!videoView.isPlaying()) {
  44. videoView.start(); // 开始播放
  45. }
  46. break;
  47. case R.id.pause:
  48. if (videoView.isPlaying()) {
  49. videoView.pause(); // 暂停播放
  50. }
  51. break;
  52. case R.id.replay:
  53. if (videoView.isPlaying()) {
  54. videoView.resume(); // 重新播放
  55. }
  56. break;
  57. default:
  58. break;
  59. }
  60. }
  61. @Override
  62. protected void onDestroy() {
  63. super.onDestroy();
  64. if (videoView != null) { // 释放资源
  65. videoView.suspend();
  66. }
  67. }
  68. }
  69. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal">
  70. <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center">
  71. <Button android:id="@+id/play" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Play" />
  72. <Button android:id="@+id/pause" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Pause" />
  73. <Button android:id="@+id/replay" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Replay" />
  74. </LinearLayout>
  75. <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="wrap_content" />
  76. </LinearLayout>

需要从SD卡中读取音频文件

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

Exler

Exler

Exler

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. MediaPlayer mediaPlayer;
  3. SurfaceView surfaceView;
  4. Button btnPlay;
  5. String path;
  6. SurfaceHolder sh;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. Button btnPlay = (Button) findViewById(R.id.play);
  12. btnPlay.setOnClickListener(this);
  13. surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
  14. path = "/sdcard/movie.mp4";
  15. mediaPlayer = new MediaPlayer();
  16. }
  17. @Override
  18. public void onClick(View arg0) {
  19. switch (arg0.getId()) {
  20. case R.id.play:
  21. try {
  22. mediaPlayer.reset();
  23. mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  24. mediaPlayer.setDataSource(path);
  25. sh = surfaceView.getHolder(); // 创建表面视图处理接口对象
  26. mediaPlayer.setDisplay(sh);
  27. mediaPlayer.prepare();
  28. mediaPlayer.start();
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. break;
  33. default:
  34. break;
  35. }
  36. }
  37. }
  38. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" >
  39. <SurfaceView android:id="@+id/surfaceView" android:layout_width="200dp" android:layout_height="200dp" />
  40. <Button android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="play" />
  41. >
  42. </LinearLayout>

Exler

Exler


此博文为个人学习笔记,仅供个人学习使用,希望对大家有帮助。

发表评论

表情:
评论列表 (有 0 条评论,315人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android学习随笔(1)

    学习流程来自《第一行代码》(第二版) 最近开始了Android的学习,看到很多人都推荐这一本书,就决定按照这一本书的讲解流程熟悉一下Android。 环境配置 ![