[英]Changing volume throughout activities
So here I am creating a Music Service class所以我在这里创建了一个音乐服务类
public class MusicService extends Service implements MediaPlayer.OnErrorListener {
private final IBinder mBinder = new ServiceBinder();
MediaPlayer mPlayer;
private int length = 0;
public MusicService() {
}
public class ServiceBinder extends Binder {
MusicService getService() {
return MusicService.this;
}
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
mPlayer = MediaPlayer.create(this, R.raw.game_music);
mPlayer.setOnErrorListener(this);
if (mPlayer != null) {
mPlayer.setLooping(true);
mPlayer.setVolume(100, 100);
}
mPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int
extra) {
onError(mPlayer, what, extra);
return true;
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mPlayer.start();
return START_STICKY;
}
public void pauseMusic() {
if (mPlayer.isPlaying()) {
mPlayer.pause();
length = mPlayer.getCurrentPosition();
}
}
public void resumeMusic() {
if (mPlayer.isPlaying() == false) {
mPlayer.seekTo(length);
mPlayer.start();
}
}
public void stopMusic() {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mPlayer != null) {
try {
mPlayer.stop();
mPlayer.release();
} finally {
mPlayer = null;
}
}
}
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(this, "music player failed", Toast.LENGTH_SHORT).show();
if (mPlayer != null) {
try {
mPlayer.stop();
mPlayer.release();
} finally {
mPlayer = null;
}
}
return false;
}
}
Then, in my main game activity I start out the code with然后,在我的主要游戏活动中,我开始使用以下代码
public class Gameview extends AppCompatActivity {
/**
* Whether or not the system UI should be auto-hidden after
* {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
private boolean mIsBound = false;
private MusicService mServ;
private ServiceConnection Scon =new ServiceConnection(){
public void onServiceConnected(ComponentName name, IBinder
binder) {
/*mServ = ((MusicService.ServiceBinder binder).getService());*/
MusicService.ServiceBinder sb = (MusicService.ServiceBinder) binder;
mServ = sb.getService();
}
public void onServiceDisconnected(ComponentName name) {
mServ = null;
}
};
void doBindService(){
bindService(new Intent(this,MusicService.class),
Scon, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService()
{
if(mIsBound)
{
unbindService(Scon);
mIsBound = false;
}
}
Then, in the main game activities onCreate, I bind the service to the activity with然后,在主游戏活动 onCreate 中,我将服务绑定到活动
doBindService();
Intent music = new Intent();
music.setClass(this,MusicService.class);
startService(music);
NOW, the game works by allowing the user to click on planets and when they do that, it redirects them to a different activity with the picture of the planet.现在,游戏的工作原理是允许用户点击行星,当他们这样做时,它会将它们重定向到带有行星图片的不同活动。 As it stands, music will continue playing and looping through the game when users click on planets and then return to the activity it is created in (so this part is working perfectly).
就目前而言,当用户单击行星然后返回到创建它的活动时,音乐将继续播放并在游戏中循环播放(因此这部分工作正常)。 MY PROBLEM IS THIS: I want to have the music either pause or reduce in volume when a user clicks on the planet and return to normal when going back to the main game view.
我的问题是:我想让音乐在用户点击地球时暂停或降低音量,并在返回主游戏视图时恢复正常。 My problem is I don't know how to inherit my MusicService object from these planet-views in order to manipulate it.. I tried messing around with something like this the the planet's onCreate, but had no luck
我的问题是我不知道如何从这些行星视图中继承我的 MusicService 对象以操纵它。
MusicService.ServiceBinder sb = (MusicService.ServiceBinder) binder;
mServ = sb.getService()
How can I manipulate my MusicService object outside of the game's main activity???如何在游戏的主要活动之外操作我的 MusicService 对象???
public
and static
modifiers makes an object or a variable usable from other classes. public
和static
修饰符使对象或变量可以从其他类使用。
Simply declare MusicService
object as public static
, and call it in the onCreate
method of the planet activity.只需将
MusicService
对象声明为public static
,并在行星活动的onCreate
方法中调用它。
Hope it helps!希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.