服务中的媒体播放器未停止

时间:2018-04-12 18:44:37

标签: java android android-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);
    }
}

1 个答案:

答案 0 :(得分:0)

jut在if语句和问题解决后移动了Mediaplayer分配。