Android的 - 我可以静音当前正在播放的音频应用? [英] Android - can I mute currently playing audio applications?

查看:193
本文介绍了Android的 - 我可以静音当前正在播放的音频应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新来的这一点。我的自学项目是一个小的应用程序,它会播放一段音频消息流。为了有效的应用程序确实需要中断任何当前正在播放的媒体播放器(如:最后调频,Imeem的音乐播放器,Spotify的等等)。

Hi I'm new to this. My self-teaching project is a small application which plays an audio news stream. To be effective, the application really needs to interrupt any currently playing media players (eg: Last FM, Imeem, music player, Spotify etc).

我不知道会玩或者是应用程序将被打,但我要问他们都暂停,直到我所做的一切。听起来相当大胆的,我知道,但它一定是可能的,因为当有来电时,这基本上会发生什么。

I don't know what will be playing or what application will be playing it, but I want to ask them all to pause until I've done. Sounds rather bold, I know, but it must be possible, because when there's an incoming call, that's basically what happens.

我可以将消息发送给所有的音频播放器,要求他们暂停了一会儿?我可以欺骗来电,只是花费的时间?

Can I send a message to all audio players asking them to pause for a moment? Could I spoof an incoming call, just for the time it takes?

感谢。

推荐答案

音频处理上的Andr​​oid将是pretty的可怕的片刻。这些API是pretty的怪异,记录不完整和不断变化/日precating /版本之间的断裂。即使是 AudioManager code的FIXMEs在里面。

Audio handling on Android is going to be pretty horrible for a while. The APIs are pretty weird, poorly documented, and keep changing/deprecating/breaking between versions. Even the AudioManager code has FIXMEs in it.

无论如何,有在机器人几个流类型(音乐,通知,电话呼叫等)和应用程序是为了选择适当的一个进行播放。我想大部分的Andr​​oid应用程序应该使用的音乐/媒体类型( STREAM_TYPE_MUSIC )。您可以设置这在的MediaPlayer 使用 setAudioStreamType 方法。

Anyway, there are several stream types in Android (music, notifications, phone calls, etc.) and applications are meant to choose the appropriate one for playback. I imagine the majority of Android apps should use the music/media type (STREAM_TYPE_MUSIC). You set this on your MediaPlayer using the setAudioStreamType method.

该SDK确实允许您设置一个流的键入的独唱—导致所有其他流静音—但我不相信你能识别的音频正在播放由特定的应用程序,并以某种方式暂停/取消暂停。音乐应用程序中一般会使用 PhoneStateListener 暂停自己,当有电话打进来

The SDK does allow you to set a single stream type as solo — causing all other streams to be muted — but I don't believe you can identify the audio being played back by particular applications and somehow pause/unpause it. Music applications in general will use the PhoneStateListener to pause themselves when a call comes in.

因此​​,在你的情况,你可以借用手机通话流的的MediaPlayer 和使用方法调用 AudioManager.setStreamSolo(AudioManager。 STREAM_VOICE_CALL,真)开始播放时,则取消独奏与流时,播放或活动完成的。

So in your case, you could "borrow" the phone call stream for your MediaPlayer and use the method call AudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true) when playback begins, then un-solo the stream with false when playback or your Activity is done.

我可以告诉你,这个工作,但我不记得随便你是否还需要设置音频的模式 MODE_IN_CALL 时,使用语音呼叫流(像这样: AudioManager.setMode(AudioManager.MODE_IN_CALL))。如果发现是必需的,那么你需要确保你返回模式为 MODE_NORMAL 一旦播放完成,否则当你preSS音量硬键,它的会说通话音量!但是,如果当你想改回 MODE_NORMAL ,你必须检查一个真正的电话是不会发生在那个时候......

I can tell you that this works, but I can't remember offhand whether you need to also set the audio mode to MODE_IN_CALL when using the voice call stream (like this: AudioManager.setMode(AudioManager.MODE_IN_CALL)). If you find that is required, then you need to make sure you return the mode to MODE_NORMAL once playback completes, otherwise whenever you press the volume hard keys, it'll say "In-call volume"! However, if and when you do want to change back to MODE_NORMAL, you must check that a genuine phone call isn't happening at that time...

也许你可以使用另一种流类型,而不是语音通话,但是我只是从经验上讲工作的一个应用程序,可以使用任何扬声器或耳机的音频播放,它的需要使用语音呼叫流。

Maybe you could use another stream type rather than the voice call one, but I'm just speaking from experience working on an app that could use either the speakerphone or the earpiece for audio playback, which requires the use of the voice call stream.

就像我说的,音频的处理不是特别好玩...;)

Like I said, audio handling isn't particularly fun... ;)

这篇关于Android的 - 我可以静音当前正在播放的音频应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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