需要一个简单的演示只检查是否背景音乐运行或不? [英] Need a simple demo for only check whether background Music running or not?

查看:304
本文介绍了需要一个简单的演示只检查是否背景音乐运行或不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个简单的演示,我在其中要检查的背景音乐是​​否正在运行或不?并也没有影响其上在后台运行音乐

I need a simple demo, In which I want to check whether the music is running in the background or not? And also does not effected on the music which is running in Background

我从这里研究链接阅读
并做了演示code

I read from here study Link and made a demo code

演示code是在这里,但这个不是给我的O / P:

AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
    int result = am.requestAudioFocus(new OnAudioFocusChangeListener() {

        @Override
        public void onAudioFocusChange(int focusChange) {
            if(//background Music is running )
            {}
            else{
                 //background Music is not running
            }
        }
    },AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

什么是必须增加对我想要的东西。请帮助,使其向东这个事情。

What is need to be add for what I want. Please help to make it east this thing.

推荐答案

您已经发布当前code似乎是,你实际上是要求使用STREAM_MUSIC流而不是检测它。我相信你可能有这样两个选择。

The current code you've posted appears to be that you are actually requesting to use the STREAM_MUSIC stream rather than detecting it. I believe you may have two options for this.

我没有试过是这样的方法,但它听起来像的 AudioManager.isMusicActive()可能会为你工作。

I haven't tried is this method, but it sounds like AudioManager.isMusicActive() may work for you.

在此页面中,它说:

检查任何音乐是否有效。

Checks whether any music is active.

如果任何音乐曲目是活动的,则返回true。

Returns true if any music tracks are active.

您另一种选择是使用<一个href=\"http://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html\"相对=nofollow> AudioManager.OnAudioFocusChangeListener 。必须先创建这个监听器,然后注册它来检测的变化。这意味着你的应用程序需要在后台运行之前,另一个应用程序开始使用流。

Your other option is to use AudioManager.OnAudioFocusChangeListener. You must first create this listener and then register it to detect changes. This means your app would need to be running in the background before another app started using the stream.

创建一个监听器(从 http://developer.android例子。 COM /培训/管理音频/音频focus.html

OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
    public void onAudioFocusChange(int focusChange) {
        if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT) {
            // another app took the stream temporarily
        } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
            // another app let go of the stream
        } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
            // another app took full control of the stream
        }
    }
};

现在可以注册监听器和检测更改

Now you can register the listener and detect changes

int result = am.requestAudioFocus(afChangeListener,
                                 // Use the music stream.
                                 AudioManager.STREAM_MUSIC,
                                 // Request permanent focus.
                                 AudioManager.AUDIOFOCUS_GAIN);

if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
    // You successfully took control of the stream and can detect any future changes
}

这是不是一个非常优雅的解决方案,因为你很可能会夺去其他应用程序的音乐流。这取决于你的使用情况,但它可能是一个起点。

This isn't a terribly elegant solution since you will most likely take away the music stream from other apps. It depends on your use case, but it may be a starting point.

这篇关于需要一个简单的演示只检查是否背景音乐运行或不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆