Я хочу отключить фоновую музыку c в моем приложении - PullRequest
0 голосов
/ 06 апреля 2020

Я сейчас нахожусь в процессе создания приложения для своей компании, приложение должно автоматически воспроизводить musi c на всех oop в фоновом режиме, как только приложение будет запущено. Теперь я хотел бы добавить на панель действий кнопку, которая может отключить и включить звук фона musi c после нажатия, но я не могу найти код, который можно использовать, поэтому, пожалуйста, если кто-нибудь может мне помочь и сказать, что это код, который я могу использовать, я был бы очень признателен. Вот код, который я использовал для фона musi c:

package com.example.prettywomanalbum;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class Lipsticks extends AppCompatActivity {

    HomeWatcher mHomeWatcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lipsticks);

        doBindService();
        Intent music = new Intent();
        music.setClass(this, MusicService.class);
        startService(music);


        mHomeWatcher = new HomeWatcher(this);
        mHomeWatcher.setOnHomePressedListener(new HomeWatcher.OnHomePressedListener() {
            @Override
            public void onHomePressed() {
                if (mServ != null) {
                    mServ.pauseMusic();
                }
            }
            @Override
            public void onHomeLongPressed() {
                if (mServ != null) {
                    mServ.pauseMusic();
                }
            }
        });
        mHomeWatcher.startWatch();

    }




    private boolean mIsBound = false;
    private MusicService mServ;
    private ServiceConnection Scon =new ServiceConnection(){

        public void onServiceConnected(ComponentName name, IBinder
                binder) {
            mServ = ((MusicService.ServiceBinder)binder).getService();
        }

        public void onServiceDisconnected(ComponentName name) {
            mServ = null;
        }
    };

    void doBindService(){
        bindService(new Intent(this,MusicService.class),
                Scon, Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }

    void doUnbindService()
    {
        if(mIsBound)
        {
            unbindService(Scon);
            mIsBound = false;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (mServ != null) {
            mServ.resumeMusic();
        }

    }



    @Override
    protected void onPause() {
        super.onPause();

        PowerManager pm = (PowerManager)
                getSystemService(Context.POWER_SERVICE);
        boolean isScreenOn = false;
        if (pm != null) {
            isScreenOn = pm.isScreenOn();
        }

        if (!isScreenOn) {
            if (mServ != null) {
                mServ.pauseMusic();
            }
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        doUnbindService();
        Intent music = new Intent();
        music.setClass(this,MusicService.class);
        stopService(music);

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }




    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.abus:
                Intent aboutusintent=new Intent(Lipsticks.this, AboutUs.class);
                startActivity(aboutusintent);
                return false;

            case R.id.lipgm:
                Intent lipglossintent=new Intent (Lipsticks.this, Lipgloss.class);
                startActivity(lipglossintent);
                return false;

            default:
                return super.onOptionsItemSelected(item);
        }
    }









}
...