[英]Android playing song during call
I am trying to build an application which plays a song during call. 我正在尝试构建一个在通话过程中播放歌曲的应用程序。 It is working fine when calling someone. 打电话给某人时工作正常。 But it is unable to play the song while continuing with the call. 但是在继续通话时无法播放歌曲。 I have put the mp3 file in the assets folder. 我已将mp3文件放在资产文件夹中。 Here is my code: 这是我的代码:
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
//Log.i(LOG_TAG, "OFFHOOK");
try {
AssetFileDescriptor descriptor = getAssets().openFd("la.mp3");
mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
mp.prepare();
mp.setVolume(1f, 1f);
mp.setLooping(true);
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
I am able to do this as calling the playAudio() before calling the callIntent- 我可以通过在调用callIntent-之前调用playAudio()来做到这一点-
public void playAudio() {
try {
if (m.isPlaying()) {
Log.d("Playing the audio", "");
m.stop();
m.release();
m = new MediaPlayer();
}
m.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
AssetFileDescriptor descriptor = getAssets().openFd("test.mp3");
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopAudio() {
if (m.isPlaying()) {
Log.d("stop the audio", "");
m.stop();
m.release();
m = new MediaPlayer();
}
}
public class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
//Hangup
case TelephonyManager.CALL_STATE_IDLE:
stopAudio();
audioManager.setSpeakerphoneOn(false);
break;
//Outgoing
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
//Incoming
case TelephonyManager.CALL_STATE_RINGING:
break;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.