Java Code Examples for android.media.MediaPlayer#setVolume()
The following examples show how to use
android.media.MediaPlayer#setVolume() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BeepManager.java From moVirt with Apache License 2.0 | 7 votes |
private MediaPlayer buildMediaPlayer(Context activity) { MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnCompletionListener(this); mediaPlayer.setOnErrorListener(this); try { AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); } finally { file.close(); } mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { Log.w(TAG, ioe); mediaPlayer.release(); return null; } }
Example 2
Source File: AlarmRingService.java From Moring-Alarm with Apache License 2.0 | 6 votes |
private void ringTheAlarm(String song) { AssetFileDescriptor assetFileDescriptor= null; try { mPlayer=new MediaPlayer(); mPlayer.reset(); mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); if(song.contains("/")){ //说明是自定义铃声 mPlayer.setDataSource(song); }else{ assetFileDescriptor = this.getAssets().openFd(song); mPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength()); } mPlayer.setVolume(1f, 1f); mPlayer.setLooping(true); mPlayer.prepare(); mPlayer.start(); } catch (IOException e) { e.printStackTrace(); } }
Example 3
Source File: MainActivity.java From pretixdroid with GNU General Public License v3.0 | 6 votes |
private MediaPlayer buildMediaPlayer(Context activity) { mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnCompletionListener(this); // mediaPlayer.setOnErrorListener(this); try { AssetFileDescriptor file = activity.getResources() .openRawResourceFd(R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); } finally { file.close(); } mediaPlayer.setVolume(0.10f, 0.10f); mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { mediaPlayer.release(); return null; } }
Example 4
Source File: BeepManager.java From SimplifyReader with Apache License 2.0 | 6 votes |
private MediaPlayer buildMediaPlayer(Context activity) { MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnCompletionListener(this); mediaPlayer.setOnErrorListener(this); try { AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); } finally { file.close(); } mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { Log.w(TAG, ioe); mediaPlayer.release(); return null; } }
Example 5
Source File: BeepManager.java From QRScanner with GNU Lesser General Public License v3.0 | 6 votes |
private MediaPlayer buildMediaPlayer(Context activity) { MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnCompletionListener(this); mediaPlayer.setOnErrorListener(this); try { AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); } finally { file.close(); } mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { Log.w(TAG, ioe); mediaPlayer.release(); return null; } }
Example 6
Source File: Cocos2dxMusic.java From Example-of-Cocos2DX with MIT License | 6 votes |
/** * create mediaplayer for music * * @param pPath * the pPath relative to assets * @return */ private MediaPlayer createMediaplayer(final String pPath) { MediaPlayer mediaPlayer = new MediaPlayer(); try { if (pPath.startsWith("/")) { final FileInputStream fis = new FileInputStream(pPath); mediaPlayer.setDataSource(fis.getFD()); fis.close(); } else { final AssetFileDescriptor assetFileDescritor = this.mContext.getAssets().openFd(pPath); mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), assetFileDescritor.getStartOffset(), assetFileDescritor.getLength()); } mediaPlayer.prepare(); mediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume); } catch (final Exception e) { mediaPlayer = null; Log.e(Cocos2dxMusic.TAG, "error: " + e.getMessage(), e); } return mediaPlayer; }
Example 7
Source File: VideoLiveWallpaper.java From LLApp with Apache License 2.0 | 6 votes |
@Override public void onSurfaceCreated(SurfaceHolder holder) { LogUtils.d(TAG,"VideoEngine#onSurfaceCreated "); super.onSurfaceCreated(holder); mMediaPlayer = new MediaPlayer(); mMediaPlayer.setSurface(holder.getSurface()); try { AssetManager assetMg = getApplicationContext().getAssets(); AssetFileDescriptor fileDescriptor = assetMg.openFd("test1.mp4"); mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength()); mMediaPlayer.setLooping(true); mMediaPlayer.setVolume(0, 0); mMediaPlayer.prepare(); mMediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } }
Example 8
Source File: VideoLiveWallpaper.java From LiveWallPaper with Apache License 2.0 | 6 votes |
@Override public void onSurfaceCreated(SurfaceHolder holder) { L.d("VideoEngine#onSurfaceCreated "); super.onSurfaceCreated(holder); mMediaPlayer = new MediaPlayer(); mMediaPlayer.setSurface(holder.getSurface()); try { AssetManager assetMg = getApplicationContext().getAssets(); AssetFileDescriptor fileDescriptor = assetMg.openFd("test1.mp4"); mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength()); mMediaPlayer.setLooping(true); mMediaPlayer.setVolume(0, 0); mMediaPlayer.prepare(); mMediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } }
Example 9
Source File: BeepManager.java From FamilyChat with Apache License 2.0 | 6 votes |
private MediaPlayer buildMediaPlayer(Context activity) { MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnCompletionListener(this); mediaPlayer.setOnErrorListener(this); try { AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); } finally { file.close(); } mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { Log.w(TAG, ioe); mediaPlayer.release(); return null; } }
Example 10
Source File: AlarmService.java From sleep-cycle-alarm with GNU General Public License v3.0 | 6 votes |
private void startPlayer() { player = new MediaPlayer(); try { if (isVibrateEnabled()) { postVibrationHandler(); } player.setDataSource(this, Uri.parse(alarmRingtone)); player.setLooping(true); player.setAudioStreamType(AudioManager.STREAM_ALARM); player.setVolume(volumeLevel, volumeLevel); player.prepare(); player.start(); postDelayedVolumeHandler(); } catch (Exception e) { if (player.isPlaying()) { player.stop(); } Log.e(getClass().getName(), "startPlayer(): " + e.getMessage()); stopSelf(); } }
Example 11
Source File: BeepManager.java From weex with Apache License 2.0 | 6 votes |
private MediaPlayer buildMediaPlayer(Context activity) { MediaPlayer mediaPlayer = new MediaPlayer(); try { AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); } finally { file.close(); } mediaPlayer.setOnErrorListener(this); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setLooping(false); mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { Log.w(TAG, ioe); mediaPlayer.release(); return null; } }
Example 12
Source File: BeepManager.java From analyzer-of-android-for-Apache-Weex with Apache License 2.0 | 6 votes |
private MediaPlayer buildMediaPlayer(Context activity) { MediaPlayer mediaPlayer = new MediaPlayer(); try { AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); } finally { file.close(); } mediaPlayer.setOnErrorListener(this); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setLooping(false); mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { Log.w(TAG, ioe); mediaPlayer.release(); return null; } }
Example 13
Source File: BeepManager.java From AirFree-Client with GNU General Public License v3.0 | 6 votes |
private MediaPlayer buildMediaPlayer(Context activity) { MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnCompletionListener(this); mediaPlayer.setOnErrorListener(this); try { AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); } finally { file.close(); } mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { Log.w(TAG, ioe); mediaPlayer.release(); return null; } }
Example 14
Source File: Cocos2dxMusic.java From Example-of-Cocos2DX with MIT License | 6 votes |
/** * create mediaplayer for music * * @param pPath * the pPath relative to assets * @return */ private MediaPlayer createMediaplayer(final String pPath) { MediaPlayer mediaPlayer = new MediaPlayer(); try { if (pPath.startsWith("/")) { final FileInputStream fis = new FileInputStream(pPath); mediaPlayer.setDataSource(fis.getFD()); fis.close(); } else { final AssetFileDescriptor assetFileDescritor = this.mContext.getAssets().openFd(pPath); mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), assetFileDescritor.getStartOffset(), assetFileDescritor.getLength()); } mediaPlayer.prepare(); mediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume); } catch (final Exception e) { mediaPlayer = null; Log.e(Cocos2dxMusic.TAG, "error: " + e.getMessage(), e); } return mediaPlayer; }
Example 15
Source File: BeepManager.java From ScanZxing with Apache License 2.0 | 5 votes |
/** * 创建MediaPlayer * * @param activity * @return */ private MediaPlayer buildMediaPlayer(Context activity) { MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); // 监听是否播放完成 mediaPlayer.setOnCompletionListener(this); mediaPlayer.setOnErrorListener(this); // 配置播放资源 try { AssetFileDescriptor file = activity.getResources() .openRawResourceFd(R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); } finally { file.close(); } // 设置音量 mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); return mediaPlayer; } catch (IOException ioe) { Log.w(TAG, ioe); mediaPlayer.release(); return null; } }
Example 16
Source File: SoundManager.java From Camdroid with Apache License 2.0 | 5 votes |
public void play(int resId) { final MediaPlayer mp = this.sounds.get(resId); if (mp != null) { float volume = this.currentVolume(); synchronized (mp) { mp.setVolume(volume, volume); Log.d(TAG, "mp: " + mp.getAudioSessionId() + " play resId: " + resId); mp.start(); } } }
Example 17
Source File: LocationAwareService.java From LocationAware with Apache License 2.0 | 5 votes |
private void startPlayer() { mPlayer = new MediaPlayer(); mPlayer.setOnErrorListener(mErrorListener); try { isAlarmRinging = true; // add vibration to alarm alert if it is set //if (App.getState().settings().vibrate()) { mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); mHandler.post(mVibrationRunnable); //} // Player setup is here String ringtone;// = App.getState().settings().ringtone(); //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M // && ringtone.startsWith("content://media/external/") // && checkSelfPermission( // Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString(); //} mPlayer.setDataSource(this, Uri.parse(ringtone)); mPlayer.setLooping(true); mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mPlayer.setVolume(mVolumeLevel, mVolumeLevel); mPlayer.prepare(); mPlayer.start(); //if (App.getState().settings().ramping()) { // mHandler.postDelayed(mVolumeRunnable, VOLUME_INCREASE_DELAY); //} else { mPlayer.setVolume(MAX_VOLUME, MAX_VOLUME); //} } catch (Exception e) { isAlarmRinging = false; if (mPlayer.isPlaying()) { mPlayer.stop(); } stopSelf(); } }
Example 18
Source File: AlarmService.java From talalarmo with MIT License | 5 votes |
private void startPlayer() { mPlayer = new MediaPlayer(); mPlayer.setOnErrorListener(mErrorListener); try { // add vibration to alarm alert if it is set if (App.getState().settings().vibrate()) { mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); mHandler.post(mVibrationRunnable); } // Player setup is here String ringtone = App.getState().settings().ringtone(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ringtone.startsWith("content://media/external/") && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString(); } mPlayer.setDataSource(this, Uri.parse(ringtone)); mPlayer.setLooping(true); mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mPlayer.setVolume(mVolumeLevel, mVolumeLevel); mPlayer.prepare(); mPlayer.start(); if (App.getState().settings().ramping()) { mHandler.postDelayed(mVolumeRunnable, VOLUME_INCREASE_DELAY); } else { mPlayer.setVolume(MAX_VOLUME, MAX_VOLUME); } } catch (Exception e) { if (mPlayer.isPlaying()) { mPlayer.stop(); } stopSelf(); } }
Example 19
Source File: DownloadService.java From Popeens-DSub with GNU General Public License v3.0 | 4 votes |
private void applyReplayGain(MediaPlayer mediaPlayer, DownloadFile downloadFile) { if(currentPlaying == null) { return; } SharedPreferences prefs = Util.getPreferences(this); try { float adjust = 0f; if (prefs.getBoolean(Constants.PREFERENCES_KEY_REPLAY_GAIN, false)) { float[] rg = BastpUtil.getReplayGainValues(downloadFile.getFile().getCanonicalPath()); /* track, album */ boolean singleAlbum = false; String replayGainType = prefs.getString(Constants.PREFERENCES_KEY_REPLAY_GAIN_TYPE, "1"); // 1 => Smart replay gain if("1".equals(replayGainType)) { // Check if part of at least <REQUIRED_ALBUM_MATCHES> consequetive songs of the same album int index = downloadList.indexOf(downloadFile); if(index != -1) { String albumName = downloadFile.getSong().getAlbum(); int matched = 0; // Check forwards for(int i = index + 1; i < downloadList.size() && matched < REQUIRED_ALBUM_MATCHES; i++) { if(Util.equals(albumName, downloadList.get(i).getSong().getAlbum())) { matched++; } else { break; } } // Check backwards for(int i = index - 1; i >= 0 && matched < REQUIRED_ALBUM_MATCHES; i--) { if(Util.equals(albumName, downloadList.get(i).getSong().getAlbum())) { matched++; } else { break; } } if(matched >= REQUIRED_ALBUM_MATCHES) { singleAlbum = true; } } } // 2 => Use album tags else if("2".equals(replayGainType)) { singleAlbum = true; } // 3 => Use track tags // Already false, no need to do anything here // If playing a single album or no track gain, use album gain if((singleAlbum || rg[0] == 0) && rg[1] != 0) { adjust = rg[1]; } else { // Otherwise, give priority to track gain adjust = rg[0]; } if (adjust == 0) { /* No RG value found: decrease volume for untagged song if requested by user */ int untagged = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_REPLAY_GAIN_UNTAGGED, "0")); adjust = (untagged - 150) / 10f; } else { int bump = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_REPLAY_GAIN_BUMP, "150")); adjust += (bump - 150) / 10f; } } float rg_result = ((float) Math.pow(10, (adjust / 20))) * volume; if (rg_result > 1.0f) { rg_result = 1.0f; /* android would IGNORE the change if this is > 1 and we would end up with the wrong volume */ } else if (rg_result < 0.0f) { rg_result = 0.0f; } mediaPlayer.setVolume(rg_result, rg_result); } catch(IOException e) { Log.w(TAG, "Failed to apply replay gain values", e); } }
Example 20
Source File: DownloadService.java From Audinaut with GNU General Public License v3.0 | 4 votes |
private void applyReplayGain(MediaPlayer mediaPlayer, DownloadFile downloadFile) { if (currentPlaying == null) { return; } SharedPreferences prefs = Util.getPreferences(this); try { float adjust = 0f; if (prefs.getBoolean(Constants.PREFERENCES_KEY_REPLAY_GAIN, false)) { float[] rg = BastpUtil.getReplayGainValues(downloadFile.getFile().getCanonicalPath()); /* track, album */ boolean singleAlbum = false; String replayGainType = prefs.getString(Constants.PREFERENCES_KEY_REPLAY_GAIN_TYPE, "1"); // 1 => Smart replay gain if ("1".equals(replayGainType)) { // Check if part of at least <REQUIRED_ALBUM_MATCHES> consequetive songs of the same album int index = downloadList.indexOf(downloadFile); if (index != -1) { String albumName = downloadFile.getSong().getAlbum(); int matched = 0; // Check forwards for (int i = index + 1; i < downloadList.size() && matched < REQUIRED_ALBUM_MATCHES; i++) { if (albumName.equals(downloadList.get(i).getSong().getAlbum())) { matched++; } else { break; } } // Check backwards for (int i = index - 1; i >= 0 && matched < REQUIRED_ALBUM_MATCHES; i--) { if (albumName.equals(downloadList.get(i).getSong().getAlbum())) { matched++; } else { break; } } if (matched >= REQUIRED_ALBUM_MATCHES) { singleAlbum = true; } } } // 2 => Use album tags else if ("2".equals(replayGainType)) { singleAlbum = true; } // 3 => Use track tags // Already false, no need to do anything here // If playing a single album or no track gain, use album gain if ((singleAlbum || rg[0] == 0) && rg[1] != 0) { adjust = rg[1]; } else { // Otherwise, give priority to track gain adjust = rg[0]; } if (adjust == 0) { /* No RG value found: decrease volume for untagged song if requested by user */ int untagged = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_REPLAY_GAIN_UNTAGGED, "0")); adjust = (untagged - 150) / 10f; } else { int bump = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_REPLAY_GAIN_BUMP, "150")); adjust += (bump - 150) / 10f; } } float rg_result = ((float) Math.pow(10, (adjust / 20))) * volume; if (rg_result > 1.0f) { rg_result = 1.0f; /* android would IGNORE the change if this is > 1 and we would end up with the wrong volume */ } else if (rg_result < 0.0f) { rg_result = 0.0f; } mediaPlayer.setVolume(rg_result, rg_result); } catch (IOException e) { Log.w(TAG, "Failed to apply replay gain values", e); } }