我想播放.mp3重复说5-10次,因为一旦选择了该项,一首歌应该重复播放5次。
是否有可能无时间播放歌曲,并且在应用程序启动之前不会以编程方式定义任何时间。请任何帮助。
public class BackgroundAudioService extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {
MediaPlayer mp;
SoundPool loopChant;
int itempositionno;
int soundID, i;
Uri path;
Intent playbackServiceIntent;
BackgroundAudioService instanceService;
public BackgroundAudioService() {
Log.d(this.getClass().getName(), "Background method called");
}
@Override
public void onCreate() {
Log.d(this.getClass().getName(), "onCreate method called");
instanceService = this;
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
Log.d(this.getClass().getName(), "onBind method called");
int itempositionno = intent.getIntExtra("Itempositionno", 0);
this.itempositionno = itempositionno;
if (mp == null ){
mp = new MediaPlayer();}
setSongAtPosition(itempositionno);
return new LocalBinder();
}
public void setSongAtPosition(int position){
Log.d(this.getClass().getName(), "setSongAtPosition method called");
itempositionno = position;
Uri path = null;
switch (itempositionno){
case 0:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.shiva);
break;
}
case 1:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.perumal);
break;
}
case 2:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.ganesh);
break;
}
case 3:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.muruga);
break;
}
case 4:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.iyyappa);
break;
}
case 5:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.hanuman);
break;
}
case 6:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.gayathri);
break;
}
case 7:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.aigiri);
break;
}
case 8:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.saibaba);
break;
}
case 9:{
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.om);
break;
}
}
if(mp.isPlaying()) {
mp.release();
}
mp = MediaPlayer.create(BackgroundAudioService.this, path);
mp.setOnPreparedListener(BackgroundAudioService.this);
if (itempositionno == 1){
setLoopChant();
}
}
@Override
public void onPrepared(MediaPlayer mp) {
Log.d(this.getClass().getName(), "onPrepared method called");
mp.start();
}
@Override
public void onCompletion(MediaPlayer mp) {
Log.d(this.getClass().getName(), "onCompletion method called");
}
class LocalBinder extends Binder{
public BackgroundAudioService getService(){
Log.d(this.getClass().getName(), "LocalBinder method called");
return instanceService;
}
}
public void setLoopChant(){
Log.d(this.getClass().getName(), "setLoopChant method called");
mp.setLooping(true);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(this.getClass().getName(), "onStartCommand method called");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d(this.getClass().getName(), "onDestroy method called");
mp.release();
super.onDestroy();
}
}
答案 0 :(得分:0)
您的代码甚至无法播放一次媒体。你忘了打电话了
mp.prepareAsync();
在您设置DataSource之后。
为什么要两次创建MediaPlayer?我建议在新的MediaPlayer()之后将setOnPreparedListener移动到该行。
if (mp == null ){
mp = new MediaPlayer();
mp.setOnPreparedListener(BackgroundAudioService.this);
}
然后在setDataSource之后调用prepareAsync。替换行:
mp = MediaPlayer.create(BackgroundAudioService.this, path);
与
mp.setDataSource(path);
mp.prepareAsync();