我正在开发一款播放背景音乐的应用。音乐由在后台运行的服务管理。在Main活动中,在onCreate()方法中,服务以:
开头public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Background music service.
Intent musicServiceIntent = new Intent(this, BackgroundSoundService.class);
startService(musicServiceIntent);
final BackgroundSoundService bss = new BackgroundSoundService();
buttonStart = (Button) v.findViewById(R.id.button_start);
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bss.onStart();
}
});
buttonPause = (Button) v.findViewById(R.id.button_pause);
buttonPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bss.onPause();
}
});
buttonStop = (Button) v.findViewById(R.id.button_stop);
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bss.onStop();
}
});
}
}
}
该服务是以下代码: 的 BackgroundSoundService.java
public class BackgroundSoundService extends Service {
private static final String TAG = null;
static MediaPlayer mMediaPlayer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.background_music);
mMediaPlayer.setVolume(1.0f, 1.0f);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
playMusic();
}
});
playMusic();
}
public void playMusic() {
mMediaPlayer.start();
}
/**
*
* @param arg0 -
* @return -
*/
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
/**
*
*/
public void onStart() {
Log.d("A", "onStart()");
if(!mMediaPlayer.isPlaying()) {
Log.d("A", "mMediaPlayer is not playing");
playMusic();
} else {
Log.d("A", "mMediaPlayer is playing");
}
}
/**
*
*/
public void onPause() {
Log.d("A", "onPause()");
if(mMediaPlayer.isPlaying()) {
Log.d("A", "mMediaPlayer is playing");
mMediaPlayer.pause();
} else {
Log.d("A", "mMediaPlayer is not playing");
}
}
/**
*
*/
public void onStop() {
Log.d("A", "onStop()");
if(mMediaPlayer.isPlaying()) {
Log.d("A", "mMediaPlayer is playing");
mMediaPlayer.stop();
} else {
Log.d("A", "mMediaPlayer is not playing");
}
}
/**
*
*/
@Override
public void onDestroy() {
Log.d("A", "onDestroy()");
mMediaPlayer.stop();
mMediaPlayer.release();
}
/**
*
*/
@Override
public void onLowMemory() {
}
}
现在,MainActivity有三个按钮来控制音乐。 SART按钮,启动音乐,PAUSE按钮,暂停声音和STOP按钮,停止音乐。每个都在BackgroundSoundService中的Pause()和onStop()上执行方法onStart()。问题是如果我在STOP按钮后按下START按钮,日志会显示此错误:
E/MediaPlayer: start called in state 0
E/MediaPlayer: error (-38, 0)
我尝试解决这个问题:
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
但没有奏效。有什么建议?谢谢。
答案 0 :(得分:0)
发布MainActivity代码有助于我们提供精确的解决方案,而不会让您偏离随机答案。
答案 1 :(得分:0)
.stop()
之后,如果您想再次启动,则需要重置它。
public void onStop() {
Log.d("A", "onStop()");
if(mMediaPlayer.isPlaying()) {
Log.d("A", "mMediaPlayer is playing");
mMediaPlayer.stop();
mMediaPlayer.reset();
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.background_music);
} else {
Log.d("A", "mMediaPlayer is not playing");
}
}
修改强>
boolean isPrepared = false;
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
isPrepared = true;
}
});
public void playMusic() {
if(isPrepared){
mMediaPlayer.start();
isPrepared = false;
}
else{
Toast.makeText(getApplicationContext(),"Not prepared",LENGTH.SHORT)
.show();
}
}
答案 2 :(得分:0)