Android Studio检查MediaPlayer是否在屏幕小部件活动中播放

发布于 2025-01-18 16:27:01 字数 1373 浏览 7 评论 0原文

主屏幕上会有一个按钮,它将播放一首歌并更改按钮的背景图像。如果用户再次单击按钮(音乐在播放时),则音乐应停止,并且按钮的背景图像应将其自身恢复到一般位置。但是看来该程序无法检测到我的媒体演奏者是否在玩。我在这里想念什么?

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        AppWidgetManager appWidgetManager= AppWidgetManager.getInstance(context);
        RemoteViews rv= new RemoteViews(context.getPackageName(),
                R.layout.playbtn_widget);
        if (intent.getAction().equals("btnPlay")) {
            if (!mediaPlayer.isPlaying()) {
                mediaPlayer= MediaPlayer.create(context,R.raw.itsmylife);
                mediaPlayer.start();
                rv.setImageViewResource(R.id.imbtnwidget,
                        R.drawable.btnk32);
            } else {
                mediaPlayer.stop();

            }
            mediaPlayer.setOnCompletionListener(mediaPlayer -> {
                rv.setImageViewResource(R.id.imbtnwidget,
                        R.drawable.btnk3);
                appWidgetManager.updateAppWidget(new ComponentName(context,
                        BtnAppWidgetProvider.class), rv);
            });
            appWidgetManager.updateAppWidget(new ComponentName(context,
                    BtnAppWidgetProvider.class), rv);
        }
    }

当我点击按钮时,它应该设置背景图像并停止音乐,但它只是启动了媒体播放器,而背景图像保持不变。我不知道如何解决这个问题。似乎每次都会创建一个新的媒体游戏

There'd be a button on the home screen which would play a certain song and change the background image of the button. If the user clicks on the button again (when the music is playing) then the music should stop and the background image of the button should set itself back to the general position. But it looks like the program can't detect if my Mediaplayer is playing. What am I missing here?

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        AppWidgetManager appWidgetManager= AppWidgetManager.getInstance(context);
        RemoteViews rv= new RemoteViews(context.getPackageName(),
                R.layout.playbtn_widget);
        if (intent.getAction().equals("btnPlay")) {
            if (!mediaPlayer.isPlaying()) {
                mediaPlayer= MediaPlayer.create(context,R.raw.itsmylife);
                mediaPlayer.start();
                rv.setImageViewResource(R.id.imbtnwidget,
                        R.drawable.btnk32);
            } else {
                mediaPlayer.stop();

            }
            mediaPlayer.setOnCompletionListener(mediaPlayer -> {
                rv.setImageViewResource(R.id.imbtnwidget,
                        R.drawable.btnk3);
                appWidgetManager.updateAppWidget(new ComponentName(context,
                        BtnAppWidgetProvider.class), rv);
            });
            appWidgetManager.updateAppWidget(new ComponentName(context,
                    BtnAppWidgetProvider.class), rv);
        }
    }

It should set the background image back and stop the music when I tap on the button, but it just starts the mediaplayer again and the background image remains the same. I have no idea how I could fix this. It seems like it creates a new mediaplayer every single time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2025-01-25 16:27:01

问题


它创建了一个新的Media Player,因为该类被重新创建或重新执行。这会清除所有变量并重新定义它们。这使得你的情况出现问题。

解决方案


  1. 只需创建一个类名,例如 MediaPlayerHelper.java

  2. 创建一个公共且静态的 MediaPlayer 变量。像这样:

    public static MediaPlayer mp;
    
  3. 现在,当按下按钮时,您可以检查它是否正在播放。像这样的事情:

    if(MediaPlayerHelper.mp != null && MediaPlayerHelper.mp.isPlaying()){
       // 媒体播放器当前正在播放。停止吧。
    }别的{
       // 媒体未播放。开始吧
    }
    

Issue


It creates a new Media Player because the class is recreated or re-executed. This clears all the variables and re-defines them. This makes the issue in your case.

Solution


  1. Just create a class name anything like MediaPlayerHelper.java

  2. Create a variable of MediaPlayer which is public and static. Like this:

    public static MediaPlayer mp;
    
  3. Now, when the button is pressed, you can check if it is playing or not. Something like this:

    if(MediaPlayerHelper.mp != null && MediaPlayerHelper.mp.isPlaying()){
       // the media player is currently playing. Stop it.
    }else{
       // the media is not playing. Start it
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文