Created
May 18, 2020 04:47
-
-
Save Feroz-Istar/bf2f9353fe5964275b2065df41b8abfb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Service; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.media.AudioAttributes; | |
import android.media.AudioFocusRequest; | |
import android.media.AudioManager; | |
import android.media.MediaPlayer; | |
import android.media.ToneGenerator; | |
import android.os.Build; | |
import android.os.Handler; | |
import android.os.IBinder; | |
import android.os.Looper; | |
import android.util.Log; | |
import salesken.ai.R; | |
public class PlayToneService extends Service implements AudioManager.OnAudioFocusChangeListener { | |
private static final String TAG = "PlayToneService"; | |
ToneGenerator tg; | |
MediaPlayer mPlayer ; | |
public PlayToneService() { | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
mPlayer = MediaPlayer.create(PlayToneService.this, R.raw.call_end); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
mPlayer.setAudioAttributes( | |
new AudioAttributes | |
.Builder() | |
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) | |
.build()); | |
}else { | |
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); | |
} | |
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); | |
int result; | |
Log.d(TAG,"AudioManager.requestAudioFocus..."); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
result= audioManager.requestAudioFocus(new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) | |
.setAudioAttributes( | |
new AudioAttributes.Builder() | |
.setUsage(AudioAttributes.USAGE_MEDIA) | |
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) | |
.build() | |
) | |
.setAcceptsDelayedFocusGain(true) | |
.setOnAudioFocusChangeListener(this).build() | |
); | |
} else { | |
result= audioManager.requestAudioFocus(this, | |
AudioManager.STREAM_MUSIC, | |
AudioManager.AUDIOFOCUS_GAIN); | |
} | |
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { | |
Log.d(TAG,"AUDIOFOCUS_REQUEST_GRANTED Granted bhai"); | |
/* tg= new ToneGenerator(AudioManager.STREAM_ALARM, 100); | |
tg.startTone(ToneGenerator.TONE_PROP_BEEP);*/ | |
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { | |
@Override | |
public void onCompletion(MediaPlayer mp) { | |
mp.release(); | |
stopSelf(); | |
} | |
}); | |
mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { | |
@Override | |
public boolean onError(MediaPlayer mp, int what, int extra) { | |
mp.release(); | |
stopSelf(); | |
return false; | |
} | |
}); | |
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
mPlayer.stop(); | |
} | |
},1000); | |
mPlayer.setLooping(true); | |
mPlayer.start(); | |
} else { | |
// Audio Focus Gain failed | |
Log.d(TAG,"Not Granted bhai"); | |
stopSelf(); | |
} | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
// We don't want this service to continue running if it is explicitly | |
// stopped, so return not sticky. | |
return START_NOT_STICKY; | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
// TODO: Return the communication channel to the service. | |
throw new UnsupportedOperationException("Not yet implemented"); | |
} | |
@Override | |
public void onAudioFocusChange(int focusChange) { | |
Log.d(TAG,"focusChange ..."+focusChange); | |
if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { | |
// You now have the audio focus and may play sound. | |
Log.d(TAG,"Granted bhai"); | |
Log.d(TAG,"AudioManager.AUDIOFOCUS_REQUEST_Sucess..."); | |
} | |
else if (focusChange == AudioManager.AUDIOFOCUS_REQUEST_FAILED) { | |
// Handle the failure. | |
Log.d(TAG,"AudioManager.AUDIOFOCUS_REQUEST_FAILED ..."); | |
stopSelf(); | |
} | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
if(mPlayer != null){ | |
mPlayer.release(); | |
mPlayer=null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment