Example usage for android.content Context AUDIO_SERVICE

List of usage examples for android.content Context AUDIO_SERVICE

Introduction

In this page you can find the example usage for android.content Context AUDIO_SERVICE.

Prototype

String AUDIO_SERVICE

To view the source code for android.content Context AUDIO_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.media.AudioManager for handling management of volume, ringer modes and audio routing.

Usage

From source file:info.bartowski.easteregg.MLand.java

public MLand(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    setFocusable(true);/*from w  ww. j a  va  2  s  . c  o  m*/
    PARAMS = new Params(getResources());
    mTimeOfDay = irand(0, SKIES.length - 1);
    mScene = irand(0, SCENE_COUNT);

    mTouchPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mTouchPaint.setColor(0x80FFFFFF);
    mTouchPaint.setStyle(Paint.Style.FILL);

    mPlayerTracePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPlayerTracePaint.setColor(0x80FFFFFF);
    mPlayerTracePaint.setStyle(Paint.Style.STROKE);
    mPlayerTracePaint.setStrokeWidth(2 * dp);

    // we assume everything will be laid out left|top
    setLayoutDirection(LAYOUT_DIRECTION_LTR);

    setupPlayers(DEFAULT_PLAYERS);

}

From source file:jp.sonymusicstudio.cast.castcompanionlibrary.cast.VideoCastManager.java

protected VideoCastManager(Context context, String applicationId, Class<?> targetActivity,
        String dataNamespace) {//from w w  w. ja v a2 s  . c om
    super(context, applicationId);
    LOGD(TAG, "VideoCastManager is instantiated");
    mDataNamespace = dataNamespace;
    if (targetActivity == null) {
        targetActivity = VideoCastControllerActivity.class;
    }
    mTargetActivity = targetActivity;
    mPreferenceAccessor.saveStringToPreference(PREFS_KEY_CAST_ACTIVITY_NAME, mTargetActivity.getName());
    if (!TextUtils.isEmpty(mDataNamespace)) {
        mPreferenceAccessor.saveStringToPreference(PREFS_KEY_CAST_CUSTOM_DATA_NAMESPACE, dataNamespace);
    }
    mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
}

From source file:org.botlibre.sdk.activity.MicConfiguration.java

private void setStreamVolume() {
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    if (volume != 0) {
        Log.d("ChatActivity", "The volume changed and saved to : " + volume);
        MainActivity.volume = volume;/*  w  w  w. ja v  a  2  s  .c om*/
    }
}

From source file:io.github.carlorodriguez.morningritual.MainActivity.java

private void notifyStepFinish() {
    if (vibrateWhenStepFinish()) {
        ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(500);
    }//from  w  w  w .  j  av  a 2 s.co m

    if (soundWhenStepFinish()) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION).build();

            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

            MediaPlayer.create(this, R.raw.sound, audioAttributes, audioManager.generateAudioSessionId())
                    .start();
        } else {
            try {
                playLegacyNotificationSound();
            } catch (IOException e) {
                Toast.makeText(MainActivity.this, R.string.cannot_load_notification_sound, Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
}

From source file:com.android.onemedia.playback.LocalRenderer.java

public LocalRenderer(Context context, Bundle params) {
    super(context, params);
    mContext = context;
    mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}

From source file:ca.mudar.snoozy.receiver.PowerConnectionReceiver.java

private void nativeRingtone(Context context, boolean hasSound) {
    if (hasSound) {
        final AudioManager.OnAudioFocusChangeListener listener = this;
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        int result = audioManager.requestAudioFocus(listener, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {

            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            mRingtone = RingtoneManager.getRingtone(context, notification);
            mRingtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
            mRingtone.play();/* ww w.ja  v a  2s . c o m*/

            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    audioManager.abandonAudioFocus(listener);
                }
            }, AUDIO_FOCUS_DURATION);
        }
    }
}

From source file:com.google.android.car.kitchensink.audio.AudioTestFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
    Log.i(TAG, "onCreateView");
    init();/*  w  ww . j  a va  2  s.co  m*/
    View view = inflater.inflate(R.layout.audio, container, false);
    mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    mAudioFocusHandler = new FocusHandler((RadioGroup) view.findViewById(R.id.button_focus_request_selection),
            (Button) view.findViewById(R.id.button_audio_focus_request),
            (TextView) view.findViewById(R.id.text_audio_focus_state));
    mMediaPlay = (Button) view.findViewById(R.id.button_media_play_start);
    mMediaPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean requestFocus = true;
            boolean repeat = true;
            mMusicPlayer.start(requestFocus, repeat, AudioManager.AUDIOFOCUS_GAIN);
        }
    });
    mMediaPlayOnce = (Button) view.findViewById(R.id.button_media_play_once);
    mMediaPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mMusicPlayerShort.start(true, false, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
            // play only for 1 sec and stop
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mMusicPlayerShort.stop();
                }
            }, 1000);
        }
    });
    mMediaStop = (Button) view.findViewById(R.id.button_media_play_stop);
    mMediaStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mMusicPlayer.stop();
        }
    });
    mWavPlay = (Button) view.findViewById(R.id.button_wav_play_start);
    mWavPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mWavPlayer.start(true, true, AudioManager.AUDIOFOCUS_GAIN);
        }
    });
    mWavStop = (Button) view.findViewById(R.id.button_wav_play_stop);
    mWavStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mWavPlayer.stop();
        }
    });
    mNavPlayOnce = (Button) view.findViewById(R.id.button_nav_play_once);
    mNavPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mAppFocusManager == null) {
                return;
            }
            if (DBG) {
                Log.i(TAG, "Nav start");
            }
            if (!mNavGuidancePlayer.isPlaying()) {
                try {
                    mAppFocusManager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION,
                            mOwnershipCallbacks);
                } catch (CarNotConnectedException e) {
                    Log.e(TAG, "Failed to set active focus", e);
                }
                mNavGuidancePlayer.start(true, false, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK,
                        new PlayStateListener() {
                            @Override
                            public void onCompletion() {
                                mAppFocusManager.abandonAppFocus(mOwnershipCallbacks,
                                        CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
                            }
                        });
            }
        }
    });
    mVrPlayOnce = (Button) view.findViewById(R.id.button_vr_play_once);
    mVrPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mAppFocusManager == null) {
                return;
            }
            if (DBG) {
                Log.i(TAG, "VR start");
            }
            try {
                mAppFocusManager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND,
                        mOwnershipCallbacks);
            } catch (CarNotConnectedException e) {
                Log.e(TAG, "Failed to set active focus", e);
            }
            if (!mVrPlayer.isPlaying()) {
                mVrPlayer.start(true, false, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, new PlayStateListener() {
                    @Override
                    public void onCompletion() {
                        mAppFocusManager.abandonAppFocus(mOwnershipCallbacks,
                                CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND);
                    }
                });
            }
        }
    });
    mSystemPlayOnce = (Button) view.findViewById(R.id.button_system_play_once);
    mSystemPlayOnce.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (DBG) {
                Log.i(TAG, "System start");
            }
            if (!mSystemPlayer.isPlaying()) {
                // system sound played without focus
                mSystemPlayer.start(false, false, 0);
            }
        }
    });
    mNavStart = (Button) view.findViewById(R.id.button_nav_start);
    mNavStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleNavStart();
        }
    });
    mNavEnd = (Button) view.findViewById(R.id.button_nav_end);
    mNavEnd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleNavEnd();
        }
    });
    mVrStart = (Button) view.findViewById(R.id.button_vr_start);
    mVrStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleVrStart();
        }
    });
    mVrEnd = (Button) view.findViewById(R.id.button_vr_end);
    mVrEnd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleVrEnd();
        }
    });
    mRadioStart = (Button) view.findViewById(R.id.button_radio_start);
    mRadioStart.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleRadioStart();
        }
    });
    mRadioEnd = (Button) view.findViewById(R.id.button_radio_end);
    mRadioEnd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            handleRadioEnd();
        }
    });
    mSpeakerPhoneOn = (Button) view.findViewById(R.id.button_speaker_phone_on);
    mSpeakerPhoneOn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setSpeakerphoneOn(true);
        }
    });
    mSpeakerPhoneOff = (Button) view.findViewById(R.id.button_speaker_phone_off);
    mSpeakerPhoneOff.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setSpeakerphoneOn(false);
        }
    });
    mMicrophoneOn = (Button) view.findViewById(R.id.button_microphone_on);
    mMicrophoneOn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setMicrophoneMute(false); // Turn the microphone on.
        }
    });
    mMicrophoneOff = (Button) view.findViewById(R.id.button_microphone_off);
    mMicrophoneOff.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAudioManager.setMicrophoneMute(true); // Mute the microphone.
        }
    });

    mRejectFocus = (ToggleButton) view.findViewById(R.id.button_reject_audio_focus);
    mRejectFocus.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mCarEmulator == null) {
                return;
            }
            if (!mEnableMocking.isChecked()) {
                return;
            }
            if (isChecked) {
                mCarEmulator.setAudioFocusControl(true);
            } else {
                mCarEmulator.setAudioFocusControl(false);
            }
        }
    });
    mRejectFocus.setActivated(false);
    mEnableMocking = (ToggleButton) view.findViewById(R.id.button_mock_audio);
    mEnableMocking.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (mCarEmulator == null) {
                //TODO(pavelm): need to do a full switch between emulated and normal mode
                // all Car*Manager references should be invalidated.
                Toast.makeText(AudioTestFragment.this.getContext(), "Not supported yet :(", Toast.LENGTH_SHORT)
                        .show();
                return;
            }
            if (isChecked) {
                mRejectFocus.setActivated(true);
                mCarEmulator.start();
            } else {
                mRejectFocus.setActivated(false);
                mCarEmulator.stop();
                mCarEmulator = null;
            }
        }
    });
    mMuteMedia = (ToggleButton) view.findViewById(R.id.button_mute_media);
    mMuteMedia.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            try {
                if (isChecked) {
                    mCarAudioManager.setMediaMute(true);
                } else {
                    mCarAudioManager.setMediaMute(false);
                }
            } catch (CarNotConnectedException e) {
                //ignore
            }
        }
    });
    return view;
}

From source file:dk.bearware.gui.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String serverName = getIntent().getStringExtra(ServerEntry.KEY_SERVERNAME);
    if ((serverName != null) && !serverName.isEmpty())
        setTitle(serverName);//  ww  w.  j  av a 2  s  .  c  om
    getActionBar().setDisplayHomeAsUpEnabled(true);

    restarting = (savedInstanceState != null);
    accessibilityAssistant = new AccessibilityAssistant(this);
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mediaButtonEventReceiver = new ComponentName(getPackageName(), MediaButtonEventReceiver.class.getName());
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    wakeLock = ((PowerManager) getSystemService(Context.POWER_SERVICE))
            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    wakeLock.setReferenceCounted(false);

    channelsAdapter = new ChannelListAdapter(this.getBaseContext());
    filesAdapter = new FileListAdapter(this, this, accessibilityAssistant);
    textmsgAdapter = new TextMessageAdapter(this.getBaseContext(), accessibilityAssistant);
    mediaAdapter = new MediaAdapter(this.getBaseContext());

    // Create the adapter that will return a fragment for each of the five
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(mSectionsPagerAdapter);

    setupButtons();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        final MediaPlayer mMediaPlayer;
        mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.silence);
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mMediaPlayer.release();
            }
        });
        mMediaPlayer.start();
    }
}

From source file:com.google.android.car.kitchensink.radio.RadioTestFragment.java

private void init() {
    mCar = Car.createCar(getContext(), new ServiceConnection() {
        @Override/*from  ww  w .java  2  s . c  o m*/
        public void onServiceConnected(ComponentName name, IBinder service) {
            try {
                mCarAudioManager = (CarAudioManager) mCar.getCarManager(Car.AUDIO_SERVICE);
                mRadioAudioAttrib = mCarAudioManager
                        .getAudioAttributesForCarUsage(CarAudioManager.CAR_AUDIO_USAGE_RADIO);
            } catch (CarNotConnectedException e) {
                Log.e(TAG, "Car not connected", e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    });
    mCar.connect();
    mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
    initializeRadio();
}

From source file:com.example.android.leanback.MediaSessionService.java

@Override
public void onCreate() {
    super.onCreate();

    // This service can be created for multiple times, the objects will only be created when
    // it is null
    if (mMediaSession == null) {
        mMediaSession = new MediaSessionCompat(this, MUSIC_PLAYER_SESSION_TOKEN);
        mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mMediaSession.setCallback(new MediaSessionCallback());
    }/*from  ww  w  .ja va2s.c o  m*/

    if (mAudioManager == null) {
        // Create audio manager through system service
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    }

    // initialize the player (including activate media session, request audio focus and
    // set up the listener to listen to player's state)
    initializePlayer();
}