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适合播放长点的。
- SoundPool载入音乐文件使用了独立的线程,不会阻塞UI主线程的操作。但是这里如果音效文件过大没有载入完成,我们调用play方法时可能产生严重的后果,这里Android SDK提供了一个SoundPool.OnLoadCompleteListener类来帮助我们了解媒体文件是否载入完成,我们重载 onLoadComplete(SoundPool soundPool, int sampleId, int status) 方法即可获得。
- 从上面的onLoadComplete方法可以看出该类有很多参数,比如类似id,是的SoundPool在load时可以处理多个媒体一次初始化并放入内存中,这里效率比MediaPlayer高了很多。
- 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()
}
}
}
这就是今天学习的内容了