lhp15575865420
lhp15575865420
采纳率100%
2017-04-16 05:06

Mediaplayer可以单曲播放但不能循环播放,求解

MediaPlayer实现背景音乐播放时,播放一次时没错,循环播放时出错,望大神帮忙解决
public class FirstActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=new Intent(FirstActivity.this,MusicSever.class);
startService(intent);
}

@Override
protected  void onDestroy(){
    Toast.makeText(FirstActivity.this,"活动完", Toast.LENGTH_LONG).show();
    Intent intent=new Intent(FirstActivity.this,MusicSever.class);
    stopService(intent);
    super.onDestroy();
}

}

然后这是server类
public class MusicSever extends Service{
public MediaPlayer me;

@Override
public void onCreate(){
    me=MediaPlayer.create(this,R.raw.yinyue1);//实例化播放器类
    super.onCreate();
    me.setLooping(true);
}

@Override
public int onStartCommand(Intent intent,int flag,int startId){
    me.start();
    me.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            Toast.makeText(MusicSever.this,"在播放", Toast.LENGTH_LONG).show();
            me.start();
        }
    });
    me.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Toast.makeText(MusicSever.this,"出错", Toast.LENGTH_LONG).show();
            me.release();
            return false;
        }
    });
    return START_STICKY;
    // 在运行onStartCommand后service进程被kill后,并且没有新的intent传递给它
}

@Override
public void onDestroy(){
    Toast.makeText(MusicSever.this,"播放完", Toast.LENGTH_LONG).show();
    me.stop();
    me.release();
    super.onDestroy();

}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • lhp15575865420 lhp15575865420 4年前

    换了一个音频文件后可以循环播放了。这又是什么情况,难道还有音频不支持循环播放????

    点赞 评论 复制链接分享

为你推荐