2015-04-16 255 views
0

即時播放MP3的時候是打兩次音樂播放器播放MP3連續兩次從音樂播放器在後臺 但音樂播放器的幫助原始文件夾

我我解釋說,我在做什麼..im recieving在後臺推送從服務器 通知和播放MP3從原材料使用音樂播放器

我的問題是,我的MP3是在一個時間

這裏打兩次是我的代碼

public class GCMIntentService extends GCMBaseIntentService { 
    public static MediaPlayer mPlayer=null; 
    public static SoundPool sp=null; 
    static int iTmp; 
    private static final String TAG = "GCMIntentService"; 

    public GCMIntentService() { 
     super(SENDER_ID); 
    } 

    /** 
    * Method called on device registered 
    **/ 
    @Override 
    protected void onRegistered(Context context, String registrationId) { 
     Log.i(TAG, "Device registered: regId = " + registrationId); 
     displayMessage(context, "Your device registred with GCM"); 
     Log.d("NAME", MainMenuActivity.android_id); 
     ServerUtilities.register(context, MainMenuActivity.android_id, registrationId); 
    } 

    /** 
    * Method called on device un registred 
    * */ 
    @Override 
    protected void onUnregistered(Context context, String registrationId) { 
     Log.i(TAG, "Device unregistered"); 
     displayMessage(context, getString(R.string.gcm_unregistered)); 
     ServerUtilities.unregister(context, registrationId); 
    } 

    /** 
    * Method called on Receiving a new message 
    * */ 
    @Override 
    protected void onMessage(Context context, Intent intent) { 
     Log.i(TAG, "Received message"); 
     String message = intent.getExtras().getString("price"); 

     displayMessage(context, message); 
     // notifies user 
     generateNotification(context, message); 
    } 

    /** 
    * Method called on receiving a deleted message 
    * */ 
    @Override 
    protected void onDeletedMessages(Context context, int total) { 
     Log.i(TAG, "Received deleted messages notification"); 
     String message = getString(R.string.gcm_deleted, total); 
     displayMessage(context, message); 
     // notifies user 
     generateNotification(context, message); 
    } 

    /** 
    * Method called on Error 
    * */ 
    @Override 
    public void onError(Context context, String errorId) { 
     Log.i(TAG, "Received error: " + errorId); 
     displayMessage(context, getString(R.string.gcm_error, errorId)); 
    } 

    @Override 
    protected boolean onRecoverableError(Context context, String errorId) { 
     // log message 
     Log.i(TAG, "Received recoverable error: " + errorId); 
     displayMessage(context, getString(R.string.gcm_recoverable_error, 
       errorId)); 
     return super.onRecoverableError(context, errorId); 
    } 

    /** 
    * Issues a notification to inform the user that server has sent a message. 
    */ 
    @SuppressWarnings("deprecation") 
    private static void generateNotification(Context context, String message) { 
     int icon = R.drawable.islamicicon; 
     long when = System.currentTimeMillis(); 
     String notify[]=message.split(" "); 
     String namaz=""; 
     if(notify[0].equals("appnotify")) 
     { 
      for (int i = 1; i < notify.length; i++) { 
      namaz=namaz+notify[i]+" "; 

     } 

     } 
     else { 
     namaz=message; 
     } 

     NotificationManager notificationManager = (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, namaz, when); 
     RemoteViews remoteViews= new RemoteViews(context.getPackageName(), R.layout.customenotification); 
     // ImageView right=(ImageView)context. 
     remoteViews.setImageViewResource(R.id.imagenotileft,icon); 
     remoteViews.setImageViewResource(R.id.imagenotiright,R.drawable.silent); 
     remoteViews.setTextViewText(R.id.title,context.getString(R.string.app_name)); 
     remoteViews.setTextViewText(R.id.text,namaz); 

     notification.contentView=remoteViews; 


     Intent notificationIntent = new Intent(context, PrayTimtableFragment.class); 

     // set intent so it does not start a new activity 
     /*notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
       Intent.FLAG_ACTIVITY_SINGLE_TOP);*/ 
     PendingIntent intent = 
       PendingIntent.getActivity(context, 0, notificationIntent,0); 
     notification.contentIntent=intent; 

    // notification.setLatestEventInfo(context, title, namaz, intent); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     // notification.sound=Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.azan1); 

     String Packagname=context.getPackageName(); 

     if(notify[0].equals("appnotify")) 
       { 
      /* sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0); 

       iTmp = sp.load(context, R.raw.azan1, 1); // in 2nd param u have to pass your desire ringtone 

       sp.play(iTmp, 1, 1, 0, 0, 1); 
       */ 
     //notification.sound = Uri.parse("android.resource://com.example.islamicapp/"+R.raw.azan1); 


      mPlayer = MediaPlayer.create(context, R.raw.azan1); 
      // in 2nd param u have to pass your desire ringtone 
      if(mPlayer.isPlaying()) 
       { 


       } 
      else 
       { 
        mPlayer.start(); 
       } 
      } 
     else 
     { 
      notification.defaults |= Notification.DEFAULT_SOUND; 
     } 






    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() +R.raw.azan1); 
    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() +); 
     // Vibrate if vibrate is enabled 
     //notification.defaults |= Notification.DEFAULT_VIBRATE; 
     notificationManager.notify(0, notification); 



    } 

    public static void StopNotification() 
    { 

    mPlayer.stop(); 
     mPlayer.reset(); 

    } 
} 

回答

0

在你的onMessage,請確保您只處理

GoogleCloudMessaging. MESSAGE_TYPE_MESSAGE.equals(messageType)

此外,不正確履行既呼籲generateNotification

@Override protected void onMessage(Context context, Intent intent) { Log.i(TAG, "Received message"); String message = intent.getExtras().getString("price"); displayMessage(context, message); // notifies user generateNotification(context, message); } /** * Method called on receiving a deleted message * */ @Override protected void onDeletedMessages(Context context, int total) { Log.i(TAG, "Received deleted messages notification"); String message = getString(R.string.gcm_deleted, total); displayMessage(context, message); // notifies user generateNotification(context, message); }