1

I am developing music play app. When user push my app to background song is playing fine. Now if user opens the camera and starts recording video from camera, I need to pause the song playing from my app .How to do this?

Prabhu M
  • 3,534
  • 8
  • 48
  • 87

2 Answers2

0

I expect that the app responsible for video recording will request audio focus to notify other apps that they should cease playback. If this is the case, you can implement AudioManager.OnAudioFocusChangeListener like this:

@Override
public void onAudioFocusChange(int focusChange)
{
    if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
    {
          // pause playback
    }
    else if (focusChange == AudioManager.AUDIOFOCUS_LOSS)
    {
        ((AudioManager)getSystemService(Context.AUDIO_SERVICE)).abandonAudioFocus(this);
        doStopPlayback();
    }
    // else if... listen to other types of audio focus loss/ gain
}

where doStopPlayback() is your method for releasing the MediaPlayer etc.

See also this guide for media playback.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
-1

You can check it using method Camera.open(cameraId).

Creates a new Camera object to access a particular hardware camera. If the same camera is opened by other applications, this will throw a RuntimeException.

Throws RuntimeException If opening the camera fails (For Example, if the camera is in use by another process or device policy manager has disabled the camera). Update:

Example:

public boolean isCameraUsebyApp() {
    Camera camera = null;
    try {
        camera = Camera.open();
    } catch (RuntimeException e) {
        return true;
    } finally {
        if (camera != null) camera.release();
    }
    return false;
}

You can use this method to use as but keep this thing in mind that this method first acquire the camera.

If its acquire successfully then its mean that no other application is using this camera and don't forgot to release it again otherwise you will not able to acquire it again.

Its its throws RuntimeException it means that camera is in use by another process or device policy manager has disabled the camera.

Akshay Chopde
  • 670
  • 4
  • 10