繁体   English   中英

Mediaplayer内部服务未停止

[英]Mediaplayer inside service not stopping

其他相关主题都没有帮助。 我的“应用”是关于手机进入地理围栏警报启动时的位置跟踪。 一切正常,但我无法停止mediaplayer。 尝试做Mediaplayer本地,全局静态。 这是我第一次问问题。

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分配移到内部。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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