我正在开发一款带有服务的音乐播放器。目前专注于 通知和按钮......
现在我在通知栏上有按钮播放,下一个,上一个..我想要 如果媒体正在播放,则显示暂停按钮如果不是播放按钮 打...
错误是当我在通知.addction中添加If条件时,它显示红线..
我完成了显示错误的代码......
服务代码是......
Foreground.java(班级名称)..
public class ForegroundService extends Service {
private static final String LOG_TAG = "ForegroundService";
public static boolean IS_SERVICE_RUNNING = false;
final MediaPlayer mp=new MediaPlayer();
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Start Foreground Intent ");
showNotification();
//-------------------------------------------
try{
//you can change the path, here path is external directory(e.g. sdcard) /Music/maine.mp3
mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/downloadedfile.mp3");
mp.prepare();
}catch(Exception e){e.printStackTrace();}
mp.start();
//-------------------------------------
Toast.makeText(this, "Service Started!", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
Log.i(LOG_TAG, "Clicked Previous");
Toast.makeText(this, "Clicked Previous!", Toast.LENGTH_SHORT)
.show();
} else if (intent.getAction().equals(Constants.ACTION.PAUSE_ACTION)) {
Log.i(LOG_TAG, "Clicked Play");
Toast.makeText(this, "Clicked Play!", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
Log.i(LOG_TAG, "Clicked Next");
Toast.makeText(this, "Clicked Next!", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(
Constants.ACTION.STOPFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Stop Foreground Intent");
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
private void showNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Intent previousIntent = new Intent(this, ForegroundService.class);
previousIntent.setAction(Constants.ACTION.PREV_ACTION);
PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
previousIntent, 0);
Intent playIntent = new Intent(this, ForegroundService.class);
playIntent.setAction(Constants.ACTION.PLAY_ACTION);
PendingIntent pplayIntent = PendingIntent.getService(this, 0,
playIntent, 0);
//---------------------------------------------------------------------------------
Intent pauseIntent = new Intent(this, ForegroundService.class);
pauseIntent.setAction(Constants.ACTION.PAUSE_ACTION);
PendingIntent ppauseIntent = PendingIntent.getService(this, 0,
playIntent, 0);
//------------------------------------------------------------------------
Intent nextIntent = new Intent(this, ForegroundService.class);
nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
PendingIntent pnextIntent = PendingIntent.getService(this, 0,
nextIntent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.aa);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("TutorialsFace Music Player")
.setTicker("TutorialsFace Music Player")
.setContentText("My song")
.setSmallIcon(R.drawable.aa)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.addAction(android.R.drawable.ic_media_previous, "Previous",
ppreviousIntent)
//---------------------------------------------------------------------------
if(mp.isPlaying()){
.addAction(android.R.drawable.ic_media_play, "Play",
pplayIntent)
}
else{
.addAction(android.R.drawable.ic_media_pause, "Pause",
pplayIntent)
}
//------------------------------------------------------------------------------------------------
.addAction(android.R.drawable.ic_media_next, "Next",
pnextIntent).build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
notification);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(LOG_TAG, "In onDestroy");
Toast.makeText(this, "Service Detroyed!", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
// Used only in case if services are bound (Bound Services).
return null;
}
}
我添加的代码是Put Inside the Line .....
任何人都可以建议我使用精确代码或Displayig播放和暂停按钮Simultaneoisly ??谢谢
答案 0 :(得分:0)
我建议你需要为媒体播放器使用自定义视图。 Here这是一个教程,你如何做到这一点。 简而言之:您需要使用:
notification = new Notification.Builder(this).build();
notification.contentView = views;
notification.bigContentView = bigViews;
提供自定义和展开的自定义视图。
接下来,您不仅需要在开始显示通知时更新通知,还需要在按播放或暂停按钮的任何时候更新通知。所以你需要改变:
} else if (intent.getAction().equals(Constants.ACTION.PAUSE_ACTION)) {
Log.i(LOG_TAG, "Clicked Play");
Toast.makeText(this, "Clicked Play!", Toast.LENGTH_SHORT).show();
}
在
} else if (intent.getAction().equals(Constants.ACTION.PAUSE_ACTION)) {
Log.i(LOG_TAG, "Clicked Pause");
mp.pause();
showNotification();
Toast.makeText(this, "Clicked Pause!", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
Log.i(LOG_TAG, "Clicked Play");
mp.start();
showNotification();
Toast.makeText(this, "Clicked Play!", Toast.LENGTH_SHORT).show();
}
这样的事情。希望你有主意。
答案 1 :(得分:0)
在if之前放置一个半冒号,然后使用通知对象将操作设置如下:
if(mp.isPlaying())notification.addAction(android.R.drawable.ic_media_play," Play", pplayIntent); 其他 通知.addAction(android.R.drawable.ic_media_pause," Pause", pplayIntent);
我想这会做你的工作