我想在安卓上做一个井字游戏。它也有一些变体。我创建了一个后台服务类。相同的代码如下。
package com.sudarshan.tictactoenew;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Service;
import android.util.Log;
public class BackgroundSoundService extends Service {
private static final String TAG = null;
// Intent intent;
MediaPlayer mp;
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
mp = new MediaPlayer();
Bundle extras = intent.getExtras();
String itemname = (String) extras.get("itemname");
Log.i("Itemname",itemname);
if(itemname.equals("menupage"))
{
String filename = "android.resource://" + this.getPackageName() + "/raw/got";
try {
mp.setDataSource(this, Uri.parse(filename));
mp.prepareAsync();
mp.setLooping(true); // Set looping
mp.setVolume(100,100);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
;
} catch(Exception e){
e.printStackTrace();
}
// = MediaPlayer.create(this, R.raw.got);
// player1.start();
}
else if(itemname.equals("classictictactoe"))
{
Log.i("Inside","Inside classic tic tac toe");
String filename = "android.resource://" + this.getPackageName() + "/raw/cof";
try {
mp.setDataSource(this, Uri.parse(filename));
mp.prepareAsync();
mp.setLooping(true); // Set looping
mp.setVolume(100,100);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
} catch(Exception e){
e.printStackTrace();
}
}
return startId;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
}
@Override
public void onDestroy() {
mp.stop();
mp.release();
}
@Override
public void onLowMemory() {
}
}
此代码不会执行 Log.i("Inside","Inside classic tic tac toe"); 以下是我的日志文件部分
2022-10-02 21:44:36.475 13019-13019 Itemname com.sudarshan.tictactoenew I menupage
2022-10-02 21:45:01.080 13019-13019 Itemname com.sudarshan.tictactoenew I classictictactoe
2022-10-02 21:57:39.288 13019-13019 Itemname com.sudarshan.tictactoenew I menupage
2022-10-02 21:57:46.764 13019-13019 Itemname com.sudarshan.tictactoenew I classictictactoe
2022-10-02 21:57:57.114 13019-13019 Itemname com.sudarshan.tictactoenew I menupage
2022-10-02 21:58:03.626 13019-13019 Itemname com.sudarshan.tictactoenew I menupage
2022-10-02 21:58:08.837 13019-13019 Itemname com.sudarshan.tictactoenew I classictictactoe
---------------------------- PROCESS ENDED (13019) for package com.sudarshan.tictactoenew ----------------------------
---------------------------- PROCESS STARTED (13341) for package com.sudarshan.tictactoenew ----------------------------
---------------------------- PROCESS ENDED (13341) for package com.sudarshan.tictactoenew ----------------------------
---------------------------- PROCESS STARTED (13430) for package com.sudarshan.tictactoenew ----------------------------
2022-10-02 21:59:31.086 13430-13430 Itemname com.sudarshan.tictactoenew I menupage
2022-10-02 21:59:36.137 13430-13430 Itemname com.sudarshan.tictactoenew I classictictactoe
menupage和classicticctactoe都是activity。活动打开时应播放音乐(菜单页面和 classicticctactoe) 但是当activity classictictactoe打开时没有音乐播放,音乐只在menupage activity打开时播放
//classictictactoe活动相关代码部分
Intent intent = new Intent(ClassicTicToe.this, BackgroundSoundService.class);
intent.putExtra("itemname","classictictactoe");
startService(intent);
//menupage activity相关代码部分
i = new Intent(MenuPage.this, BackgroundSoundService.class);
i.putExtra("itemname","menupage");
startService(i);
我不明白为什么两个活动的音乐都没有播放
我在raw文件夹里有音乐文件cof.mp3和got.mp3
我认为你应该将
mp = null;
添加到Destroy
public void onDestroy() {
mp.stop();
mp.release();
mp = null;
}
查看这个例子了解更多信息
请检查:
if(mp != null) {
mp.stop();
mp.release();
}
mp = new MediaPlayer();
...
...
...
return Start_Sticky;