SoundPool
常用方法:
new SoundPool(最大播放数,AudioManager.MUSIC,0)
声音id=load(上下文,资源id)
流id=play(声音id,左声道:1.0f,右声道:1.0f,优先级:1,播放次数:0,播放倍速:1.0f)
stop(流id)
relese():释放资源
pause():暂停
// An highlighted block
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = findViewById(R.id.main_play);
pause = findViewById(R.id.main_pause);
stop= findViewById(R.id.main_stop);
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
//加载声音,获得声音id
initPool();
}
private void initPool() {
pool = new SoundPool(2, AudioManager.STREAM_MUSIC,0);
soundId1 = pool.load(this,R.raw.jiuer,1);
if(soundId1<=0){
Log.e("###########","加载失败");
finish();
}
soundId2 = pool.load(this,R.raw.river,1);
if(soundId2<=0){
Log.e("###########","加载失败");
finish();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.main_play:
streamId1 = pool.play(soundId1,1.0f,1.0f,1,0,1.0f);
streamId2 = pool.play(soundId2,1.0f,1.0f,1,0,1.0f);
break;
case R.id.main_pause:
pool.pause(streamId2);
break;
case R.id.main_stop:
pool.stop(streamId1);
pool.release();
break;
}
}
VideoView
用法:
设置控制器:setMediaController(new MediaController(this))
Uri uri=Uri.pare(网路连接)
setvideoUri(uri)设置播放源
requestFouse();播放时获得焦点
start();
pause();
stoplayback()
resume();
// An highlighted block
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
videoView=findViewById(R.id.main2_video);
play=findViewById(R.id.main2_play);
pause=findViewById(R.id.main2_pause);
stop=findViewById(R.id.main2_stop);
videoView.setMediaController(new MediaController(this));//获得控制器
Uri uri = Uri.parse("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4");
videoView.setVideoURI(uri);//设置播放资源
videoView.requestFocus();//获得焦点
videoView.start();
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.main2_play:
Toast.makeText(Main2Activity.this,"paly",Toast.LENGTH_SHORT).show();
videoView.start();
break;
case R.id.main2_pause:
Toast.makeText(Main2Activity.this,"pause",Toast.LENGTH_SHORT).show();
videoView.pause();
break;
case R.id.main2_stop:
Toast.makeText(Main2Activity.this,"stop",Toast.LENGTH_SHORT).show();
videoView.stopPlayback();
videoView.resume();
break;
}
}