简体   繁体   中英

Mediaplayer inside service not stopping

None of other related topics did help. My "app" is about location tracking when phone enters geofence alarm starts. Everything works but I cant stop mediaplayer. tried doing Mediaplayer local, global static. Its my first time asking question.

import android.support.annotation.Nullable;
import android.util.Log;

public class PlayerService extends Service {

   private static MediaPlayer mediaPlayer;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.best_tone_2);
        mediaPlayer.setLooping(true);
        Log.i("PLAYER_SERVICE", "onStartCommand: Player service started");

        String type = intent.getStringExtra("type");


        if(type.equalsIgnoreCase("entered")) {
            Log.i("PLAYER_SERVICE", "onStartCommand: Player service started with type 'ENTERED'");
            Notification.Builder builder = new Notification.Builder(this);

            Intent not_intent = new Intent(this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, not_intent, 0);

            // builder.setFullScreenIntent(pendingIntent,true);
            builder.setContentTitle("Title");
            builder.setSmallIcon(this.getApplicationInfo().icon);
            builder.setTicker("Ticker");
            builder.setContentIntent(pendingIntent);
            builder.setDeleteIntent(pendingIntent);
            builder.setAutoCancel(true);

            builder.setWhen(System.currentTimeMillis());
            builder.setContentText("Text");
            builder.setVibrate(new long[]{1000, 1000});
            Notification notification = builder.build();
            NotificationManager manager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
            manager.notify(0, notification);


            mediaPlayer.start();
        }

        if(type.equalsIgnoreCase("stop") )
        {
            Log.i("PLAYER_SERVICE", "TRYING TO STOP PLAYER");
            mediaPlayer.stop();
            mediaplayer.reset();
            mediaplayer.release();
        }
        return START_NOT_STICKY;
       // return super.onStartCommand(intent, flags, startId);
    }
}

如果语句和问题解决,则将Mediaplayer分配移到内部。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM