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

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

问题描述

我是新手.我的自学项目是一个播放音频新闻流的小应用程序.为了有效,应用程序确实需要中断任何当前正在播放的媒体播放器(例如:Last FM、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?

谢谢.

推荐答案

Android 上的音频处理将在一段时间内变得非常糟糕.API 非常奇怪,文档不全,并且在版本之间不断更改/弃用/破坏.甚至 AudioManager 代码也包含 FIXME.

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.

无论如何,Android 中有多种流类型(音乐、通知、电话等),应用程序旨在选择合适的一种进行播放.我想大多数 Android 应用程序应该使用音乐/媒体类型 (STREAM_TYPE_MUSIC).您可以使用 setAudioStreamType 方法在 MediaPlayer 上设置它.

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 的电话呼叫流,并在以下情况下使用方法 call AudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true)播放开始,然后在播放或​​您的 Activity 完成时使用 false 取消独奏.

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,否则每当您按下音量硬键时,它都会显示通话音量"!但是,如果您确实想改回 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天全站免登陆