当前位置:   article > 正文

Android开发,使用kotlin学习多媒体功能(详细)_kotlin mediaplayer

kotlin mediaplayer

一、通知

1.用到的类和方法

(1) Context类

getSystemService()接收一个字符串参数用于确定获取系统的哪个服务,这里我们传入Context.NOTIFICATION_SERVICE,获取NotificationManager

(2) NotificationManager类

createNotificationChannel()创建通知渠道
notify()让通知显示出来,第一个参数是id,第二个参数是Notification对象

(3) NotificationChannel类

NotificationChannel()构建通知渠道,第一个参数,渠道ID,第二个参数,渠道名称,第三个参数,通知的重要等级,等级从高到低有IMPORTANCE_HIGH、IMPORTANCE_DEFAULT、IMPORTANCE_LOW、IMPORTANCE_MIN这几种。

(4) NotificationCompat类

NotificationCompat.builder()创建Notification对象,第一个参数是context,第二个参数是渠道ID。

(5) Notification

setContentTitle()指定通知的标题内容
setContentText()指定通知的正文内容
setSmallIcon()设置通知的小图标
setLargeIcon()设置通知的大图标
setWhen()设置创建的时间
setContentIntent()通过PendingIntent构建一个延迟执行的”意图“,参数为PendingIntent对象
setAutoCancel()方法传入true,就表示点击这个通知时,通知会自动取消
setStyle()接收一个NotificationCompat.Style参数,这个参数时用来构建具体的富文本信息的,如长文字、图片等。
  1. .setStyle(NotificationCompat.BigTextStyle().bigText(".........................")
  2. .setStyle(NotificationCompat.BigPictureStyle().bigPicture(BigmapFactory.decodeResource(resources,R.drawable.big_image)))

(6) PendingIntent

getActivity()获得PendingIntent对象,第一个参数是Context,第二个参数用不到,传入0,第三个参数是一个Intent对象,第四个参数是确定PendingIntent的行为,行为有FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT四种可选。

2.创建通知

步骤

(1)创建通知渠道

获取NotificationManager的实例,,由于NotificationManager类和createNotificationChannel()方法都是Android 8.0系统新增的API,因此在使用的时候要进行版本判断,接下来使用NotificationChannel类构建通知渠道,并调用NotificationManager类的createNotificationChannel()方法完成创建。

  1. val manager=getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  2. if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
  3. val channel=NotificationChannel("normal","Normal",NotificationManager.IMPORTANCE_DEFAULT)
  4. manager.createNotificationChannel(channel)
  5. }

(2)设置通知点击事件,构建Notification对象并显示通知。

  1. val sendNotion:Button=findViewById(R.id.sendNotion)
  2. sendNotion.setOnClickListener {
  3. val intent= Intent(this,otherActivity::class.java)
  4. val pi=PendingIntent.getActivity(this,0,intent,0)
  5. val notification= NotificationCompat.Builder(this,"normal")
  6. .setContentTitle("This is content title")
  7. .setContentText("This is content text")
  8. .setSmallIcon(R.drawable.small)
  9. .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.big))
  10. .setWhen(System.currentTimeMillis())
  11. .setContentIntent(pi)
  12. .setAutoCancel(true)
  13. .build()
  14. manager.notify(1,notification)
  15. }

二、调用摄像头和相册

1.用到的方法

 Uri.fromFile()将File对象转换成Uri对象,参数为File对象
FileProvider.getUriForFile()将File对象转换成一个封装过的Uri对象,第一个参数是    Context   对象,第二个参数是任意唯一的字符串,第三个参数是File对象。
BitmapFactory.decodeStream()将输入流传入,把图片加载成Bitmap
imageView.setImageBitmap():显示图片
File类常用方法
File(pathname:String)通过路径名创建一个新的File实例
File(parent:File,child:String)从父抽象路径和子路径名字符串创建新的File实例
File(parent:String,child:String)从父路径名字符串和子路径名字符串创建新的File实例
exists()路径是否存在
delete()删除文件或者路径
createNewFile()创建新文件
getPath()得到File的路径

2.步骤

(1)在布局文件中,添加Button和ImageView

  1. <Button
  2. android:id="@+id/takePhotoBtn"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:text="take Photo"/>
  6. <ImageView
  7. android:id="@+id/imageView"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_horizontal"/>

(2)设置变量

  1. private val takePhoto=1
  2. lateinit var imageUri: Uri
  3. lateinit var outputImage:File

(3)创建File对象

将图片命名为output_image.jpg并存放在手机SD卡的应用关联缓存目录下

  1. outputImage = File(externalCacheDir, "output_image.jpg")
  2. if (outputImage.exists()) {//如果路径存在,就先删除
  3. outputImage.delete()
  4. }
  5. outputImage.createNewFile()//创建一个新的File

(4)把File转换成Uri

从Android7.0系统开始,直接使用本地路径的Uri被认为是不安全,会抛出一个FileUriExposedException异常,而FileProvide则是一种特殊的ContentProvider,它使用了和ContentProvider类似的机制来对数据进行保护,可以选择性地将封装过的Uri共享给外部,从而提高了应用的安全性。

  1. imageUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  2. FileProvider.getUriForFile(this, "com.example.cameraman's.fileprovider", outputImage);
  3. } else {
  4. Uri.fromFile(outputImage);
  5. }

(5)启动相机程序

  1. // 启动相机程序
  2. val intent= Intent("android.media.action.IMAGE_CAPTURE")
  3. intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri)
  4. requestDataLauncher.launch(intent)

(6)在onActivityResult()函数中,在相机程序拍到的图片显示出来

  1. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  2. super.onActivityResult(requestCode, resultCode, data)
  3. val imageView: ImageView = findViewById(R.id.imageView)
  4. when (requestCode) {
  5. takePhoto -> {
  6. if (resultCode == RESULT_OK) {
  7. // 将拍摄的照片显示出来
  8. val bitmap =
  9. BitmapFactory.decodeStream(contentResolver.openInputStream(imageUri))
  10. imageView.setImageBitmap(rotateIfRequired(bitmap))
  11. }
  12. }
  13. }
  14. }

(7)解决调用相机程序去拍照有可能会在一些手机上发生照片旋转的情况

  1. private fun rotateIfRequired(bitmap: Bitmap): Bitmap {
  2. val exif = ExifInterface(outputImage.path)
  3. val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
  4. return when (orientation) {
  5. ExifInterface.ORIENTATION_ROTATE_90 -> rotateBitmap(bitmap, 90)
  6. ExifInterface.ORIENTATION_ROTATE_180 -> rotateBitmap(bitmap, 180)
  7. ExifInterface.ORIENTATION_ROTATE_270 -> rotateBitmap(bitmap, 270)
  8. else -> bitmap
  9. }
  10. }
  11. private fun rotateBitmap(bitmap: Bitmap, degree: Int): Bitmap {
  12. val matrix = Matrix()
  13. matrix.postRotate(degree.toFloat())
  14. val rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
  15. bitmap.recycle()
  16. return rotatedBitmap
  17. }

(8)在AndroidManifeest.xml对ContentProvider进行注册

android:authorities属性的值必须和刚才FileProvider.getUriForFile()方法中的第二个参数一致,另外,这里还在<provider>标签的内部使用<meta-data>指定Uri的共享路径,并引入一个@xml/file_paths资源。

  1. <provider
  2. android:name="androidx.core.content.FileProvider"
  3. android:authorities="com.example.cameraman's.fileprovider"
  4. android:exported="false"
  5. android:grantUriPermissions="true">
  6. <meta-data
  7. android:name="android.support.FILE_PROVIDER_PATHS"
  8. android:resource="@xml/file_paths" />
  9. </provider>

(9)右击res目录->New->Directory,创建一个xml目录,接着右击xml目录->New->File,创建一个file_paths.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <paths xmlns:android="http://schemas.android.com/apk/res/android">
  3. <external-path name="my_images" path="/" />
  4. </paths>

三、从相册中选择图片

步骤

(1)在布局文件中加入Button:fromAlbumBtn

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

(2)初始化fromAlbum

val fromAlbum = 2

(3)在按键fromAlbumBtn的点击事件中,打开文件选择器,显示图片

  1. val fromAlbumBtn: Button = findViewById(R.id.fromAlbumBtn)
  2. fromAlbumBtn.setOnClickListener {
  3. val intent1=Intent(Intent.ACTION_OPEN_DOCUMENT)
  4. intent1.addCategory(Intent.CATEGORY_OPENABLE)
  5. intent1.type="image/*"
  6. requestDataLauncher1.launch(intent1)
  7. }

(4)在onActivityResult()函数中,将选择的图片显示出来

  1. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  2. super.onActivityResult(requestCode, resultCode, data)
  3. val imageView: ImageView = findViewById(R.id.imageView)
  4. when (requestCode) {
  5. ...
  6. fromAlbum -> {
  7. if (resultCode == RESULT_OK && data != null) {
  8. data.data?.let { uri ->
  9. //将选择的图片显示
  10. val bitmap = getBitmapFromUri(uri)
  11. imageView.setImageBitmap(bitmap)
  12. }
  13. }
  14. }
  15. }
  16. }

(5)将Uri转换成Bitmap

  1. private fun getBitmapFromUri(uri: Uri) =
  2. contentResolver.openFileDescriptor(uri, "r")?.use{
  3. BitmapFactory.decodeFileDescriptor(it.fileDescriptor)
  4. }

四、播放多媒体文件

1、播放音频

 步骤

(1)创建一个MediaPlayer的实例

private val mediaPlayer=MediaPlayer()

(2)为MediaPlayer对象进行初始化操作

     通过getAssets()方法得到一个AssetManager的实例,AssetManager可用于读取assets目录下的任何资源,接着调用openFd()方法将音频文件句柄打开,后来调用setDataSource()方法设置要播放的音频文件的位置和prepare()方法完成准备工作。

  1. private fun initMediaPlayer() {
  2. val assetManager=assets
  3. val fd=assetManager.openFd("music.mp3")
  4. mediaPlayer.setDataSource(fd.fileDescriptor,fd.startOffset,fd.length)
  5. mediaPlayer.prepare()
  6. }

(3)设置三个按键的点击事件

  1. val play:Button=findViewById(R.id.play)
  2. val pause:Button=findViewById(R.id.pause)
  3. val stop:Button=findViewById(R.id.stop)
  4. play.setOnClickListener {
  5. if(!mediaPlayer.isPlaying)
  6. mediaPlayer.start()
  7. }
  8. pause.setOnClickListener {
  9. if(mediaPlayer.isPlaying){
  10. mediaPlayer.pause()
  11. }
  12. }
  13. stop.setOnClickListener {
  14. if(mediaPlayer.isPlaying){
  15. mediaPlayer.reset()
  16. initMediaPlayer()
  17. }
  18. }

(4)最后在onDestroy()方法中,我们需要调用stop()方法和release()方法将MediaPlayer相关的资源释放掉。

  1. override fun onDestroy() {
  2. super.onDestroy()
  3. mediaPlayer.stop()
  4. mediaPlayer.release()
  5. }


2、播放视频

 步骤

(1)在布局文件中放置三个按钮,分别控制视频的播放,暂停,和重新播放,在按钮下面放置视频VideoView。

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal">
  5. <Button
  6. android:id="@+id/play"
  7. android:layout_width="0dp"
  8. android:layout_height="wrap_content"
  9. android:layout_weight="1"
  10. android:text="play"/>
  11. <Button
  12. android:id="@+id/pause"
  13. android:layout_width="0dp"
  14. android:layout_height="wrap_content"
  15. android:layout_weight="1"
  16. android:text="pause"/>
  17. <Button
  18. android:id="@+id/replay"
  19. android:layout_width="0dp"
  20. android:layout_height="wrap_content"
  21. android:layout_weight="1"
  22. android:text="replay"/>
  23. </LinearLayout>
  24. <VideoView
  25. android:id="@+id/videoView"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:text="videoView"/>

(2)点击res->New->Directory,然后输入raw,把视频资源放在这。

(3)初始化VideoView

  1. val uri=Uri.parse("android.resource://$packageName/${R.raw.video}")
  2. videoView.setVideoURI(uri)

(4)设置三个按键的点击事件

  1. play.setOnClickListener {
  2. if(!videoView.isPlaying)
  3. videoView.start()
  4. }
  5. pause.setOnClickListener {
  6. if(videoView.isPlaying){
  7. videoView.pause()
  8. }
  9. }
  10. replay.setOnClickListener {
  11. if(videoView.isPlaying){
  12. videoView.resume()
  13. }
  14. }

(5)在onDestroy()方法中,需要调用suspend()方法,将VideoView所占用的资源释放掉。

  1. override fun onDestroy() {
  2. super.onDestroy()
  3. videoView.suspend()
  4. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/747591
推荐阅读
相关标签
  

闽ICP备14008679号