在项目中,有时候遇到要求,点击播放下一曲无法播放的问题。明明调用了start()方法了,为什么还是不行呢。
以下是我的代码
mediaPlayer=new MediaPlayer(); mediaPlayer.setDataSource(url[0]); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.prepareAsync(); mediaPlayer.start();
相信大家都懂这段代码了吧,mediaPlayer.prepareAsync();是为了异步加载网络数据,如果不这样可能app直接卡死掉。如果上面有报错记得try、catch。
后面想到了异步,那么可能是不及时的,比如说mediaPlayer.prepareAsync();刚开始是没任何东西的,可能需要延迟个几秒或毫秒才会得以加载网络的数据。那这个时候再执行mediaPlayer.start();肯定是没有播放的。后面想到了休眠,没错就是这么简单,将代码改成了
mediaPlayer=new MediaPlayer(); mediaPlayer.setDataSource(url[0]); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.prepareAsync(); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(2000); mediaPlayer.start(); }catch (Exception e){} } }).start();
问题得以解决。
贴上全部代码,app界面如图:
播放java文件
package com.wt.authenticwineunion.page.buys.activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.free.statuslayout.manager.StatusLayoutManager;
import com.wt.authenticwineunion.R;
import com.wt.authenticwineunion.base.BaseActivity;
import com.wt.authenticwineunion.base.BasePresenter;
import com.wt.authenticwineunion.util.ToastUtil;
import com.wt.authenticwineunion.widget.TitleView;
import butterknife.BindView;
import butterknife.OnClick;
/**
* 将音频以数组的格式返回过来,然后将他们进行下一首
* */
public class PlayAudio2Activity extends BaseActivity {
@BindView(R.id.title_view)
TitleView titleView;
@BindView(R.id.user_img)
ImageView userImg;
@BindView(R.id.title)
TextView title;
@BindView(R.id.title2)
TextView title2;
@BindView(R.id.new_time)
TextView newTime;
@BindView(R.id.all_time)
TextView allTime;
@BindView(R.id.progress)
ProgressBar progress;
@BindView(R.id.tui)
ImageView tui;
@BindView(R.id.last)
ImageView last;
@BindView(R.id.play)
ImageView play;
@BindView(R.id.next)
ImageView next;
@BindView(R.id.jin)
ImageView jin;
@BindView(R.id.content)
TextView content;
@BindView(R.id.toComment)
TextView toComment;
@BindView(R.id.number1)
TextView number1;
@BindView(R.id.pinlun)
LinearLayout pinlun;
@BindView(R.id.number2)
TextView number2;
@BindView(R.id.like)
LinearLayout like;
@BindView(R.id.fenxiang)
LinearLayout fenxiang;
private MediaPlayer mediaPlayer;
private int w=0;
private String url[]={"http://sc1.111ttt.cn:8282/2018/1/03m/13/396131232171.m4a?tflag=1546606800&pin=97bb2268ae26c20fe093fd5b0f04be80#.mp3",
"http://sc1.111ttt.cn:8282/2018/1/03m/13/396131226156.m4a?tflag=1546606800&pin=97bb2268ae26c20fe093fd5b0f04be80#.mp3",
"http://sc1.111ttt.cn:8282/2017/1/05m/09/298092035545.m4a?tflag=1546606800&pin=97bb2268ae26c20fe093fd5b0f04be80#.mp3"};
@Override
protected void initStatusLayout() {
statusLayoutManager = StatusLayoutManager.newBuilder(this)
.contentView(R.layout.activity_play_audio2)
.loadingView(R.layout.loading_layout)
.build();
statusLayoutManager.showContent();
}
@Override
public void initView(Bundle bundle) {
try {
mediaPlayer=new MediaPlayer();
mediaPlayer.setDataSource(url[0]);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
mediaPlayer.start();
}catch (Exception e){}
}
}).start();
// mediaPlayer.reset();
}catch (Exception e){
}
}
@Override
public BasePresenter initPresenter() {
return null;
}
@OnClick({R.id.tui, R.id.last, R.id.play, R.id.next, R.id.jin})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.tui:
break;
case R.id.last:
//-2是因为算了本省正在播放的歌曲
if (w<=url.length-1){
if (w==0){
ToastUtil.showToast("已经是第一首了");
}else{
w--;
Log.d("TGA", "onViewClicked:返回上一首 "+w);
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(url[w]);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
}catch (Exception E){}
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
mediaPlayer.start();
}catch (Exception e){}
}
}).start();
} }else{
ToastUtil.showToast("已经是第一首了");
}
break;
case R.id.play:
if (!mediaPlayer.isPlaying()){
mediaPlayer.start();
}else{
mediaPlayer.pause();
}
break;
case R.id.next:
if (w<url.length-1){
w++;
Log.d("TGA", "onViewClicked:下一首 "+w);
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(url[w]);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
}catch (Exception E){}
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
mediaPlayer.start();
}catch (Exception e){}
}
}).start();
}else{
ToastUtil.showToast("已经是最后一首了"+w);
}
break;
case R.id.jin:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
xml 布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<com.wt.authenticwineunion.widget.TitleView
android:id="@+id/title_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:padding="30dp"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:src="@drawable/bg3"/>
<ImageView
android:id="@+id/user_img"
android:padding="70dp"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:src="@drawable/img4"/>
</FrameLayout>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:gravity="center"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/chengse"
android:text="如何鉴别这个酒好不好喝?"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/title2"
android:layout_width="match_parent"
android:gravity="center"
android:textColor="@color/text_color1"
android:text="如何·清华大学教授"
android:layout_height="wrap_content" />
<RelativeLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/new_time"
android:textColor="@color/text_color1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"/>
<TextView
android:id="@+id/all_time"
android:textColor="@color/text_color1"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:00"/>
</RelativeLayout>
<ProgressBar
android:id="@+id/progress"
android:paddingLeft="10dp"
android:paddingRight="10dp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/tui"
android:padding="25dp"
android:layout_marginTop="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:src="@drawable/box19"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/last"
android:padding="25dp"
android:layout_marginTop="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:src="@drawable/box20"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/play"
android:layout_width="0dp"
android:layout_weight="1"
android:src="@drawable/box18"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/next"
android:padding="25dp"
android:layout_marginTop="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:src="@drawable/box21"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/jin"
android:padding="25dp"
android:layout_marginTop="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:src="@drawable/box22"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="15dp"
android:background="@color/content_light_black"/>
<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:paddingTop="7dp"
android:paddingRight="10dp"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:src="@drawable/box7"/>
<TextView
android:paddingTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文章内容"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="@color/title_black"/>
</LinearLayout>
<TextView
android:padding="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="简介"
android:textSize="16sp"
android:textColor="@color/title_black"/>
<TextView
android:id="@+id/content"
android:layout_marginBottom="70dp"
android:padding="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="简介简介简介简介简介简介简介简介简介简介简介简介简介简介简介"
android:textColor="@color/title_black"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/content_light_black"/>
<LinearLayout
android:padding="5dp"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginTop="5dp"
android:id="@+id/toComment"
android:paddingTop="10dp"
android:paddingRight="30dp"
android:paddingLeft="30dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="40dp"
android:textSize="12sp"
android:background="@drawable/banyuan4"
android:hint="请填写你想评论的内容"/>
<LinearLayout
android:id="@+id/pinlun"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:orientation="vertical">
<ImageView
android:layout_marginLeft="5dp"
android:layout_marginTop="1dp"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/box24"/>
<TextView
android:id="@+id/number1"
android:textSize="10dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/like"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:orientation="vertical">
<ImageView
android:layout_width="24dp"
android:layout_height="20dp"
android:src="@drawable/box25"/>
<TextView
android:id="@+id/number2"
android:textSize="10dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="111"/>
</LinearLayout>
<LinearLayout
android:visibility="gone"
android:id="@+id/fenxiang"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:orientation="vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="20dp"
android:src="@drawable/box26"/>
<TextView
android:paddingTop="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="请朋友读"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
希望对大家,有帮助。