List of usage examples for android.media AudioManager STREAM_MUSIC
int STREAM_MUSIC
To view the source code for android.media AudioManager STREAM_MUSIC.
Click Source Link
From source file:com.google.android.apps.santatracker.dasherdancer.DasherDancerActivity.java
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dasher_dancer); mMeasurement = FirebaseAnalytics.getInstance(this); MeasurementManager.recordScreenView(mMeasurement, getString(R.string.analytics_screen_dasher)); AnalyticsManager.initializeAnalyticsTracker(this); AnalyticsManager.sendScreenView(R.string.analytics_screen_dasher); mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); mMemoryCache = new LruCache<Integer, Bitmap>(240) { protected void entryRemoved(boolean evicted, Integer key, Bitmap oldValue, Bitmap newValue) { if ((oldValue != null) && (oldValue != newValue)) { oldValue.recycle();/*w ww . j a v a 2s . com*/ oldValue = null; } } }; CharacterAdapter adapter = new CharacterAdapter(sCharacters); mPager = (NoSwipeViewPager) findViewById(R.id.character_pager); mPager.setAdapter(adapter); mPager.setGestureDetectorListeners(this, this, this); mPager.setOnPageChangeListener(this); mHandler = new Handler(getMainLooper(), this); mDetector = new ShakeDetector(this); mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); mSoundIds[0][Character.ANIM_PINCH_IN] = mSoundPool.load(this, R.raw.santa_pinchin, 1); mSoundIds[0][Character.ANIM_PINCH_OUT] = mSoundPool.load(this, R.raw.santa_pinchout, 1); mSoundIds[0][Character.ANIM_SHAKE] = mSoundPool.load(this, R.raw.santa_shake, 1); mSoundIds[0][Character.ANIM_SWIPE_UP] = mSoundPool.load(this, R.raw.santa_swipeup, 1); mSoundIds[0][Character.ANIM_SWIPE_LEFT] = mSoundPool.load(this, R.raw.santa_swipeleft, 1); mSoundIds[0][Character.ANIM_SWIPE_RIGHT] = mSoundPool.load(this, R.raw.santa_swiperight, 1); mSoundIds[0][Character.ANIM_SWIPE_DOWN] = mSoundPool.load(this, R.raw.santa_swipedown, 1); mSoundIds[0][Character.ANIM_TAP] = mSoundPool.load(this, R.raw.santa_tap, 1); mSoundIds[1][Character.ANIM_PINCH_IN] = mSoundPool.load(this, R.raw.elf_pinchin_ball, 1); mSoundIds[1][Character.ANIM_PINCH_OUT] = mSoundPool.load(this, R.raw.elf_pinchout, 1); mSoundIds[1][Character.ANIM_SHAKE] = mSoundPool.load(this, R.raw.elf_shake2, 1); mSoundIds[1][Character.ANIM_SWIPE_DOWN] = mSoundPool.load(this, R.raw.elf_swipedown2, 1); mSoundIds[1][Character.ANIM_SWIPE_UP] = mSoundPool.load(this, R.raw.elf_swipeup2, 1); mSoundIds[1][Character.ANIM_SWIPE_LEFT] = mSoundPool.load(this, R.raw.elf_swipeleft, 1); mSoundIds[1][Character.ANIM_SWIPE_RIGHT] = mSoundPool.load(this, R.raw.elf_swiperight, 1); mSoundIds[1][Character.ANIM_TAP] = mSoundPool.load(this, R.raw.elf_tap3, 1); mSoundIds[2][Character.ANIM_PINCH_IN] = mSoundPool.load(this, R.raw.reindeer_pinchin, 1); mSoundIds[2][Character.ANIM_PINCH_OUT] = mSoundPool.load(this, R.raw.reindeer_pinchout, 1); mSoundIds[2][Character.ANIM_SHAKE] = mSoundPool.load(this, R.raw.reindeer_shake, 1); mSoundIds[2][Character.ANIM_SWIPE_UP] = mSoundPool.load(this, R.raw.reindeer_swipeup, 1); mSoundIds[2][Character.ANIM_SWIPE_DOWN] = mSoundPool.load(this, R.raw.reindeer_swipedown, 1); mSoundIds[2][Character.ANIM_SWIPE_LEFT] = mSoundPool.load(this, R.raw.reindeer_swipeleft, 1); mSoundIds[2][Character.ANIM_SWIPE_RIGHT] = mSoundPool.load(this, R.raw.reindeer_swiperight, 1); mSoundIds[2][Character.ANIM_TAP] = mSoundPool.load(this, R.raw.reindeer_tap2, 1); mSoundIds[3][Character.ANIM_PINCH_IN] = mSoundPool.load(this, R.raw.snowman_pinchin, 1); mSoundIds[3][Character.ANIM_PINCH_OUT] = mSoundPool.load(this, R.raw.snowman_pinchout, 1); mSoundIds[3][Character.ANIM_SHAKE] = mSoundPool.load(this, R.raw.snowman_shake, 1); mSoundIds[3][Character.ANIM_SWIPE_UP] = mSoundPool.load(this, R.raw.snowman_swipeup, 1); mSoundIds[3][Character.ANIM_SWIPE_DOWN] = mSoundPool.load(this, R.raw.snowman_swipedown, 1); mSoundIds[3][Character.ANIM_SWIPE_LEFT] = mSoundPool.load(this, R.raw.snowman_swipeleft, 1); mSoundIds[3][Character.ANIM_SWIPE_RIGHT] = mSoundPool.load(this, R.raw.snowman_swiperight, 1); mSoundIds[3][Character.ANIM_TAP] = mSoundPool.load(this, R.raw.snowman_tap, 1); mAchievements = new HashSet[4]; mAchievements[0] = new HashSet<Integer>(); mAchievements[1] = new HashSet<Integer>(); mAchievements[2] = new HashSet<Integer>(); mAchievements[3] = new HashSet<Integer>(); mProgressAnimator = ObjectAnimator.ofFloat(findViewById(R.id.progress), "rotation", 360f); mProgressAnimator.setDuration(4000); mProgressAnimator.start(); mGamesFragment = PlayGamesFragment.getInstance(this, this); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { ImmersiveModeHelper.setImmersiveSticky(getWindow()); ImmersiveModeHelper.installSystemUiVisibilityChangeListener(getWindow()); } }
From source file:com.lithiumli.fiction.PlaybackService.java
public void play(int index) { mQueue.setCurrent(index);//from w w w . ja v a 2 s .c om Song song = mQueue.getCurrent(); Intent intent = new Intent(EVENT_PLAYING); intent.putExtra(DATA_SONG, song); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); broadcastPlayState(PlayState.PLAYING); Log.d("fiction", "Playing new song"); try { MediaPlayer nextMediaPlayer = new MediaPlayer(); nextMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); nextMediaPlayer.setDataSource(getApplicationContext(), song.getUri()); nextMediaPlayer.setOnPreparedListener(this); nextMediaPlayer.prepareAsync(); } catch (IOException e) { } }
From source file:cn.jasonlv.siri.activity.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // ??? ???//from w w w .j a v a 2 s .c o m musicManager = new MusicManager(getApplicationContext()); // ?? initImageLoader(getApplicationContext()); /* get the installed package list*/ // ?? mPackageManager = new NativePackageManager(getApplicationContext()); // ? setVolumeControlStream(AudioManager.STREAM_MUSIC); // ? mSynthesizer = new Synthesizer(getApplicationContext()); //mSynthesizer.speak("EDI"); // ? mContactManager = new ContactsManager(getApplicationContext()); mContactManager.getContactList(); // ?? detactor = new LocationDetactor(getApplicationContext()); info = detactor.getLocationInfo(); Log.d("location info", info.lat + ", " + info.lon); //for(Object o : mPackageManager.getPackageList()){ // System.out.println(o.toString()); setContentView(R.layout.sdk2_api); //txtLog = (TextView) findViewById(R.id.txtLog); btn = (ActionButton) findViewById(R.id.btn); // ? speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this, new ComponentName(this, VoiceRecognitionService.class)); speechRecognizer.setRecognitionListener(this); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); boolean api = sp.getBoolean("api", false); if (api) { switch (status) { case STATUS_None: start(); //btn.setText("?"); status = STATUS_WaitingReady; break; case STATUS_WaitingReady: cancel(); status = STATUS_None; //btn.setText(""); break; case STATUS_Ready: cancel(); status = STATUS_None; //btn.setText(""); break; case STATUS_Speaking: stop(); status = STATUS_Recognition; //btn.setText(""); break; case STATUS_Recognition: cancel(); status = STATUS_None; //btn.setText(""); break; } } else { start(); } } }); ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setDisplayShowTitleEnabled(true); container = (LinearLayout) findViewById(R.id.container); scrollView = (ScrollView) findViewById(R.id.scroll); scrollView.fullScroll(View.FOCUS_DOWN); }
From source file:com.scooter1556.sms.android.activity.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Retrieve preferences if they exist SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); sharedPreferences.registerOnSharedPreferenceChangeListener(onPreferencesChanged); // Set volume control to media setVolumeControlStream(AudioManager.STREAM_MUSIC); // Initialise database db = new ConnectionDatabase(this); // Load default settings PreferenceManager.setDefaultValues(this, R.xml.preferences, false); // Action Bar assert getSupportActionBar() != null; getSupportActionBar().setDisplayShowHomeEnabled(false); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Navigation Drawer drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); drawerList = (ListView) findViewById(R.id.navigation_drawer_list); NavigationDrawerListItem[] drawerListItems = new NavigationDrawerListItem[3]; drawerListItems[MENU_MEDIA_BROWSER] = new NavigationDrawerListItem(R.drawable.ic_media_browser, getResources().getStringArray(R.array.navigation_drawer_list_items)[MENU_MEDIA_BROWSER]); drawerListItems[MENU_SETTINGS] = new NavigationDrawerListItem(R.drawable.ic_settings, getResources().getStringArray(R.array.navigation_drawer_list_items)[MENU_SETTINGS]); drawerListItems[MENU_EXIT] = new NavigationDrawerListItem(R.drawable.ic_close, getResources().getStringArray(R.array.navigation_drawer_list_items)[MENU_EXIT]); NavigationDrawerListItemAdapter drawerAdapter = new NavigationDrawerListItemAdapter(this, R.layout.drawer_list_item, drawerListItems); drawerList.setAdapter(drawerAdapter); drawerList.setOnItemClickListener(new DrawerItemClickListener()); drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { /** Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View view) { super.onDrawerClosed(view); getSupportActionBar().setTitle(title); }/* w ww . ja v a 2 s. c o m*/ /** Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); title = getSupportActionBar().getTitle().toString(); getSupportActionBar().setTitle(getString(R.string.navigation_drawer_title)); } }; // Set the drawer toggle as the DrawerListener drawerLayout.setDrawerListener(drawerToggle); // Sliding Panel slidingPanel = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout); slidingPanel.setPanelHeight((int) getResources().getDimension(R.dimen.audio_player_small_fragment_height)); PanelSlideListener slidingPanelListener = new SlidingUpPanelLayout.PanelSlideListener() { @Override public void onPanelAnchored(final View panel) { } @Override public void onPanelCollapsed(final View panel) { View audioPlayerSmallContainer = findViewById(R.id.sliding_panel_small_container); View audioPlayerContainer = findViewById(R.id.sliding_panel_container); audioPlayerSmallContainer.setVisibility(View.VISIBLE); audioPlayerSmallContainer.setAlpha(1.0f); audioPlayerContainer.setVisibility(View.GONE); audioPlayerContainer.setAlpha(1.0f); // Update menu items audioPlayerFragment.setMenuVisibility(false); audioPlaylistFragment.setMenuVisibility(false); mediaBrowserFragment.setMenuVisibility(true); //Update action bar title getSupportActionBar().setTitle(title); } @Override public void onPanelExpanded(final View panel) { View audioPlayerSmallContainer = findViewById(R.id.sliding_panel_small_container); View audioPlayerContainer = findViewById(R.id.sliding_panel_container); audioPlayerSmallContainer.setVisibility(View.GONE); audioPlayerSmallContainer.setAlpha(1.0f); audioPlayerContainer.setVisibility(View.VISIBLE); audioPlayerContainer.setAlpha(1.0f); // Update menu items audioPlayerFragment.setMenuVisibility(slidingPanelFragment == SLIDING_PANEL_PLAYER); audioPlaylistFragment.setMenuVisibility(slidingPanelFragment == SLIDING_PANEL_PLAYLIST); mediaBrowserFragment.setMenuVisibility(false); //Update action bar title getSupportActionBar().setTitle(slidingPanelTitle); } @Override public void onPanelHidden(final View view) { } @Override public void onPanelSlide(final View panel, final float slideOffset) { View audioPlayerSmallContainer = findViewById(R.id.sliding_panel_small_container); View audioPlayerContainer = findViewById(R.id.sliding_panel_container); if (slideOffset < 1.0f) { audioPlayerSmallContainer.setVisibility(View.VISIBLE); audioPlayerContainer.setVisibility(View.VISIBLE); } else { audioPlayerSmallContainer.setVisibility(View.GONE); audioPlayerContainer.setVisibility(View.VISIBLE); } audioPlayerSmallContainer.setAlpha(1.0f - slideOffset); audioPlayerContainer.setAlpha(slideOffset); } }; slidingPanel.setPanelSlideListener(slidingPanelListener); if (savedInstanceState == null) { // Initialise main view mediaBrowserFragment = new MediaFolderFragment(); mediaBrowserTitle = title = getString(R.string.media_title); getSupportFragmentManager().beginTransaction() .add(R.id.main_container, mediaBrowserFragment, Integer.toString(MENU_MEDIA_BROWSER)).commit(); assert getSupportActionBar() != null; getSupportActionBar().setTitle(getString(R.string.media_title)); updateDrawer(MENU_MEDIA_BROWSER); // Initialise small audio player sliding panel fragment audioPlayerSmallFragment = new AudioPlayerSmallFragment(); getSupportFragmentManager().beginTransaction().add(R.id.sliding_panel_small_container, audioPlayerSmallFragment, Integer.toString(SLIDING_PANEL_SMALL_PLAYER)).commit(); // Initialise audio player fragment audioPlayerFragment = new AudioPlayerFragment(); audioPlayerFragment.setMenuVisibility(false); getSupportFragmentManager().beginTransaction() .add(R.id.sliding_panel_container, audioPlayerFragment, Integer.toString(SLIDING_PANEL_PLAYER)) .commit(); slidingPanelTitle = getString(R.string.audio_player_title); slidingPanelFragment = SLIDING_PANEL_PLAYER; // Initialise audio playlist fragment audioPlaylistFragment = new AudioPlaylistFragment(); audioPlaylistFragment.setMenuVisibility(false); getSupportFragmentManager().beginTransaction().add(R.id.sliding_panel_container, audioPlaylistFragment, Integer.toString(SLIDING_PANEL_PLAYLIST)).hide(audioPlaylistFragment).commit(); // Check connection long id = sharedPreferences.getLong("Connection", -1); if (id < 0) { Intent connectionsIntent = new Intent(this, ConnectionActivity.class); startActivityForResult(connectionsIntent, RESULT_CODE_CONNECTIONS); } else { Connection connection = db.getConnection(id); RESTService.getInstance().setConnection(connection); } } else { // Reload fragments mediaBrowserFragment = getSupportFragmentManager() .findFragmentByTag(Integer.toString(MENU_MEDIA_BROWSER)); audioPlayerSmallFragment = (AudioPlayerSmallFragment) getSupportFragmentManager() .findFragmentByTag(Integer.toString(SLIDING_PANEL_SMALL_PLAYER)); audioPlayerFragment = (AudioPlayerFragment) getSupportFragmentManager() .findFragmentByTag(Integer.toString(SLIDING_PANEL_PLAYER)); audioPlaylistFragment = (AudioPlaylistFragment) getSupportFragmentManager() .findFragmentByTag(Integer.toString(SLIDING_PANEL_PLAYLIST)); // Restore activity state variables slidingPanelFragment = savedInstanceState.getInt(STATE_SLIDING_PANEL_FRAGMENT); slidingPanelTitle = savedInstanceState.getString(STATE_SLIDING_PANEL_TITLE); title = savedInstanceState.getString(STATE_MAIN_TITLE); if (savedInstanceState.getSerializable(STATE_SLIDING_PANEL) == PanelState.EXPANDED) { slidingPanel.setPanelState(PanelState.EXPANDED); slidingPanelListener.onPanelSlide(slidingPanel, 1.0f); slidingPanelListener.onPanelExpanded(slidingPanel); } else { slidingPanel.setPanelState(PanelState.COLLAPSED); slidingPanelListener.onPanelSlide(slidingPanel, 0.0f); slidingPanelListener.onPanelCollapsed(slidingPanel); } } // Add fragment back stack listener getSupportFragmentManager().addOnBackStackChangedListener(this); }
From source file:com.classiqo.nativeandroid_32bitz.playback.LocalPlayback.java
@Override public void play(QueueItem item) { mPlayOnFocusGain = true;//from w w w .ja v a2 s . com tryToGetAudioFocus(); registerAudioNoisyReceiver(); String mediaId = item.getDescription().getMediaId(); boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId); if (mediaHasChanged) { mCurrentPosition = 0; mCurrentMediaId = mediaId; } if (mState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) { configMediaPlayerState(); } else { mState = PlaybackStateCompat.STATE_STOPPED; relaxResources(false); MediaMetadataCompat track = mMusicProvider .getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId())); //noinspection ResourceType String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE); if (source != null) { source = source.replaceAll(" ", "%20"); } try { createMediaPlayerIfNeeded(); mState = PlaybackStateCompat.STATE_BUFFERING; mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.setDataSource(source); mMediaPlayer.prepareAsync(); mWifiLock.acquire(); if (mCallback != null) { mCallback.onPlaybackStatusChanged(mState); } } catch (IOException ex) { LogHelper.e(TAG, ex, "Exception playing song"); if (mCallback != null) { mCallback.onError(ex.getMessage()); } } } }
From source file:com.example.chu.googleplaylibrary.playback.LocalPlayback.java
@Override public void play(QueueItem item) { mPlayOnFocusGain = true;/* w w w.j ava2 s.c om*/ tryToGetAudioFocus(); registerAudioNoisyReceiver(); String mediaId = item.getDescription().getMediaId(); boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId); if (mediaHasChanged) { mCurrentPosition = 0; mCurrentMediaId = mediaId; } if (mState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) { configMediaPlayerState(); } else { mState = PlaybackStateCompat.STATE_STOPPED; relaxResources(false); // release everything except MediaPlayer String source = mMusicProvider; try { createMediaPlayerIfNeeded(); mState = PlaybackStateCompat.STATE_BUFFERING; mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.setDataSource(source); // Starts preparing the media player in the background. When // it's done, it will call our OnPreparedListener (that is, // the onPrepared() method on this class, since we set the // listener to 'this'). Until the media player is prepared, // we *cannot* call start() on it! mMediaPlayer.prepareAsync(); // If we are streaming from the internet, we want to hold a // Wifi lock, which prevents the Wifi radio from going to // sleep while the song is playing. mWifiLock.acquire(); if (mCallback != null) { mCallback.onPlaybackStatusChanged(mState); } } catch (IOException ex) { LogHelper.e(TAG, ex, "Exception playing song"); if (mCallback != null) { mCallback.onError(ex.getMessage()); } } } }
From source file:org.chromium.latency.walt.AudioFragment.java
@Override public void onClick(View v) { switch (v.getId()) { case R.id.button_start_audio: chartLayout.setVisibility(View.GONE); disableButtons();/*from w w w .j a va 2 s .c om*/ AudioTestType testType = getSelectedTestType(); switch (testType) { case CONTINUOUS_PLAYBACK: case CONTINUOUS_RECORDING: case DISPLAY_WAVEFORM: audioTest.setAudioMode(AudioTest.AudioMode.CONTINUOUS); audioTest.setPeriod(AudioTest.CONTINUOUS_TEST_PERIOD); break; case COLD_PLAYBACK: case COLD_RECORDING: audioTest.setAudioMode(AudioTest.AudioMode.CONTINUOUS); audioTest.setPeriod(AudioTest.COLD_TEST_PERIOD); break; } if (testType == AudioTestType.DISPLAY_WAVEFORM) { // Only need to record 1 beep to display wave audioTest.setRecordingRepetitions(1); } else { audioTest.setRecordingRepetitions( getIntPreference(getContext(), R.string.preference_audio_in_reps, 5)); } if (testType == AudioTestType.CONTINUOUS_PLAYBACK || testType == AudioTestType.COLD_PLAYBACK || testType == AudioTestType.CONTINUOUS_RECORDING || testType == AudioTestType.COLD_RECORDING) { latencyChart.setVisibility(View.VISIBLE); latencyChart.clearData(); latencyChart.setLegendEnabled(false); final String description = getResources().getStringArray(R.array.audio_mode_array)[modeSpinner .getSelectedItemPosition()] + " [ms]"; latencyChart.setDescription(description); } switch (testType) { case CONTINUOUS_RECORDING: case COLD_RECORDING: case DISPLAY_WAVEFORM: attemptRecordingTest(); break; case CONTINUOUS_PLAYBACK: case COLD_PLAYBACK: // Set media volume to max AudioManager am = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0); audioTest.beginPlaybackMeasurement(); break; } break; case R.id.button_stop_audio: audioTest.stopTest(); break; case R.id.button_close_chart: chartLayout.setVisibility(View.GONE); break; } }
From source file:com.codegarden.nativenavigation.JuceActivity.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); isScreenSaverEnabled = true;/*from w w w. j a v a2 s. c om*/ viewHolder = new ViewHolder(this); setContentView(viewHolder); // -- Custom native UI setContentView(R.layout.main_activity); LinearLayout juceViewContainer = (LinearLayout) findViewById(R.id.juce_view_container); juceViewContainer.addView(viewHolder); final Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar); messageTitle = new StringBuilder(); messageTitle.append("JUCE meets Android"); toolbar.setTitle(messageTitle); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); initialiseData(); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.left_drawer); LinearLayoutManager llm = new LinearLayoutManager(this); recyclerView.setLayoutManager(llm); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { public void onDrawerClosed(View view) { toolbar.setTitle(messageTitle); } public void onDrawerOpened(View drawerView) { toolbar.setTitle(drawerTitle); } }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); MessageListAdapter adapter = new MessageListAdapter(messages, messageTitle, drawerLayout); recyclerView.setAdapter(adapter); //-------------------------------------- setVolumeControlStream(AudioManager.STREAM_MUSIC); }
From source file:com.example.android.supportv4.media.Playback.java
public void play(QueueItem item) { mPlayOnFocusGain = true;//from w ww.ja va 2s . c o m tryToGetAudioFocus(); registerAudioNoisyReceiver(); String mediaId = item.getDescription().getMediaId(); boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId); if (mediaHasChanged) { mCurrentPosition = 0; mCurrentMediaId = mediaId; } if (mState == PlaybackState.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) { configMediaPlayerState(); } else { mState = PlaybackState.STATE_STOPPED; relaxResources(false); // release everything except MediaPlayer MediaMetadataCompat track = mMusicProvider .getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId())); String source = track.getString(MusicProvider.CUSTOM_METADATA_TRACK_SOURCE); try { createMediaPlayerIfNeeded(); mState = PlaybackState.STATE_BUFFERING; mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.setDataSource(source); // Starts preparing the media player in the background. When // it's done, it will call our OnPreparedListener (that is, // the onPrepared() method on this class, since we set the // listener to 'this'). Until the media player is prepared, // we *cannot* call start() on it! mMediaPlayer.prepareAsync(); // If we are streaming from the internet, we want to hold a // Wifi lock, which prevents the Wifi radio from going to // sleep while the song is playing. mWifiLock.acquire(); if (mCallback != null) { mCallback.onPlaybackStatusChanged(mState); } } catch (IOException ex) { Log.e(TAG, "Exception playing song", ex); if (mCallback != null) { mCallback.onError(ex.getMessage()); } } } }
From source file:com.example.android.gft.ColorsFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.word_list, container, false); // Create and setup the {@link AudioManager} to request audio focus mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE); // Create a list of words final ArrayList<Word> words = new ArrayList<Word>(); words.add(new Word(R.string.color_red, R.string.miwok_color_red, R.drawable.color_red, R.raw.color_red)); words.add(new Word(R.string.color_mustard_yellow, R.string.miwok_color_mustard_yellow, R.drawable.color_mustard_yellow, R.raw.color_mustard_yellow)); words.add(new Word(R.string.color_dusty_yellow, R.string.miwok_color_dusty_yellow, R.drawable.color_dusty_yellow, R.raw.color_dusty_yellow)); words.add(new Word(R.string.color_green, R.string.miwok_color_green, R.drawable.color_green, R.raw.color_green));//from ww w . ja v a2s. c om words.add(new Word(R.string.color_brown, R.string.miwok_color_brown, R.drawable.color_brown, R.raw.color_brown)); words.add( new Word(R.string.color_gray, R.string.miwok_color_gray, R.drawable.color_gray, R.raw.color_gray)); words.add(new Word(R.string.color_black, R.string.miwok_color_black, R.drawable.color_black, R.raw.color_black)); words.add(new Word(R.string.color_white, R.string.miwok_color_white, R.drawable.color_white, R.raw.color_white)); // 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(getActivity(), words, R.color.category_colors); // 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) rootView.findViewById(R.id.list); // 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); // Set a click listener to play the audio when the list item is clicked on listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { // Release the media player if it currently exists because we are about to // play a different sound file releaseMediaPlayer(); // Get the {@link Word} object at the given position the user clicked on Word word = words.get(position); // Request audio focus so in order to play the audio file. The app needs to play a // short audio file, so we will request audio focus with a short amount of time // with AUDIOFOCUS_GAIN_TRANSIENT. int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { // We have audio focus now. // Create and setup the {@link MediaPlayer} for the audio resource associated // with the current word mMediaPlayer = MediaPlayer.create(getActivity(), word.getAudioResourceId()); // Start the audio file mMediaPlayer.start(); // Setup a listener on the media player, so that we can stop and release the // media player once the sound has finished playing. mMediaPlayer.setOnCompletionListener(mCompletionListener); } } }); return rootView; }