bounty还有5天到期。回答此问题可获得+100声望奖励。Sudarshan Taparia正在寻找规范答案。
我试图在Android上做一个井字游戏。它也有几个变种。我已经创建了一个后台服务类。相同的代码如下。
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和classictictacttoe都是活动。每当活动打开时都应播放音乐(menupage和classictacttoe),但当活动classictacttoe打开时不播放音乐,音乐仅在menupage活动打开时播放
//classictactoe活动的相关代码部分
Intent intent = new Intent(ClassicTicToe.this, BackgroundSoundService.class);
intent.putExtra("itemname","classictictactoe");
startService(intent);
//menupage活动的相关代码部分
i = new Intent(MenuPage.this, BackgroundSoundService.class);
i.putExtra("itemname","menupage");
startService(i);
我不明白为什么音乐在两个活动中都不播放
我有音乐文件cof.mp3和got.mp3在原始文件夹
3条答案
按热度按时间kd3sttzy1#
我觉得你应该把
mp = null;
加到Destroy查看this example了解更多信息
hmmo2u0o2#
请检查这个:
p8ekf7hl3#
您可以从
BroadcastReeiver
获得一些帮助。当活动恢复时,您只需要检查服务是否正在运行(您可以使用
ServiceConnection
)。如果它已经在运行,那么你可以简单地发送一个广播&之后,在接收广播时,检查媒体播放器是否正在播放以前的媒体,如果它已经在播放媒体,那么你可以做任何你想做的(已经有一些答案)。
如果该服务没有运行,那么您需要首先使用适当的intent数据启动它,并且必须在
onStartCommand
中查看该信息并相应地播放音乐。就这样!给予看。