Android Audio Focus Listener 未在服务中工作
Android Audio Focus Listener not working in service
我有播放音调服务,我必须等待音频焦点播放音调。
下面是我实现的代码:
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.ToneGenerator;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
public class PlayToneService extends Service implements AudioManager.OnAudioFocusChangeListener {
private static final String TAG = "PlayToneService";
public PlayToneService() {
}
@Override
public void onCreate() {
super.onCreate();
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Log.d(TAG,"AudioManager.requestAudioFocus...");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
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 {
audioManager.requestAudioFocus(this,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
}
}
@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) {
if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// You now have the audio focus and may play sound.
final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 500);
tg.startTone(ToneGenerator.TONE_PROP_BEEP);
Log.d(TAG,"AudioManager.AUDIOFOCUS_REQUEST_Sucess...");
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
public void run() {
tg.release();
stopSelf();
}
},550);
}
else if (focusChange == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
// Handle the failure.
Log.d(TAG,"AudioManager.AUDIOFOCUS_REQUEST_FAILED ...");
stopSelf();
}
}
}
我没有收到任何回调是我的 onAudioFocusChange 侦听器。我的 onAudioFocusChange 监听器中没有打印记录器。
您的 AudioFocusChangeListener 实例不会在向 AudioManager 发出初始请求时调用(例如调用 requestAudioFocus())。使用来自 requestAudioFocus() 的 return 值来确定您是否最初已从系统获得音频焦点。您的更改侦听器应该被调用以关注任何后续更改。
我有播放音调服务,我必须等待音频焦点播放音调。
下面是我实现的代码:
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.ToneGenerator;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
public class PlayToneService extends Service implements AudioManager.OnAudioFocusChangeListener {
private static final String TAG = "PlayToneService";
public PlayToneService() {
}
@Override
public void onCreate() {
super.onCreate();
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Log.d(TAG,"AudioManager.requestAudioFocus...");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
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 {
audioManager.requestAudioFocus(this,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
}
}
@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) {
if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// You now have the audio focus and may play sound.
final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 500);
tg.startTone(ToneGenerator.TONE_PROP_BEEP);
Log.d(TAG,"AudioManager.AUDIOFOCUS_REQUEST_Sucess...");
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
public void run() {
tg.release();
stopSelf();
}
},550);
}
else if (focusChange == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
// Handle the failure.
Log.d(TAG,"AudioManager.AUDIOFOCUS_REQUEST_FAILED ...");
stopSelf();
}
}
}
我没有收到任何回调是我的 onAudioFocusChange 侦听器。我的 onAudioFocusChange 监听器中没有打印记录器。
您的 AudioFocusChangeListener 实例不会在向 AudioManager 发出初始请求时调用(例如调用 requestAudioFocus())。使用来自 requestAudioFocus() 的 return 值来确定您是否最初已从系统获得音频焦点。您的更改侦听器应该被调用以关注任何后续更改。