Java sinch来电铃声
Alarm manager铃声在kitkat上运行良好,但当我在棒棒糖/棉花糖上尝试时,它在sinch来电中不起作用。 我尝试了默认铃声和自定义铃声,但在kitkat之后的版本中没有铃声 sinchJava sinch来电铃声,java,android,sinch,Java,Android,Sinch,Alarm manager铃声在kitkat上运行良好,但当我在棒棒糖/棉花糖上尝试时,它在sinch来电中不起作用。 我尝试了默认铃声和自定义铃声,但在kitkat之后的版本中没有铃声 sinch public class AudioPlayer { static final String LOG_TAG = AudioPlayer.class.getSimpleName(); private Context mContext; private MediaPlayer mPlayer;
public class AudioPlayer {
static final String LOG_TAG = AudioPlayer.class.getSimpleName();
private Context mContext;
private MediaPlayer mPlayer;
private AudioTrack mProgressTone;
private final static int SAMPLE_RATE = 16000;
Ringtone currentRingtone;
public AudioPlayer(Context context) {
this.mContext = context.getApplicationContext();
}
public void playRingtone() {
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
// Honour silent mode
switch (audioManager.getRingerMode()) {
case AudioManager.RINGER_MODE_NORMAL:
Uri currentRintoneUri = RingtoneManager.getActualDefaultRingtoneUri(mContext
, RingtoneManager.TYPE_RINGTONE);
Log.e("RingTone",""+currentRintoneUri);
currentRingtone = RingtoneManager.getRingtone(mContext, currentRintoneUri);
currentRingtone.play();
break;
}
}
public void playRingtoneLM() {
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_RING, audioManager.getStreamMaxVolume(AudioManager.STREAM_RING), AudioManager.FLAG_ALLOW_RINGER_MODES|AudioManager.FLAG_PLAY_SOUND);
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, max, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_RING);
try {
mPlayer.setDataSource(mContext,
Uri.parse("android.resource://" + mContext.getPackageName() + "/" + R.raw.phone_loud1));
mPlayer.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "Could not setup media player for ringtone");
mPlayer = null;
return;
}
mPlayer.setLooping(true);
mPlayer.start();
}
public void stopRingtoneLM() {
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
public void stopRingtone() {
if (currentRingtone != null) {
currentRingtone.stop();
currentRingtone = null;
}
}
public void playProgressTone() {
stopProgressTone();
try {
mProgressTone = createProgressTone(mContext);
mProgressTone.play();
} catch (Exception e) {
Log.e(LOG_TAG, "Could not play progress tone", e);
}
}
public void stopProgressTone() {
if (mProgressTone != null) {
mProgressTone.stop();
mProgressTone.release();
mProgressTone = null;
}
}
private static AudioTrack createProgressTone(Context context) throws IOException {
AssetFileDescriptor fd = context.getResources().openRawResourceFd(R.raw.progress_tone);
int length = (int) fd.getLength();
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, SAMPLE_RATE,
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, length, AudioTrack.MODE_STATIC);
byte[] data = new byte[length];
readFileToBytes(fd, data);
audioTrack.write(data, 0, data.length);
audioTrack.setLoopPoints(0, data.length / 2, 30);
return audioTrack;
}
private static void readFileToBytes(AssetFileDescriptor fd, byte[] data) throws IOException {
FileInputStream inputStream = fd.createInputStream();
int bytesRead = 0;
while (bytesRead < data.length) {
int res = inputStream.read(data, bytesRead, (data.length - bytesRead));
if (res == -1) {
break;
}
bytesRead += res;
}
}
公共级音频播放器{
静态最终字符串LOG_TAG=AudioPlayer.class.getSimpleName();
私有上下文;
私有媒体播放器mPlayer;
私人音轨mProgressTone;
私人最终静态整数抽样率=16000;
铃声;
公共音频播放器(上下文){
this.mContext=context.getApplicationContext();
}
公共无效播放铃声(){
AudioManager AudioManager=(AudioManager)mContext.getSystemService(Context.AUDIO_服务);
//荣誉沉默模式
开关(audioManager.getRingerMode()){
case AudioManager.RINGER\u模式\u正常:
Uri currentRintoneUri=RingtoneManager.getActualDefaultRingtoneUri(mContext
,铃声管理器。类型_铃声);
Log.e(“铃声”,“currentRintoneUri”);
currentRingtone=RingtoneManager.getRingtone(mContext,currentRintoneUri);
当前铃声。播放();
打破
}
}
公共无效playRingtoneLM(){
AudioManager AudioManager=(AudioManager)mContext.getSystemService(Context.AUDIO_服务);
audioManager.setStreamVolume(audioManager.STREAM\u RING,audioManager.getStreamMaxVolume(audioManager.STREAM\u RING),audioManager.FLAG\u ALLOW\u RINGER\u MODES | audioManager.FLAG\u PLAY\u SOUND);
int currentVolume=audioManager.getStreamVolume(audioManager.STREAM\u环);
int max=audioManager.getStreamMaxVolume(audioManager.STREAM_通知);
audioManager.setRingerMode(audioManager.RINGER\u模式\u正常);
audioManager.setStreamVolume(audioManager.STREAM\u环,最大值,audioManager.FLAG\u移除\u声音\u和\u振动);
mPlayer=新媒体播放器();
mPlayer.setAudioStreamType(AudioManager.STREAM_环);
试一试{
mPlayer.setDataSource(mContext,
parse(“android.resource://”+mContext.getPackageName()+“/”+R.raw.phone_loud1));
mPlayer.prepare();
}捕获(IOE异常){
Log.e(Log_标签,“无法为铃声设置媒体播放器”);
mPlayer=null;
返回;
}
mPlayer.setLooping(true);
mPlayer.start();
}
公共void stoplingtonelm(){
if(mPlayer!=null){
mPlayer.stop();
mPlayer.release();
mPlayer=null;
}
}
公共电话铃声(){
如果(currentRingtone!=null){
currentRingtone.stop();
currentRingtone=null;
}
}
公共空间playProgressTone(){
stopProgressTone();
试一试{
mProgressTone=createProgressTone(mContext);
mProgressTone.play();
}捕获(例外e){
Log.e(日志标签“无法播放进度音”,e);
}
}
公共停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场{
if(mProgressTone!=null){
mProgressTone.stop();
mProgressTone.release();
mProgressTone=null;
}
}
私有静态AudioTrack createProgressTone(上下文上下文)引发IOException{
AssetFileDescriptor fd=context.getResources().openRawResourceFd(R.raw.progress\u tone);
int length=(int)fd.getLength();
AudioTrack AudioTrack=新的AudioTrack(AudioManager.STREAM\u语音呼叫、采样率、,
AudioFormat.CHANNEL\u OUT\u MONO、AudioFormat.ENCODING\u PCM\u 16位、长度、AudioTrack.MODE\u STATIC);
字节[]数据=新字节[长度];
readFileToBytes(fd,数据);
音频轨。写入(数据,0,数据。长度);
音轨.设定点(0,data.length/2,30);
返回音轨;
}
私有静态void readFileToBytes(AssetFileDescriptor fd,字节[]数据)引发IOException{
FileInputStream inputStream=fd.createInputStream();
int字节读取=0;
while(字节读取<数据长度){
int res=inputStream.read(data,bytesRead,(data.length-bytesRead));
如果(res==-1){
打破
}
字节读取+=res;
}
}
}