通话期间Android播放音频文件 [英] Android play audio file during phone call

查看:932
本文介绍了通话期间Android播放音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的Android应用,我想在接听应用程序的呼叫后播放音频文件. 应用程序将启动电话,并且一旦接收者接听电话,应用程序应开始播放录制的音频文件.

For my Android app, I want to play an audio file after answering the call from my application. App will initiate phone call and once receiver pick up the call, app should start playing recorded audio file.

通过在Google中进行大量搜索,我发现这对于不带根目录的设备是不可能直接实现的.

By searching a lot in Google I found that this is not directly possible for unrooted device.

但是,此方法的任何替代方法或实现此目标的任何其他方法?

But any alternative of this or any other way to achieve this?

我要实现此功能是因为该应用程序的功能是:如果应用程序用户遇到麻烦,他可以按应用程序中的帮助按钮,然后该应用程序将开始呼叫所提到的联系人,并应尽快播放录制的音频文件人接了电话.

I want to implement this because functionality of the app is: if App user is in trouble, he can just press help button in the app and then app starts calling to the mentioned contact person and should play recorded audio file as soon as person received the call.

推荐答案

阅读步骤:

1.某些处理此问题的android设备中的本地音乐播放器,它们会在TelephonyManager.EXTRA_STATE_OFFHOOK(OFFHOOK STATE)中通话时限制音乐,因此无法使用本地播放器播放背景音乐.诸如"poweramp音乐播放器"之类的第三方apss是有可能的.

1.Some of the native music players in android device where handling this,they restrict the music when call is in TelephonyManager.EXTRA_STATE_OFFHOOK (OFFHOOK STATE) so there is no way of playing the background music using native players.But in some of the third party apss like "poweramp music palyer" it is possible.

2.通过使用MediaPlayer类也是不可能的(在文档中有明确提及)

2.By using the MediaPlayer class also it is not possible(clearly mentioned in documentation)

3.仅在一种情况下,如果您开发的自定义音乐播放器(不使用MediaPlayer类)在其中实现,则是可能的.

3.It is possible only in one case if your developing custom music player(with out using MediaPlayer class) in that implements

AudioManager.OnAudioFocusChangeListener通过使用此代码,您可以在下面的代码"focusChange = AUDIOFOCUS_LOSS_TRANSIENT"中获取音频管理器的状态(当音乐在后台播放时,任何来电都将调用此状态)此状态是否完全在开发人员手中播放或暂停音乐.根据您的要求,您询问是否要在非通话状态下播放音乐,请不要在非通话状态下暂停播放音乐.这仅在禁用了耳机的情况下可以实现

AudioManager.OnAudioFocusChangeListener by using this you can get the state of the audiomanager in the below code "focusChange=AUDIOFOCUS_LOSS_TRANSIENT"(this state calls when music is playing in background any incoming call came) this state is completely in developers hand whether to play or pause the music. As according to your requriment as for question you asked if you want to play the music when call is in OFFHOOK STATE dont pause playing music in OFFHOOK STATE .And this is only possible when headset is disabled

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

OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
    public void onAudioFocusChange(int focusChange) {
        if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT
            // Pause playback (during incoming call) 
        } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
            // Resume playback (incoming call ends)
        } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
            am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
            am.abandonAudioFocus(afChangeListener);
            // Stop playback (when any other app playing music  in that situation current app stop the audio)
        }
    }
};

这篇关于通话期间Android播放音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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