【问题标题】:music player play mp3 twice at a time音乐播放器一次播放 mp3 两次
【发布时间】:2015-04-17 13:55:16
【问题描述】:

我在后台音乐播放器的帮助下从原始文件夹播放 mp3 但是音乐播放器有时会播放两次

我正在解释我在做什么..我正在接收来自服务器的推送通知 并在后台使用音乐播放器从 raw 播放 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();

    }
}

【问题讨论】:

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


    【解决方案1】:

    在你的 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); }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      相关资源
      最近更新 更多