Example usage for android.media AudioManager STREAM_MUSIC

List of usage examples for android.media AudioManager STREAM_MUSIC

Introduction

In this page you can find the example usage for android.media AudioManager STREAM_MUSIC.

Prototype

int STREAM_MUSIC

To view the source code for android.media AudioManager STREAM_MUSIC.

Click Source Link

Document

Used to identify the volume of audio streams for music playback

Usage

From source file:dk.dr.radio.afspilning.Afspiller.java

    /** Srg for at volumen er skruet op til en minimumsvrdi, angivet i 5'tedele af fuld styrke */
public void tjekVolumenMindst5tedele(int min5) {
  int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
  int nu = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
  if (nu < min5 * max / 5) {
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, min5 * max / 5, AudioManager.FLAG_SHOW_UI);
  }// w w w .  j  a  v  a2  s.co m
}

From source file:com.rks.musicx.ui.fragments.PlayingViews.Playing3Fragment.java

@Override
protected void function() {
    ateKey = Helper.getATEKey(getContext());
    if (getActivity() == null) {
        return;//from w ww .j  a va 2 s  .co  m
    }
    accentColor = Config.accentColor(getContext(), ateKey);
    getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);
    getActivity().getWindow().setStatusBarColor(accentColor);
    CustomLayoutManager customlayoutmanager = new CustomLayoutManager(getActivity());
    customlayoutmanager.setOrientation(LinearLayoutManager.VERTICAL);
    customlayoutmanager.setSmoothScrollbarEnabled(true);
    queuerv.setLayoutManager(customlayoutmanager);
    queuerv.addItemDecoration(new DividerItemDecoration(getContext(), 75, false));
    queuerv.setHasFixedSize(true);
    queueAdapter = new QueueAdapter(getContext(), this);
    if (Extras.getInstance().getPlayingViewTrack()) {
        queueAdapter.setLayoutId(R.layout.song_list);
    }
    queuerv.setAdapter(queueAdapter);
    queueAdapter.setOnItemClickListener(mOnClick);
    ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(queueAdapter);
    mItemTouchHelper = new ItemTouchHelper(callback);
    mItemTouchHelper.attachToRecyclerView(queuerv);
    moreMenu.setOnClickListener(onClick);
    moreMenu.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_menu));
    favhelper = new FavHelper(getActivity());
    shuffleButton.setOnClickListener(onClick);
    shuffleButton.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.shuf_off));
    repeatButton.setOnClickListener(onClick);
    repeatButton.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.rep_no));
    playpausebutton.setOnClickListener(onClick);
    next.setOnClickListener(onClick);
    prev.setOnClickListener(onClick);
    Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.thumb);
    seekbar.setThumb(drawable);
    favButton.setOnClickListener(onClick);
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
    diagonalLayout.setOnTouchListener(new GestureListerner() {
        @Override
        public void onRightToLeft() {

        }

        @Override
        public void onLeftToRight() {

        }

        @Override
        public void onBottomToTop() {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(@NonNull View bottomSheet, int newState) {
                    if (newState == BottomSheetBehavior.STATE_DRAGGING) {
                        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                    }
                }

                @Override
                public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                }
            });
        }

        @Override
        public void onTopToBottom() {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
        }

        @Override
        public void doubleClick() {
            if (getActivity() == null) {
                return;
            }
            ((PlayingActivity) getActivity()).onBackPressed();
        }

        @Override
        public void singleClick() {

        }

        @Override
        public void otherFunction() {

        }
    });
    if (Extras.getInstance().getDarkTheme() || Extras.getInstance().getBlackTheme()) {
        bottomsheetLyrics.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.MaterialGrey));
        lrcView.setTextColor(Color.WHITE);
    } else {
        bottomsheetLyrics.setBackgroundColor(Color.WHITE);
        lrcView.setTextColor(ContextCompat.getColor(getContext(), R.color.MaterialGrey));
    }
    /**
     * Show Case
     */
    ShowcaseConfig config = new ShowcaseConfig();
    config.setDelay(500);
    config.setShapePadding(20);
    MaterialShowcaseSequence sequence = new MaterialShowcaseSequence(getActivity(), "600");
    sequence.setConfig(config);
    sequence.addSequenceItem(queuerv, "Drag ,Drop to change queue, Slide right to remove song", "GOT IT");
    sequence.addSequenceItem(lrcView, "slide up/down to show/hide view  available lyrics", "GOT IT");
    sequence.setOnItemDismissedListener(new MaterialShowcaseSequence.OnSequenceItemDismissedListener() {
        @Override
        public void onDismiss(MaterialShowcaseView materialShowcaseView, int i) {
            config.setDelay(1000);
        }
    });
    sequence.start();
    helper = new Helper(getContext());
}

From source file:CookieVideoView.java

private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;//from www  . j a  v a 2s. c  om
    }
    // Tell the music playback service to pause
    // TODO: these constants need to be published somewhere in the framework.
    Intent i = new Intent("com.android.music.musicservicecommand");
    i.putExtra("command", "pause");
    mContext.sendBroadcast(i);

    // we shouldn't clear the target state, because somebody might have
    // called start() previously
    release(false);
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
        mMediaPlayer.setOnInfoListener(mOnInfoListener);
        mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        mMediaPlayer.setDataSource(mContext, mUri, mHeaders);
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.prepareAsync();
        // we don't set the target state here either, but preserve the
        // target state that was there before.
        mCurrentState = STATE_PREPARING;
        attachMediaController();
    } catch (IOException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    } catch (IllegalArgumentException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    }
}

From source file:com.rks.musicx.services.MusicXService.java

private void initMediaData() {
    try {//  ww  w  .  j  a  va2s.  c  o m
        MediaPlayerSingleton.getInstance().getMediaPlayer();
        MediaPlayerSingleton.getInstance().getMediaPlayer().setOnPreparedListener(this);
        MediaPlayerSingleton.getInstance().getMediaPlayer().setOnCompletionListener(this);
        MediaPlayerSingleton.getInstance().getMediaPlayer().setOnErrorListener(this);
        MediaPlayerSingleton.getInstance().getMediaPlayer().setAudioStreamType(AudioManager.STREAM_MUSIC);
        MediaPlayerSingleton.getInstance().getMediaPlayer().setWakeMode(this, PowerManager.PARTIAL_WAKE_LOCK);
        Log.d(TAG, "MediaInit");
    } catch (Exception e) {
        Log.d(TAG, "initMedia_error", e);
    }
}

From source file:com.grupohqh.carservices.operator.ReadTagActivity.java

private void readTag() {
    runOnUiThread(new Runnable() {
        int scantimes = 25;
        String tagId;//from   w ww.  j a va2  s  .co  m
        ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);

        @Override
        public void run() {
            for (int i = 0; i < scantimes; i++) {
                MtiCmd mtiCmd = new CMD_Iso18k6cTagAccess.RFID_18K6CTagInventory(usbCommunication);
                CMD_Iso18k6cTagAccess.RFID_18K6CTagInventory finalCmd = (CMD_Iso18k6cTagAccess.RFID_18K6CTagInventory) mtiCmd;
                if (finalCmd.setCmd(CMD_Iso18k6cTagAccess.Action.StartInventory)) {
                    tagId = finalCmd.getTagId();
                    if (finalCmd.getTagNumber() > 0) {
                        tg.startTone(ToneGenerator.TONE_PROP_BEEP);
                        etEpc.setText(tagId);
                        break;
                    }
                }
            }
            if (!etEpc.getText().toString().equals(""))
                new HttpAsyncTask().execute(URL);
            else
                etEpc.setHint("No se encontro ningun TAG");
        }
    });
}

From source file:com.darly.im.ui.CCPActivityBase.java

/**
 *
 * @param keyCode/*from w w w. j ava  2 s.c o m*/
 * @param event
 * @return
 */
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) && mAudioManager != null) {
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        if (streamVolume >= mMusicMaxVolume) {
            LogUtil.d(LogUtil.getLogUtilsTag(BaseFragment.class), "has set the max volume");
            return true;
        }

        int mean = mMusicMaxVolume / 7;
        if (mean == 0) {
            mean = 1;
        }

        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume + mean,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
    }
    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) && mAudioManager != null) {
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int mean = mMusicMaxVolume / 7;
        if (mean == 0) {
            mean = 1;
        }

        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume - mean,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    }
    return false;
}

From source file:com.example.android.uamp.playback.LocalPlayback.java

/**
 * Try to get the system audio focus.//from w  w w  . j a  v a2  s .  co m
 */
private void tryToGetAudioFocus() {
    LogHelper.d(TAG, "tryToGetAudioFocus");
    int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        mAudioFocus = AUDIO_FOCUSED;
    } else {
        mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK;
    }
}

From source file:org.connectbot.ConsoleFragment.java

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

    clipboard = (ClipboardManager) getActivity().getSystemService(Activity.CLIPBOARD_SERVICE);
    prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

    // hide status bar if requested by user
    /*if (prefs.getBoolean(PreferenceConstants.FULLSCREEN, false)) {
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
       WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }*///  ww  w .  java 2  s . c om

    // TODO find proper way to disable volume key beep if it exists.
    getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);

    // handle requested console from incoming intent
    requested = getActivity().getIntent().getData();

    setHasOptionsMenu(true);
}

From source file:com.wordpress.httpstheredefiningproductions.phonefinder.recorder.java

@Override
public void onDestroy() {
    super.onDestroy();
    //get rid of any mute
    mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
    //stop all listening
    if (mIsCountDownOn) {
        mNoSpeechCountDown.cancel();/*from w  ww  .j  a  va 2  s .  c  om*/
    }
    if (mSpeechRecognizer != null) {
        mSpeechRecognizer.destroy();
    }
}

From source file:github.daneren2005.dsub.service.DownloadService.java

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

    final SharedPreferences prefs = Util.getPreferences(this);
    new Thread(new Runnable() {
        public void run() {
            Looper.prepare();//from  w  ww  . j av  a  2s .c o  m

            mediaPlayer = new MediaPlayer();
            mediaPlayer.setWakeMode(DownloadService.this, PowerManager.PARTIAL_WAKE_LOCK);

            audioSessionId = -1;
            Integer id = prefs.getInt(Constants.CACHE_AUDIO_SESSION_ID, -1);
            if (id != -1) {
                try {
                    audioSessionId = id;
                    mediaPlayer.setAudioSessionId(audioSessionId);
                } catch (Throwable e) {
                    audioSessionId = -1;
                }
            }

            if (audioSessionId == -1) {
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    audioSessionId = mediaPlayer.getAudioSessionId();
                    prefs.edit().putInt(Constants.CACHE_AUDIO_SESSION_ID, audioSessionId).commit();
                } catch (Throwable t) {
                    // Froyo or lower
                }
            }

            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mediaPlayer, int what, int more) {
                    handleError(new Exception("MediaPlayer error: " + what + " (" + more + ")"));
                    return false;
                }
            });

            try {
                Intent i = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
                i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, audioSessionId);
                i.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
                sendBroadcast(i);
            } catch (Throwable e) {
                // Froyo or lower
            }

            effectsController = new AudioEffectsController(DownloadService.this, audioSessionId);
            if (prefs.getBoolean(Constants.PREFERENCES_EQUALIZER_ON, false)) {
                getEqualizerController();
            }

            mediaPlayerLooper = Looper.myLooper();
            mediaPlayerHandler = new Handler(mediaPlayerLooper);

            if (runListenersOnInit) {
                onSongsChanged();
                onSongProgress();
                onStateUpdate();
            }

            Looper.loop();
        }
    }, "DownloadService").start();

    Util.registerMediaButtonEventReceiver(this);

    if (mRemoteControl == null) {
        // Use the remote control APIs (if available) to set the playback state
        mRemoteControl = RemoteControlClientHelper.createInstance();
        ComponentName mediaButtonReceiverComponent = new ComponentName(getPackageName(),
                MediaButtonIntentReceiver.class.getName());
        mRemoteControl.register(this, mediaButtonReceiverComponent);
    }

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());
    wakeLock.setReferenceCounted(false);

    try {
        timerDuration = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_SLEEP_TIMER_DURATION, "5"));
    } catch (Throwable e) {
        timerDuration = 5;
    }
    sleepTimer = null;

    keepScreenOn = prefs.getBoolean(Constants.PREFERENCES_KEY_KEEP_SCREEN_ON, false);

    mediaRouter = new MediaRouteManager(this);

    instance = this;
    shufflePlayBuffer = new ShufflePlayBuffer(this);
    artistRadioBuffer = new ArtistRadioBuffer(this);
    lifecycleSupport.onCreate();
}