我正在开发一个带有服务的音乐播放器。 Сurrently重点 声明和按钮...Android:音乐播放器的通知错误。?
现在我对通知栏播放,下一首,上..我想 显示暂停按钮,如果媒体播放&播放按钮,如果不打 ...
按钮
的错误是,当我增加了如果条件的通知.addction它显示红线..
我做了里面的代码是显示错误...
服务代码是...
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;
}
}
我添加的代码是将线内.....
任何人都可以建议我使用Exact代码或Displayig播放和暂停按钮同时进行?谢谢
你好,你有什么确切的错误?您的播放/暂停按钮图标未更新?或者addAction方法强调Api版本错误? –
当我添加条件在.addAction之间时显示红线。 –
我想建议你使用完全自定义的视图。 1)你可以做任何视图,你想要什么,2)你会支持<20 api版本 –