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:com.example.rttytranslator.Dsp_service.java

public void startAudio() {
    if (!_enableDecoder)
        return;/*ww  w  .j av a 2 s. com*/

    //boolean mic = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MICROPHONE);

    System.out.println("isRecording: " + isRecording);

    if (!isRecording) {
        isRecording = true;

        buffsize = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT);
        buffsize = Math.max(buffsize, 3000);

        mRecorder = new AudioRecord(AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT, buffsize);

        mPlayer = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT, 2 * buffsize, AudioTrack.MODE_STREAM);

        if (enableEcho) {
            AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            manager.setMode(AudioManager.MODE_IN_CALL);
            manager.setSpeakerphoneOn(true);
        }

        if (mRecorder.getState() != AudioRecord.STATE_INITIALIZED) {

            mRecorder = new AudioRecord(AudioSource.DEFAULT, 8000, AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, buffsize);

        }

        mRecorder.startRecording();
        System.out.println("STARTING THREAD");
        Thread ct = new captureThread();

        ct.start();
    }
}

From source file:com.example.android.miwok.activity.FamilyActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.word_list);/*from ww w  .  j  a va2 s .co  m*/

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

    // Create a list of words
    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(new Word("father", "p", R.drawable.family_father, R.raw.family_father));
    words.add(new Word("mother", "a", R.drawable.family_mother, R.raw.family_mother));
    words.add(new Word("son", "angsi", R.drawable.family_son, R.raw.family_son));
    words.add(new Word("daughter", "tune", R.drawable.family_daughter, R.raw.family_daughter));
    words.add(new Word("older brother", "taachi", R.drawable.family_older_brother, R.raw.family_older_brother));
    words.add(new Word("younger brother", "chalitti", R.drawable.family_younger_brother,
            R.raw.family_younger_brother));
    words.add(new Word("older sister", "tee", R.drawable.family_older_sister, R.raw.family_older_sister));
    words.add(new Word("younger sister", "kolliti", R.drawable.family_younger_sister,
            R.raw.family_younger_sister));
    words.add(new Word("grandmother ", "ama", R.drawable.family_grandmother, R.raw.family_grandmother));
    words.add(new Word("grandfather", "paapa", R.drawable.family_grandfather, R.raw.family_grandfather));

    // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
    // adapter knows how to create list items for each item in the list.
    WordAdapter adapter = new WordAdapter(this, words);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // word_list.xml layout file.
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setBackgroundColor(Color.parseColor("#379237")); //<--this is another way to set the background color is it wasn't in colors.xml
    //listView.setBackgroundColor(R.color.category_family);

    // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Word} in the list.
    listView.setAdapter(adapter);

    //plays the audio for number one when any item in the list is clicked click
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            //release current resources being used for media player if it currently exists
            //because we are about to play a different sound file.
            releaseMediaPlayer();

            //get the word located in the list that is at same position as the item clicked in the list
            Word currentWord = words.get(position);

            // Request audio focus for playback
            int result = mAudioManager.requestAudioFocus(mAudioFocusChangeListener,
                    // Use the music stream.
                    AudioManager.STREAM_MUSIC,
                    // Request temporary focus.
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {

                //create the medial player with the audio file that is stored in the list for that word.
                mMediaPlayer = MediaPlayer.create(getApplicationContext(), currentWord.getmMiwokAudio());
                //play the file
                mMediaPlayer.start();

                //listener to stop and release the media player and resources being used
                // once the sounds has finished playing
                mMediaPlayer.setOnCompletionListener(mCompletionListener);
            }
        }
    });
}

From source file:org.amahi.anywhere.service.AudioService.java

private void setUpAudioPlayerRemote() {
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    ComponentName audioReceiver = new ComponentName(getPackageName(), AudioReceiver.class.getName());

    Intent audioIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    audioIntent.setComponent(audioReceiver);
    PendingIntent audioPendingIntent = PendingIntent.getBroadcast(this, 0, audioIntent, 0);

    audioPlayerRemote = new RemoteControlClient(audioPendingIntent);
    audioPlayerRemote.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS);
    audioPlayerRemote.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);

    audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    audioManager.registerMediaButtonEventReceiver(audioReceiver);
    audioManager.registerRemoteControlClient(audioPlayerRemote);
}

From source file:com.jinfukeji.jinyihuiup.indexBannerClick.ZhiboActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zhibo);
    checkWifi();/*from   w ww. j a  va  2  s. c  o m*/
    client = new ExampleClient(URI.create(
            JinYiHuiApplication.URL_BOOT + "ws?id=" + JinYiHuiApplication.getInstace().getUser().getId()),
            this);
    am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    streamVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);//???
    streamMaxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    screen_direction = SCREEN_PORT;
    initView();
    initClickListener();
    setOnClick();
    initVariable();
    client.connect();
    isShowLayout = true;
    new Thread(new Runnable() {
        @Override
        public void run() {
            kk = NetUtil.getK();
            initParam(kk);
            initplayer();
            isPlayed = true;
        }
    }).start();
}

From source file:com.customprogrammingsolutions.MediaStreamer.MediaStreamerService.java

private boolean requestAudioFocus() {
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
        return false;
    else/*from  www . j av  a 2s  .  c o m*/
        return true;
}

From source file:com.ezio.multiwii.Main.PadMainMultiWiiActivity.java

/** Called when the activity is first created. */
@Override//from  w  w w  .  jav a2s  . co m
public void onCreate(Bundle savedInstanceState) {
    Log.d("aaa", "MAIN ON CREATE");
    //requestWindowFeature(Window.FEATURE_PROGRESS);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.pad_main_layout);

    app = (App) getApplication();

    //getSupportActionBar().setDisplayShowTitleEnabled(false);

    /*if (app.AppStartCounter % 10 == 0 && app.DonateButtonPressed == 0) {
       killme = true;
       mHandler.removeCallbacksAndMessages(null);
       startActivity(new Intent(getApplicationContext(), InfoActivity.class));
    }*/

    app.AppStartCounter++;
    app.SaveSettings(true);

    setVolumeControlStream(AudioManager.STREAM_MUSIC);
}

From source file:at.wada811.app.fragment.VideoFragment.java

private void initMediaPlayer() {
    LogUtils.i();/*from   ww w .  ja va2s .  co m*/
    if (getArguments().keySet().contains(KEY_RES_ID)) {
        mMediaPlayer = MediaPlayer.create(getActivity(), getArguments().getInt(KEY_RES_ID));
    } else {
        mMediaPlayer = new MediaPlayer();
    }
    mMediaPlayer.setOnVideoSizeChangedListener(this);
    mMediaPlayer.setOnBufferingUpdateListener(this);
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.setOnCompletionListener(this);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setOnInfoListener(new OnInfoListener() {
        @Override
        public boolean onInfo(MediaPlayer mp, int what, int extra) {
            switch (what) {
            case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING:
                LogUtils.i("MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING");
                break;
            case MediaPlayer.MEDIA_INFO_BUFFERING_END:
                LogUtils.i("MediaPlayer.MEDIA_INFO_BUFFERING_END");
                break;
            case MediaPlayer.MEDIA_INFO_BUFFERING_START:
                LogUtils.i("MediaPlayer.MEDIA_INFO_BUFFERING_START");
                break;
            case MediaPlayer.MEDIA_INFO_METADATA_UPDATE:
                LogUtils.i("MediaPlayer.MEDIA_INFO_METADATA_UPDATE");
                break;
            case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE:
                LogUtils.i("MediaPlayer.MEDIA_INFO_NOT_SEEKABLE");
                break;
            case MediaPlayer.MEDIA_INFO_SUBTITLE_TIMED_OUT:
                LogUtils.i("MediaPlayer.MEDIA_INFO_SUBTITLE_TIMED_OUT");
                break;
            case MediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE:
                LogUtils.i("MediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE");
                break;
            case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
                LogUtils.i("MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START");
                break;
            case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:
                LogUtils.i("MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING");
                break;
            case MediaPlayer.MEDIA_INFO_UNKNOWN:
            default:
                LogUtils.i("MediaPlayer.MEDIA_INFO_UNKNOWN");
                break;
            }
            LogUtils.d("extra: " + extra);
            return false;
        }
    });
    mMediaPlayer.setOnErrorListener(new OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            switch (what) {
            case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
                LogUtils.e("MediaPlayer.MEDIA_ERROR_SERVER_DIED");
                break;
            case MediaPlayer.MEDIA_ERROR_UNKNOWN:
            default:
                LogUtils.e("MediaPlayer.MEDIA_ERROR_UNKNOWN");
                break;
            }
            switch (extra) {
            case MediaPlayer.MEDIA_ERROR_IO:
                LogUtils.e("MediaPlayer.MEDIA_ERROR_IO");
                break;
            case MediaPlayer.MEDIA_ERROR_MALFORMED:
                LogUtils.e("MediaPlayer.MEDIA_ERROR_MALFORMED");
                break;
            case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
                LogUtils.e("MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK");
                break;
            case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
                LogUtils.e("MediaPlayer.MEDIA_ERROR_TIMED_OUT");
                break;
            case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
                LogUtils.e("MediaPlayer.MEDIA_ERROR_UNSUPPORTED");
                break;
            default:
                LogUtils.e("extra: " + extra);
                break;
            }
            return false;
        }
    });
    mCallback.onActivityCreated(this);
}

From source file:fi.mikuz.boarder.gui.DropboxMenu.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dropbox_menu);
    setTitle("Dropbox");
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

    APP_KEY = ApiKeyLoader.loadDropboxApiKey(this.getApplicationContext(), TAG);
    APP_SECRET = ApiKeyLoader.loadDropboxApiSecret(this.getApplicationContext(), TAG);

    AndroidAuthSession session = buildSession();
    mApi = new DropboxAPI<AndroidAuthSession>(session);

    try {//from   w  w  w.ja va 2 s.  co m
        loadCbla();
    } catch (NullPointerException npe) {
    }

    mDropboxContent = (LinearLayout) findViewById(R.id.dropbox_content);
    mAuthDropbox = (Button) findViewById(R.id.auth_button);
    mDropboxDownload = (Button) findViewById(R.id.dropbox_download);
    mDropboxUpload = (Button) findViewById(R.id.dropbox_upload);
    mSelectAll = (Button) findViewById(R.id.select_all);
    mStart = (Button) findViewById(R.id.start);

    mAuthDropbox.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // This logs you out if you're logged in, or vice versa
            if (mLoggedIn) {
                logOut();
            } else {
                // Start the remote authentication
                mApi.getSession().startAuthentication(DropboxMenu.this);
            }
        }
    });

    mDropboxDownload.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mOperation = DOWNLOAD_OPERATION;
            try {
                loadCbla();
            } catch (NullPointerException npe) {
            }
        }
    });

    mDropboxUpload.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mOperation = UPLOAD_OPERATION;
            try {
                loadCbla();
            } catch (NullPointerException npe) {
            }
        }
    });

    mSelectAll.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mCbla.selectAll();
        }
    });

    mStart.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            AlertDialog.Builder cleanerBuilder = new AlertDialog.Builder(DropboxMenu.this);
            cleanerBuilder.setTitle("Cleaner");
            if (mOperation == UPLOAD_OPERATION) {
                cleanerBuilder.setMessage(
                        "Do you want to clean files that are not found locally but only in Dropbox?");
            } else if (mOperation == DOWNLOAD_OPERATION) {
                cleanerBuilder.setMessage(
                        "Do you want to clean files that are not found in Dropbox but only locally?");
            }

            cleanerBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    boolean cleanUnusedFiles = true;
                    initializeTransfer(mCbla.getAllSelectedTitles(), cleanUnusedFiles);
                }

            });

            cleanerBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    boolean cleanUnusedFiles = false;
                    initializeTransfer(mCbla.getAllSelectedTitles(), cleanUnusedFiles);
                }
            });
            cleanerBuilder.show();
        }
    });

    // Display the proper UI state if logged in or not
    setLoggedIn(mApi.getSession().isLinked());

}

From source file:ca.rmen.android.poetassistant.main.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (BuildConfig.DEBUG && ActivityManager.isUserAMonkey()) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }// w  w  w  . ja va2  s. com
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate() called with: " + "savedInstanceState = [" + savedInstanceState + "]");
    mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    setSupportActionBar(mBinding.toolbar);
    Intent intent = getIntent();
    Uri data = intent.getData();
    mPagerAdapter = new PagerAdapter(this, getSupportFragmentManager(), intent);
    mPagerAdapter.registerDataSetObserver(mAdapterChangeListener);

    // Set up the ViewPager with the sections adapter.
    mBinding.viewPager.setAdapter(mPagerAdapter);
    mBinding.viewPager.setOffscreenPageLimit(5);
    mBinding.viewPager.addOnPageChangeListener(mOnPageChangeListener);

    mBinding.tabs.setupWithViewPager(mBinding.viewPager);
    AppBarLayoutHelper.enableAutoHide(mBinding);
    mAdapterChangeListener.onChanged();

    // If the app was launched with a query for the a particular tab, focus on that tab.
    if (data != null && data.getHost() != null) {
        Tab tab = Tab.parse(data.getHost());
        if (tab == null)
            tab = Tab.DICTIONARY;
        mBinding.viewPager.setCurrentItem(mPagerAdapter.getPositionForTab(tab));
    } else if (Intent.ACTION_SEND.equals(intent.getAction())) {
        mBinding.viewPager.setCurrentItem(mPagerAdapter.getPositionForTab(Tab.READER));
    }

    mSearch = new Search(this, mBinding.viewPager);
    loadDictionaries();
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
}

From source file:com.nfc.gemkey.MainActivity.java

@Override
public void onCreate(Bundle icicle) {
    if (DEBUG)/*from w w  w.  ja  va 2 s  .com*/
        Log.d(TAG, "onCreate");
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    mAdapter = NfcAdapter.getDefaultAdapter(this);

    // Create a generic PendingIntent that will be deliver to this activity. The NFC stack
    // will fill in the intent with the details of the discovered tag before delivering to
    // this activity.
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // Setup an intent filter for all MIME based dispatches
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
    mFilters = new IntentFilter[] { tagDetected };

    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
    registerReceiver(mUsbReceiver, filter);

    if (getLastNonConfigurationInstance() != null) {
        Log.i(TAG, "open usb accessory@onCreate");
        mAccessory = (UsbAccessory) getLastNonConfigurationInstance();
        openAccessory(mAccessory);
    }

    buttonLED = (ToggleButton) findViewById(R.id.nfc_btn);
    buttonLED.setBackgroundResource(
            buttonLED.isChecked() ? R.drawable.btn_toggle_yes : R.drawable.btn_toggle_no);
    buttonLED.setOnCheckedChangeListener(mKeyLockListener);

    tagId = (TextView) findViewById(R.id.nfc_tag);
    tagId.setText(R.string.nfc_scan_tag);

    // Avoid NetworkOnMainThreadException
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites()
            .detectNetwork().penaltyLog().build());

    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects()
            .detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

    setVolumeControlStream(AudioManager.STREAM_MUSIC);
}