设置初始音量的手机铃声音量 [英] Setting the Initial Volume to the phones ring volume

查看:132
本文介绍了设置初始音量的手机铃声音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试当用户打开其设置到任何他们在自己的手机铃声音量的音乐的音量的应用程序,使之如此。这是我的code,到目前为止,但我不是很确定是什么在setVolume的paramters(浮动,浮动)的。 Android的文档没有解释得很好。什么是我的code做错了?

  AudioManager音频=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
  INT currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);   MPLAYER = MediaPlayer.create(这一点,R.raw.song);
   mPlayer.setOnErrorListener(本);   如果(MPLAYER!= NULL)
    {
    mPlayer.setLooping(真);
    mPlayer.setVolume(currentVolume,1);
}


解决方案

看起来像<一个href=\"http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume%28int,%20int,%20int%29\"相对=nofollow> audio.setStreamVolume 是你想要的,但是传递STREAM_MUSIC而不是STREAM_RING。

请注意:音乐音量和铃声音量都可能有不同的最大值,所以你需要正常化他们。使用getStreamMaxVolume为。

我没有这样做之前,我还没有编这一点,但code应该是这个样子。

  AudioManager音频=(AudioManager)getSystemService(Context.AUDIO_SERVICE);//获取当前铃声音量为最大铃声音量的百分比。
INT currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
INT maxRingerVolume = audio.getStreamMaxVolume(AudioManager.STREAM_RING);
双比例= currentVolume /(双)maxRingerVolume;//计算所需的音乐音量为最大音量音乐的相同的百分比。
INT maxMusicVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
INT desiredMusicVolume =(INT)(比例* maxMusicVolume);//设置音乐流音量。
audio.setStreamVolume(AudioManager.STREAM_MUSIC,desiredMusicVolume,0 / * *标志/);

im trying to make it so when the user opens the app it sets the volume of the music to whatever they have their phones ringer volume at. This is my code so far but im not exactly sure what the paramters on setVolume(float, float) are. The android documentation doesn't explain it well. What is my code doing wrong here?

  AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);

   mPlayer = MediaPlayer.create(this, R.raw.song);
   mPlayer.setOnErrorListener(this);

   if(mPlayer!= null)
    {         
    mPlayer.setLooping(true);
    mPlayer.setVolume(currentVolume,1);
}

解决方案

Looks like audio.setStreamVolume is what you want, but pass in STREAM_MUSIC instead of STREAM_RING.

Note: the music volume and ringer volume are likely to have different maximum values, so you will need to normalize them. Use getStreamMaxVolume for that.

I have not done this before, and I haven't compiled this, but the code should look something like this

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

// Get the current ringer volume as a percentage of the max ringer volume.
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int maxRingerVolume = audio.getStreamMaxVolume(AudioManager.STREAM_RING);
double proportion = currentVolume/(double)maxRingerVolume;

// Calculate a desired music volume as that same percentage of the max music volume.
int maxMusicVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int desiredMusicVolume = (int)(proportion * maxMusicVolume);

// Set the music stream volume.
audio.setStreamVolume(AudioManager.STREAM_MUSIC, desiredMusicVolume, 0 /*flags*/);

这篇关于设置初始音量的手机铃声音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆