本文整理匯總了Java中android.media.AudioAttributes類的典型用法代碼示例。如果您正苦於以下問題:Java AudioAttributes類的具體用法?Java AudioAttributes怎麽用?Java AudioAttributes使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
AudioAttributes類屬於android.media包,在下文中一共展示了AudioAttributes類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: requestAudioFocus
import android.media.AudioAttributes; //導入依賴的package包/類
public boolean requestAudioFocus(OnAudioFocusChangeListener focusChangeListener,
int streamType, int audioFocusGain) {
int r;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
r = mAudioManager.requestAudioFocus(
new AudioFocusRequest.Builder(audioFocusGain)
.setAudioAttributes(
new AudioAttributes.Builder()
.setLegacyStreamType(streamType)
.build())
.setOnAudioFocusChangeListener(focusChangeListener)
.build());
} else {
//noinspection deprecation
r = mAudioManager.requestAudioFocus(focusChangeListener, streamType, audioFocusGain);
}
return r == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}
示例2: loadSounds
import android.media.AudioAttributes; //導入依賴的package包/類
protected void loadSounds()
{
if (null != soundPool) {
return;
}
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
soundPool = new SoundPool.Builder()
.setMaxStreams(clickers.length + 1)
.setAudioAttributes(audioAttributes)
.build();
for (int i = 0; i < clickers.length; ++i) {
clickers[i].clickSound = soundPool.load(this, clickers[i].soundId, 1);
}
failSound = soundPool.load(this, R.raw.fail, 1);
successSound = soundPool.load(this, R.raw.success, 1);
}
示例3: onCreate
import android.media.AudioAttributes; //導入依賴的package包/類
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_DEFAULT_CHANNEL_ID, getString(R.string.notification_channel_name), NotificationManagerCompat.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(audioFocusChangeListener)
.setAcceptsDelayedFocusGain(false)
.setWillPauseWhenDucked(true)
.setAudioAttributes(audioAttributes)
.build();
}
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mediaSession = new MediaSessionCompat(this, "PlayerService");
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setCallback(mediaSessionCallback);
Context appContext = getApplicationContext();
Intent activityIntent = new Intent(appContext, MainActivity.class);
mediaSession.setSessionActivity(PendingIntent.getActivity(appContext, 0, activityIntent, 0));
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null, appContext, MediaButtonReceiver.class);
mediaSession.setMediaButtonReceiver(PendingIntent.getBroadcast(appContext, 0, mediaButtonIntent, 0));
exoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector(), new DefaultLoadControl());
exoPlayer.addListener(exoPlayerListener);
DataSource.Factory httpDataSourceFactory = new OkHttpDataSourceFactory(new OkHttpClient(), Util.getUserAgent(this, getString(R.string.app_name)), null);
Cache cache = new SimpleCache(new File(this.getCacheDir().getAbsolutePath() + "/exoplayer"), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 100)); // 100 Mb max
this.dataSourceFactory = new CacheDataSourceFactory(cache, httpDataSourceFactory, CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);
this.extractorsFactory = new DefaultExtractorsFactory();
}
示例4: initNotificationManager
import android.media.AudioAttributes; //導入依賴的package包/類
private void initNotificationManager() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final Stopwatch watch = Stopwatch.createStarted();
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final NotificationChannel received = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID_RECEIVED,
getString(R.string.notification_channel_received_name), NotificationManager.IMPORTANCE_DEFAULT);
received.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.coins_received),
new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT).build());
nm.createNotificationChannel(received);
final NotificationChannel ongoing = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID_ONGOING,
getString(R.string.notification_channel_ongoing_name), NotificationManager.IMPORTANCE_LOW);
nm.createNotificationChannel(ongoing);
final NotificationChannel important = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID_IMPORTANT,
getString(R.string.notification_channel_important_name), NotificationManager.IMPORTANCE_HIGH);
nm.createNotificationChannel(important);
log.info("created notification channels, took {}", watch);
}
}
示例5: requestAudioFocus
import android.media.AudioAttributes; //導入依賴的package包/類
private void requestAudioFocus() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes playbackAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
AudioFocusRequest focusRequest =
new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
.setAudioAttributes(playbackAttributes)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(
new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int i) { }
})
.build();
audioManager.requestAudioFocus(focusRequest);
} else {
audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
}
}
示例6: CreateChannel
import android.media.AudioAttributes; //導入依賴的package包/類
public static void CreateChannel(String identifier, String name, String description, int importance, String soundName, int enableLights, int lightColor, int enableVibration, long[] vibrationPattern, String bundle) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
return;
channels.add(identifier);
NotificationManager nm = (NotificationManager) UnityPlayer.currentActivity.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(identifier, name, importance);
channel.setDescription(description);
if (soundName != null) {
Resources res = UnityPlayer.currentActivity.getResources();
int id = res.getIdentifier("raw/" + soundName, null, UnityPlayer.currentActivity.getPackageName());
AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build();
channel.setSound(Uri.parse("android.resource://" + bundle + "/" + id), audioAttributes);
}
channel.enableLights(enableLights == 1);
channel.setLightColor(lightColor);
channel.enableVibration(enableVibration == 1);
if (vibrationPattern == null)
vibrationPattern = new long[] { 1000L, 1000L };
channel.setVibrationPattern(vibrationPattern);
nm.createNotificationChannel(channel);
}
示例7: mediaPlayAudioFile
import android.media.AudioAttributes; //導入依賴的package包/類
@Rpc(description = "Play an audio file stored at a specified file path in external storage.")
public void mediaPlayAudioFile(String mediaFilePath) throws IOException {
mediaReset();
if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
mPlayer.setAudioAttributes(
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build());
} else {
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
mPlayer.setDataSource(mediaFilePath);
mPlayer.prepare(); // Synchronous call blocks until the player is ready for playback.
mPlayer.start();
}
示例8: maybePlaySound
import android.media.AudioAttributes; //導入依賴的package包/類
private void maybePlaySound() {
if (mSoundEnabled &&
(!mPowerManager.isInteractive() || !mSoundWhenScreenOffOnly)) {
try {
final Ringtone sfx = RingtoneManager.getRingtone(mContext,
Uri.parse(mSoundUri));
if (sfx != null) {
AudioAttributes attrs = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
sfx.setAudioAttributes(attrs);
sfx.play();
}
} catch (Throwable t) {
XposedBridge.log(t);
}
}
}
示例9: onCreateView
import android.media.AudioAttributes; //導入依賴的package包/類
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_shoot, null);
findview(v);
mUIHandler = new Handler(new InnerCallBack());
//初始化拍照的聲音
ringtone = RingtoneManager.getRingtone(getActivity(), Uri.parse
("file:///system/media/audio/ui/camera_click.ogg"));
AudioAttributes.Builder attr = new AudioAttributes.Builder();
attr.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION);
ringtone.setAudioAttributes(attr.build());
//初始化相機布局
mTextureView.setSurfaceTextureListener(mSurfacetextlistener);
mTextureView.setOnTouchListener(textTureOntuchListener);
return v;
}
示例10: createHwAvSyncAudioTrackV21
import android.media.AudioAttributes; //導入依賴的package包/類
/**
* Instantiates an {@link android.media.AudioTrack} to be used with tunneling video playback.
*/
@TargetApi(21)
private static android.media.AudioTrack createHwAvSyncAudioTrackV21(int sampleRate,
int channelConfig, int encoding, int bufferSize, int sessionId) {
AudioAttributes attributesBuilder = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MOVIE)
.setFlags(AudioAttributes.FLAG_HW_AV_SYNC)
.build();
AudioFormat format = new AudioFormat.Builder()
.setChannelMask(channelConfig)
.setEncoding(encoding)
.setSampleRate(sampleRate)
.build();
return new android.media.AudioTrack(attributesBuilder, format, bufferSize, MODE_STREAM,
sessionId);
}
示例11: createNotificationChannel
import android.media.AudioAttributes; //導入依賴的package包/類
@RequiresApi(api = Build.VERSION_CODES.O)
void createNotificationChannel(Context context) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence name = MobiComKitConstants.PUSH_NOTIFICATION_NAME;
;
int importance = NotificationManager.IMPORTANCE_HIGH;
if (mNotificationManager.getNotificationChannel(MobiComKitConstants.AL_PUSH_NOTIFICATION) == null) {
NotificationChannel mChannel = new NotificationChannel(MobiComKitConstants.AL_PUSH_NOTIFICATION, name, importance);
mChannel.enableLights(true);
mChannel.setLightColor(Color.GREEN);
if (ApplozicClient.getInstance(context).isUnreadCountBadgeEnabled()) {
mChannel.setShowBadge(true);
} else {
mChannel.setShowBadge(false);
}
if (ApplozicClient.getInstance(context).getVibrationOnNotification()) {
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
}
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build();
mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), audioAttributes);
mNotificationManager.createNotificationChannel(mChannel);
}
}
示例12: setAudioFocus
import android.media.AudioAttributes; //導入依賴的package包/類
private void setAudioFocus(boolean setFocus) {
if (audioManager != null) {
if (setFocus) {
savedAudioMode = audioManager.getMode();
// Request audio focus before making any device switch.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes playbackAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
AudioFocusRequest focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
.setAudioAttributes(playbackAttributes)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int i) {
}
})
.build();
audioManager.requestAudioFocus(focusRequest);
} else {
audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
}
/*
* Start by setting MODE_IN_COMMUNICATION as default audio mode. It is
* required to be in this mode when playout and/or recording starts for
* best possible VoIP performance. Some devices have difficulties with speaker mode
* if this is not set.
*/
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
} else {
audioManager.setMode(savedAudioMode);
audioManager.abandonAudioFocus(null);
}
}
}
示例13: calibrationNotificationCreate
import android.media.AudioAttributes; //導入依賴的package包/類
private void calibrationNotificationCreate(String title, String content, Intent intent, int notificationId) {
NotificationCompat.Builder mBuilder = notificationBuilder(title, content, intent, NotificationChannels.CALIBRATION_CHANNEL);
mBuilder.setVisibility(Pref.getBooleanDefaultFalse("public_notifications") ? Notification.VISIBILITY_PUBLIC : Notification.VISIBILITY_PRIVATE);
mBuilder.setVibrate(vibratePattern);
mBuilder.setLights(0xff00ff00, 300, 1000);
if(calibration_override_silent) {
mBuilder.setSound(Uri.parse(calibration_notification_sound), AudioAttributes.USAGE_ALARM);
} else {
mBuilder.setSound(Uri.parse(calibration_notification_sound));
}
NotificationManager mNotifyMgr = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
//mNotifyMgr.cancel(notificationId);
mNotifyMgr.notify(notificationId, XdripNotificationCompat.build(mBuilder));
}
示例14: AndroidAudioPlayer
import android.media.AudioAttributes; //導入依賴的package包/類
public AndroidAudioPlayer(final AudioConfig audioConfig) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setLegacyStreamType(AudioManager.STREAM_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
AudioFormat audioFormat = new AudioFormat.Builder()
.setChannelMask(audioConfig.getOutputChannel())
.setEncoding(audioConfig.getOutputFormat())
.setSampleRate(audioConfig.getSampleRate())
.build();
audioTrack = new AudioTrack(audioAttributes,
audioFormat,
audioConfig.getOutputBufferSize(),
AudioTrack.MODE_STATIC,
AudioManager.AUDIO_SESSION_ID_GENERATE);
outputByteCount = audioConfig.getOutputFormatByteCount();
}
示例15: getAudioTrack
import android.media.AudioAttributes; //導入依賴的package包/類
AudioTrack getAudioTrack(long bufferSize) {
AudioFormat audioFormat = getAudioFormat(AudioFormat.CHANNEL_OUT_MONO);
AudioAttributes attributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build();
AudioTrack track = new AudioTrack.Builder()
.setAudioFormat(audioFormat)
.setBufferSizeInBytes((int) bufferSize)
.setAudioAttributes(attributes)
.setTransferMode(AudioTrack.MODE_STREAM)
.build();
PresetReverb reverb = getReverb();
track.attachAuxEffect(reverb.getId());
track.setAuxEffectSendLevel(1.0f);
return track;
}