SoundPool
java布局
// An highlighted block
<?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"
android:orientation="vertical"
tools:context="bw.com.day7.Main7Activity">
<Button
android:id="@+id/startbtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="播放"
/>
<Button
android:id="@+id/pausetbtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="暂停"
/>
<Button
android:id="@+id/stoptbtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="停止"
/>
</LinearLayout>
java代码
// An highlighted block
package bw.com.day7;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import bw.com.day1.R;
public class Main7Activity extends AppCompatActivity implements View.OnClickListener {
private SoundPool soundPool;
private int sound1;
private int sound2;
private Button button1;
private Button button2;
private Button button3;
private int steamId1;
private int steamId2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main7);
initView();//初始化控件
initPool();//加载声音,获得声音的id
}
public void initView(){
button1 = findViewById(R.id.startbtn);
button2 = findViewById(R.id.pausetbtn);
button3 = findViewById(R.id.stoptbtn);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
public void initPool(){
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC,0);
sound1 = soundPool.load(Main7Activity.this, R.raw.jiuer, 1);
if (sound1 <= 0){
Toast.makeText(this, "加载失败", Toast.LENGTH_SHORT).show();
finish();
}
sound2 = soundPool.load(Main7Activity.this, R.raw.river, 1);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.startbtn:
steamId1 = soundPool.play(sound1,1.0f,1.0f,1,0,1.0f);
steamId2 = soundPool.play(sound2,1.0f,1.0f,1,0,1.0f);
break;
case R.id.pausetbtn:
soundPool.stop(steamId1);
soundPool.release();//释放资源
break;
case R.id.stoptbtn:
soundPool.pause(steamId2);
soundPool.release();//释放资源
break;
}
}
}
VideoView
效果图
布局文件
// An highlighted block
<?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"
android:orientation="vertical"
tools:context="bw.com.day7.VideoViewActivity">
<Button
android:id="@+id/vv_startbtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="播放"
/>
<Button
android:id="@+id/vv_pausetbtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="暂停"
/>
<Button
android:id="@+id/vv_stoptbtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="停止"
/>
<VideoView
android:id="@+id/vv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
java代码
// An highlighted block
package bw.com.day7;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
import bw.com.day1.R;
public class VideoViewActivity extends AppCompatActivity implements View.OnClickListener {
private VideoView videoView;
private Button button1;
private Button button2;
private Button button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_view);
initView();//初始化控件
initVideoView();
}
private void initVideoView() {
videoView.setMediaController(new MediaController(this));//获得控制器
Uri uri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
videoView.setVideoURI(uri);//设置播放资源
videoView.requestFocus();//获得焦点
videoView.start();
}
public void initView(){
videoView = findViewById(R.id.vv);
button1 = findViewById(R.id.vv_startbtn);
button2 = findViewById(R.id.vv_pausetbtn);
button3 = findViewById(R.id.vv_stoptbtn);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.vv_startbtn:
videoView.start();
break;
case R.id.vv_pausetbtn:
videoView.stopPlayback();
break;
case R.id.vv_stoptbtn:
videoView.pause();
videoView.resume();
break;
}
}
}