[英]Android - Service, MediaPlayer and setVolume
I have a service with MediaPlayer in it. 我有一个MediaPlayer服务。 From Activity I need to change Player's volume.
从活动我需要更改播放器的音量。 Any ideas?
有任何想法吗? PS I very need your help PPS Sorry for my English, please
PS我非常需要你的帮助PPS抱歉我的英文
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();
}
}
}
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. 如果它是本地
Service
而您没有强制它运行到另一个线程,您可以定义一个方法来设置MediaPlayer
的卷,从Activity
绑定到Service
,并直接调用该方法。 See the "Local Service Sample" here to see how to bind to a Service
. 请参阅此处的“本地服务示例”以了解如何绑定到
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. 一旦绑定到
Service
,就可以将收到的IBinder
转换为Service
类的实例以使用其方法。
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
. 如果它不是本地
Service
或者您已在单独的线程中运行,则需要通过Messenger
/ Handler
或使用Intent
与Service
进行通信。 There is a "Remote Messenger Service Sample" in the same link from before. 之前的同一链接中有一个“Remote Messenger Service Sample”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.