在不同活动中播放不同背景音乐的更好方式

问题描述 投票:0回答:1

我是 Android 应用程序开发新手,有很多东西需要学习。 我正在创建一个具有多种活动的应用程序。我想找到一种方法来创建一个可以根据不同活动循环播放不同背景音乐的服务。

我试图创建一个这样的背景音乐服务

public class BGMService extends Service {
    private MediaPlayer player;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String currentActivity = intent.getStringExtra("activity_key");

        // Release the previous MediaPlayer instance
        if (player != null) {
            player.stop();
            player.release();
        }

        // Initialize a new MediaPlayer instance
        player = new MediaPlayer();

        try {
            // Check the current activity and set the data source
            if (MAIN_ACTIVITY_EXTRA_VALUES.equals(currentActivity)) {
                player.setDataSource(getResources().openRawResourceFd(R.raw.background));
            } else if (MAIN_PlAY_HOME_EXTRA_VALUES.equals(currentActivity)) {
                player.setDataSource(getResources().openRawResourceFd(R.raw.hall));
            }

            // Set looping and volume
            player.setLooping(true);
            player.setVolume(0.5f, 0.5f); // Adjust volume as needed

            // Set the prepared listener
            player.setOnPreparedListener(mp -> {
                // Start playback when prepared
                mp.start();
            });

            // Prepare the MediaPlayer
            player.prepareAsync();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        if (player != null) {
            player.stop();
            player.release();
        }
        super.onDestroy();
    }
}

在我的活动中,我将为其分配一个像这样的常量

public class MainActivity extends AppCompatActivity {

    private ViewPager2 viewPager2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onBindingView();
        //for view
        ViewPagerAdapter adapter = new ViewPagerAdapter(this);
        viewPager2.setAdapter(adapter);
        viewPager2.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);

        //for background music
        Intent servicebgIntent = new Intent(this, BGMService.class);
        servicebgIntent.putExtra("activity_key", MAIN_ACTIVITY_EXTRA_VALUES);
        startService(servicebgIntent);

    }

    private void onBindingView() {
        viewPager2 = findViewById(R.id.vp2Intro);
    }

    @Override
    protected void onDestroy() {
        Intent service = new Intent(this, BGMService.class);
        stopService(service);
        super.onDestroy();
    }
}

但是,我的代码给我带来了一些问题:

  • 当我按下主页按钮时音乐仍在播放

  • 当我切换到另一个活动时,当前的背景音乐消失了

java android mobile
1个回答
0
投票

不要使用服务。如果您“确实”想要在后台播放音乐,或者您想在活动中保持相同的音乐播放,那么服务就有意义。如果您希望每个活动都有一首新歌曲,请让每个活动单独处理音乐。要使其在 Activity 进入后台时关闭,请在 onPause 中关闭音乐,然后在 onResume 中重新打开。

© www.soinside.com 2019 - 2024. All rights reserved.