音乐播放器一次播放两次mp3

时间:2015-04-16 12:44:56

标签: android push-notification audio-player android-music-player

我在背景音乐播放器的帮助下从原始文件夹播放mp3 但音乐播放器正在播放两次

我正在解释我在做什么..我从服务器接收推送通知 并使用背景音乐播放器从原始播放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();

    }
}

1 个答案:

答案 0 :(得分: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); }