有沒有一種方法可以在應答未被呼入的呼叫時啓動音頻文件(所以對方可以聽到),但只有在呼叫揚聲器中(所以只有我們的方聽到)。當應答呼叫時觸發音頻文件
聽起來很奇怪,我知道,但它是一個更大的應用程序的一部分。
有沒有一種方法可以在應答未被呼入的呼叫時啓動音頻文件(所以對方可以聽到),但只有在呼叫揚聲器中(所以只有我們的方聽到)。當應答呼叫時觸發音頻文件
聽起來很奇怪,我知道,但它是一個更大的應用程序的一部分。
首先,您需要設置一個BroadcastReceiver(我們稱之爲「CallReceiver」),並獲得有關手機狀態的知識(直觀上,添加權限爲android.permission.READ_PHONE_STATE
)。
這樣註冊您的CallReceiver動作。
<receiver android:name=".CallReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
在你CallReceiver,你可能會決定在其行動應您的音頻播放(來電/ outcoming /電話鈴聲......),所以只讀過EXTRA_STATE和getCallState()(檢查出TelephonyManager文檔)。
關於音頻,您需要使用AudioManager,並在播放聲音之前設置「回呼」模式。
private AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
am.setSpeakerphoneOn(false);
我希望這有助於!
private void PlayShortAudioFileViaAudioTrack(String filePath) throws IOException {
// We keep temporarily filePath globally as we have only two sample sounds now..
if (filePath == null)
return;
//Reading the file..
byte[] byteData = null;
File file = null;
file = new File(filePath); // for ex. path= "/sdcard/samplesound.pcm" or "/sdcard/samplesound.wav"
byteData = new byte[(int) file.length()];
FileInputStream in = null;
try {
in = new FileInputStream(file);
in.read(byteData);
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Set and push to audio track..
int intSize = android.media.AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT, intSize, AudioTrack.MODE_STREAM);
if (at != null) {
at.play();
// Write the byte array to the track
at.write(byteData, 0, byteData.length);
at.stop();
at.release();
} else
Log.d("TCAudio", "audio track is not initialised ");
你好我開發了以下應用程序,它做同樣的事情,檢查這個應用程序,如果它解決了你的問題,那麼我可以給你的源代碼。 https://market.android.com/details?id=com.devindia.acr&feature=search_result –
@KPBird,這看起來可能會有所斬獲。如果您確實發送了代碼供我們審覈,我將不勝感激。感謝您的提議。 – CraigInDallas
你好,我今天將上傳代碼... –