user2718135
user2718135

Reputation:

Android - Service, MediaPlayer and setVolume

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

Upvotes: 0

Views: 907

Answers (2)

Dave
Dave

Reputation: 4291

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.

Upvotes: 0

SweetWisher ツ
SweetWisher ツ

Reputation: 7306

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

Upvotes: 1

Related Questions