我想在我的所有活动中使用我的声音,而不仅仅是主要活动。我把这行
MediaPlayer ring = MediaPlayer.create(MainActivity.this, R.raw.song);
ring.start();
但仅适用于主要活动。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaPlayer ring = MediaPlayer.create(MainActivity.this, R.raw.song);
ring.start();
答案 0 :(得分:0)
你可以尝试这样的事情
<强> 1。在您的所有活动中添加此代码
MediaPlayer ring = MediaPlayer.create(MainActivity.this,
R.raw.song);
ring.start();
<强> 2。创建一个这样的方法,并从您想要播放音乐的活动中调用此消息
public void Playmusic(Context context){
MediaPlayer ring = MediaPlayer.create(context, R.raw.song);
ring.start();
}
现在只需像这样调用此方法
Playmusic(MainActivity.this);
答案 1 :(得分:0)
在服务中播放您的歌曲并仅在那里初始化媒体播放器对象
Select * from Table1
where Priority = 'critical'
and ([% Diff] > = 5 or [% Diff ] <= -5 )
OR (Priority = 'high' and ([% Diff ] > = 10 or [% Diff] <= -10 ))
OR (Priority = 'LOW' and ([% Diff ] > = 20 or [% Diff ] <= -20 ))
OR (Priority = 'MEDIUM' and ([% Diff ] > = 15 or [% Diff ] <= -15 ))
请在Manifest中调用此服务
Intent svc=new Intent(this, BackgroundSoundService.class);
startService(svc);
public class BackgroundSoundService extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.idil);
player.setLooping(true); // Set looping
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
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() {
player.stop();
player.release();
}
@Override
public void onLowMemory() {
}
}