SoundPool + ViedeoView
new SoundPool(最多播放几首,andio满那个人,Music(播放类型),声音)
id=load(上下文,资源id,优先级)
流id=play(声音id,左声道音量,右声道音量,优先级,循环,播放类型)
play()播放
stop()停止
pause()暂停
VideoView//用于播放视频
设置控制器
setMediaColltollerCnewMediaColl(上下文对象)
setVideoVril(Uri uri)//设置播放源
requestFocus()//播放时获得屏幕焦点
start()启动
Mediaplayer相关
player.prepare()和player.prepareAsync()
两个方法都可以将资源加载到内存中,第一个方法适合用于加载
文件资源或网络资源
ViedeoView代码
Button button1;
Button button2;
Button button3;
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
button1=findViewById(R.id.bofang);
button2=findViewById(R.id.zanting);
button3=findViewById(R.id.tingzhi);
videoView=findViewById(R.id.video);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
videoView.setMediaController(new MediaController(this));
Uri uri = Uri.parse("http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4");
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.bofang:
Log.e("####","bofang");
videoView.start();
break;
case R.id.zanting:
Log.e("####","zanting");
videoView.pause();
break;
case R.id.tingzhi:
videoView.stopPlayback();
videoView.resume();
Log.e("####","tingzhi");
break;
default:
break;
}
}
SoundPool代码
SoundPool soundPool;
Button play;
Button stop;
Button pause;
int load;
int load1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initPool();
play = findViewById(R.id.pool_start);
stop = findViewById(R.id.pool_stop);
pause = findViewById(R.id.pool_pause);
}
private void initPool() {
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
load = soundPool.load(this, R.raw.jiuer, 1);
load1 = soundPool.load(this, R.raw.river, 1);
if (load <= 0) {
Log.e("error", "load error");
finish();
}
}
public void onClickBtn(View view) {
int id = view.getId();
switch (id) {
case R.id.pool_pause:
soundPool.pause(load1);
break;
case R.id.pool_start:
load=soundPool.play(load, 1.0f, 1.0f, 1, 0, 1.0f);
load1=soundPool.play(load1,1.0f, 1.0f, 1, 0, 1.0f);
break;
case R.id.pool_stop:
soundPool.pause(load);
// soundPool.release();
break;
default:
break;
}
}