I have a service with MediaPlayer in it. From Activity I need to change Player's volume. Any ideas? P.S. I very need your help P.P.S. Sorry for my English, please
Asked
Active
Viewed 903 times
0
-
@Rani, yes, I'm using SeekBar in Activity. In it's listener, in onStopTrackingTouch() I need to change the volume of MediaPlayer – Helisia Oct 19 '13 at 07:22
2 Answers
1
This might help you..
protected static void setVolume(int volume) {
currentVolume = volume;
{
if (volume == 1) {
volume = 2;
}
try {
float vol = ((float) volume / CONSTANT.SYSTEM_MAX_VOLUME);
mediaPlayer.setVolume(vol, vol);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Also refer this
Community
- 1
- 1
SweetWisher ツ
- 7,296
- 2
- 30
- 74
-
Thanks for the quick reply! But when I must place this code, and how call this method? – Helisia Oct 19 '13 at 07:20
-
-
I want to change volume of Service's MediaPlayer, and I don't want to use AudioManager – Helisia Oct 19 '13 at 07:35
1
If it is a local Service and you have not forced it to run into another thread, you can define a method to set the MediaPlayer's volume, bind to the Service from the Activity, and call the method directly. See the "Local Service Sample" here to see how to bind to a Service. Once you have bound to the Service, you can cast the IBinder you receive to an instance of your Service class to use its methods.
If it is not a local Service or if you have made it run in a separate thread, you will need to communicate with the Service via a Messenger/Handler or using an Intent. There is a "Remote Messenger Service Sample" in the same link from before.
Dave
- 4,282
- 2
- 19
- 24