/**
* 音频播放
*/
public class AudioPlayActivity extends BaseActivity {
List<String> audioList = new ArrayList<>();
@BindView(R.id.classifty)
DeaultHeaderLayout classifty;
@BindView(R.id.listview)
ListView listview;
AudioListAdapter audioListAdapter = null;
MediaPlayer mediaPlayer = null;
@BindView(R.id.title1)
TextView title;
@BindView(R.id.a)
Button btn_play;
@BindView(R.id.b)
Button btn_pause;
@BindView(R.id.c)
Button btn_replay;
@BindView(R.id.d)
Button btn_stop;
String audioPath = null;
private String path="http://files.17173.com/ddr/music/gfmusic/heart_song.mp3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_play);
ButterKnife.bind(this);
initAdapter();
// audioList = getIntent().getStringArrayListExtra("audio");
for (int i=0;i<5;i++){
audioList.add(path);
}
if (null != audioList && audioList.size() > 0) {
audioListAdapter.updata(audioList);
}
}
private void initAdapter() {
audioListAdapter = new AudioListAdapter(this);
listview.setAdapter(audioListAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
stop();
audioPath = audioList.get(i);
title.setText("播放地址: " +audioPath);
}
});
}
@OnClick({R.id.a, R.id.b, R.id.c, R.id.d})
public void onViewClicked(View view) {
if (null == audioPath)
return;
switch (view.getId()) {
case R.id.a:
play();
break;
case R.id.b:
pause();
break;
case R.id.c:
replay();
break;
case R.id.d:
stop();
break;
}
}
/**
* 播放音乐
*/
protected void play() {
try {
mediaPlayer = new MediaPlayer();
// 设置指定的流媒体地址
mediaPlayer.setDataSource(audioPath);
// 设置音频流的类型
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
// 通过异步的方式装载媒体资源
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// 装载完毕 开始播放流媒体
mediaPlayer.start();
Toast.makeText(AudioPlayActivity.this, "开始播放", Toast.LENGTH_SHORT).show();
// 避免重复播放,把播放按钮设置为不可用
btn_play.setEnabled(false);
}
});
// 设置循环播放
// mediaPlayer.setLooping(true);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// 在播放完毕被回调
btn_play.setEnabled(true);
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// 如果发生错误,重新播放
replay();
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "播放失败", Toast.LENGTH_SHORT).show();
}
}
/**
* 暂停
*/
protected void pause() {
if (null == mediaPlayer)
return;
if (btn_pause.getText().toString().trim().equals("继续")) {
btn_pause.setText("暂停");
mediaPlayer.start();
Toast.makeText(this, "继续播放", Toast.LENGTH_SHORT).show();
return;
}
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
btn_pause.setText("继续");
Toast.makeText(this, "暂停播放", Toast.LENGTH_SHORT).show();
}
}
/**
* 重新播放
*/
protected void replay() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.seekTo(0);
Toast.makeText(this, "重新播放", Toast.LENGTH_SHORT).show();
btn_pause.setText("暂停");
return;
}
play();
}
/**
* 停止播放
*/
protected void stop() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
btn_play.setEnabled(true);
Toast.makeText(this, "停止播放", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
// 在activity结束的时候回收资源
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
super.onDestroy();
}
}
/**
* XML activity_audio_play
*/
<?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:orientation="vertical"
android:background="@color/color_white"
android:layout_height="match_parent">
<com.example.njmrs.view.DeaultHeaderLayout
android:id="@+id/classifty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
app:backDisplay="true"
app:headerTitle="音频播放">
</com.example.njmrs.view.DeaultHeaderLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@color/color_bb"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:textColor="@color/color_bf"
android:text=""
android:textSize="16sp"
android:gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="70dp">
<Button
android:id="@+id/a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="播放" />
<Button
android:id="@+id/b"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="继续" />
<Button
android:id="@+id/c"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="重播" />
<Button
android:id="@+id/d"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="停止" />
</LinearLayout>
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_marginTop="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>