现在木鱼已经能发出声音了,而且还能表现出动态画面了,下一步就是给整个敲木鱼的页面添加个BGM。
新建一个Service,取名为MyFirstBgmService好了
package com.example.cyberwoodenfish;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
public class MyFirstBgmService extends Service {
public MyFirstBgmService() {
}
MediaPlayer mp;
@Override
public void onCreate() { //开始服务时调用
super.onCreate();
mp = MediaPlayer.create(this,R.raw.dabeizhou);
mp.start();
}
@Override
public void onDestroy() { //服务销毁时停止音乐播放
if (mp != null){
mp.stop();
mp.release();
}
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
然后在MainActivity里动手package com.example.cyberwoodenfish;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intentbgm;
intentbgm=new Intent(MainActivity.this,MyFirstBgmService.class);
startService(intentbgm);
//。。。与bgm无关的代码又被我吃了
}
}
现在进入敲木鱼的界面直接播放大悲咒。。。
嗯,有那味了。
end