sinch来电铃声

时间:2016-04-28 08:33:15

标签: java android sinch

警报管理器铃声在kitkat上工作正常,但是当我在Lollipop / Marshmallow上尝试它时,它不能处理sinch来电。 我尝试使用默认铃声和自定义铃声,但它的版本不比kitkat大。

sinch

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;
    }
}

}

0 个答案:

没有答案