1

I'm developing a radio app in which I have written MediaPlayer class inside Service,on button click starting and closing service to start and stop MediaPlayer.But in the project I'm facing two problems

1.I want to show progress while preparing music.although I have written code inside service so not able to show ProgressBar.

2.when we volume up then instead of music volume ringing volume is increasing.

I spent my lot time and didn't get success can any please tell me how to resolve this.Your valuable suggestion would be great appreciated.

PlayerService.java

public class PlayerService extends Service implements OnPreparedListener {
    private MediaPlayer mPlayer;

    public PlayerService() {
        // TODO Auto-generated constructor stub
        super();
    }

    @Override
    public IBinder onBind(Intent arg0) {

        return null;

    }

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            mPlayer = new MediaPlayer();
            mPlayer.setDataSource(Live.mUrl);
            mPlayer.prepare();
            mPlayer.setOnPreparedListener(this);
            // mVisualizerView = (VisualizerView)
            // findViewById(R.id.visualizerView);
            // mVisualizerView.link(mPlayer);

            // Start with just line renderer
            // addLineRenderer();
        } catch (Exception e) {
            // TODO: handle exception

        }

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mPlayer.start();
        return 0;

    }

    @Override
    public void onDestroy() {
        mPlayer.release();
        super.onDestroy();

    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        // TODO Auto-generated method stub

    }

}
Dilip
  • 2,271
  • 5
  • 32
  • 52

1 Answers1

0

Check out registerMediaButtonEventReceiver(ComponentName broadcastReceiver);

Define a BroadcastReceiver that handles ACTION_MEDIA_BUTTON. The recieved intent includes a single extra field, EXTRA_KEY_EVENT, containing the key event that caused the broadcast. You can use this key event to get which key was pressed.

This is just a sample code. syntax errors may be there.

// in onCreate of activity
registerMediaButtonEventReceiver(mediaReceiver ); 

// later somewhere in activity.
MediaButton_Receiver mediaReceiver = new MediaButton_Receiver();

class MediaButton_Receiver implements BroadcastReceiver {
     void onReceive(Intent intent) {

          KeyEvent ke = (KeyEvent)intent.getExtra(Intent.EXTRA_KEY_EVENT); 
          if (ke .getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
               //Decrease your volume
          }
          // Similarly other key codes .......
     } 
}

Unregister the receiver in onPause() or onStop()

ravindra.kamble
  • 1,023
  • 3
  • 11
  • 20