VideoView和SoundPool

本文详细介绍了Android平台上的音视频播放技术,包括VideoView和SoundPool的使用方法。VideoView适用于播放视频,通过设置媒体控制器、URI和焦点,实现视频播放控制。SoundPool则专为播放短音频设计,适合游戏等场景,支持多音效同时播放,通过加载本地资源并利用独立线程播放,提高响应速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VideoView和SoundPool

VideoView

特别简单粗暴,就是一个系统自带的可以放片的控件,它粗暴那我也粗暴一下了

myvideo.setMediaController(MediaController1(this))//获得控制器
    myvideo.setVideoURI(Uri.parse("http://vfx.mtime.cn/Video/2019/03/19/mp4/190319125415785691.mp4"))
    myvideo.requestFocus();//获得焦点
    myvideo.start()}

同时我在布局文件中写了三个按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity"
    android:orientation="vertical">
    <Button
        android:id="@+id/vv_play"
        android:text="播放"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickvv"/>
    <Button
        android:id="@+id/vv_pause"
        android:text="暂停"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickvv"/>
    <Button
        android:id="@+id/vv_stop"
        android:text="停止"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickvv"/>
    <VideoView
        android:id="@+id/myvideo"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

功能什么 的见文知意

fun onClickvv(view: View){
        when(view.id){
            R.id.vv_play-> myvideo.start()
            R.id.vv_stop-> {
                myvideo.stopPlayback()
                myvideo.resume()
            }
            R.id.vv_pause->myvideo.pause()
        }

   }

顺带给上一个方法,这便是全使用过程

然后我们来说一下一个由系统带的可以存放并播放短音频的类

叫SoundPool

之前只知道android中可以用mediaplayer播放音乐,原来今天才发现
可以用soundpool,用soundpool可以播一些短的反应速度要求高的声音,
比如游戏中的爆破声,而mediaplayer适合播放长点的。

  1. SoundPool载入音乐文件使用了独立的线程,不会阻塞UI主线程的操作。但是这里如果音效文件过大没有载入完成,我们调用play方法时可能产生严重的后果,这里Android SDK提供了一个SoundPool.OnLoadCompleteListener类来帮助我们了解媒体文件是否载入完成,我们重载 onLoadComplete(SoundPool soundPool, int sampleId, int status) 方法即可获得。
  2. 从上面的onLoadComplete方法可以看出该类有很多参数,比如类似id,是的SoundPool在load时可以处理多个媒体一次初始化并放入内存中,这里效率比MediaPlayer高了很多。
  3. SoundPool类支持同时播放多个音效,这对于游戏来说是十分必要的,而MediaPlayer类是同步执行的只能一个文件一个文件的播放。
    然后直接看应用吧,理论不如实战
    首先需要在res文件下建一个raw文件夹,存放一些音乐,如图
    在这里插入图片描述
    然后开始在活动页写代码
 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        pool = SoundPool(2,AudioManager.STREAM_MUSIC,0)

        load1 = pool?.load(this, R.raw.jiuer, 1)
        if(load1!!<=0){
            println("###加载失败")
            finish()
        }
         load2 = pool?.load(this, R.raw.river, 1)
        if(load2!!<=0){
            println("###加载失败")
            finish()
        }
    }

同时我们需要一些可以操纵播放的暂停之类的按钮,先布局后方法

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <Button
        android:id="@+id/play"
        android:text="播放"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickbtn"/>
    <Button
        android:id="@+id/pause"
        android:text="暂停"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickbtn"/>
    <Button
        android:id="@+id/stop"
        android:text="停止"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickbtn"/>
</LinearLayout>

方法

fun onClickbtn(view:View){
        when(view.id){
            R.id.play->{
                stream1 = pool?.play(this.load1!!, 1.0f, 1.0f, 1, 0, 1.0f);
                stream2 = pool?.play(this.load2!!, 1.0f, 1.0f, 1, 0, 1.0f);

            }
            R.id.pause->{
                startActivity(Intent(this,Main2Activity::class.java))
            }
            R.id.stop->{
                pool?.stop(stream1!!)
                pool?.release()
            }

        }
    }

这就是今天学习的内容了

INTERESTING!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值