Skip to content

The app stop playing sounds when the USB MIDI is attached

Kaoru Shoji edited this page Mar 19, 2015 · 1 revision

This document may be the solution for the issue #33.
Currently, this feature has not been released yet. To test this feature, checkout the fix_for_issue_33 branch.

On some Android devices (ex. Xperia series with Android 4.4), the USB MIDI device will be mis-detected as USB Audio device.
Then, the sound output source will be changed to the USB Audio device by the system.
But there is no USB Audio actually, the application can't play any sound.

To avoid this behaviour, add these code and configuration below.
With these code, the real USB Audio device connection will also be blocked.

Add permission to AndroidManifest.xml

Add this line to inside of the manifest tag.

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

Use UsbAudioConnectionCanceller on the Activity.

  • Add a field of UsbAudioConnectionCanceller.
  • Initialize instance at onCreate method if the applicable Android device.
  • Terminate instance at onDestroy method.
UsbAudioConnectionCanceller usbAudioCanceller;

@Override
public void onCreate(final Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
    if (Build.MANUFACTURER.toLowerCase().contains("sony") &&
    	Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT &&
    	Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
        usbAudioCanceller = new UsbAudioConnectionCanceller(this);
    }

    ...
}

@Override
protected void onDestroy() {
	super.onDestroy();
    
    if (usbAudioCanceller != null) {
        usbAudioCanceller.terminate();
    }

    ...
}