1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.midisynth; 18 19 import android.media.midi.MidiDeviceService; 20 import android.media.midi.MidiDeviceStatus; 21 import android.media.midi.MidiReceiver; 22 23 import com.example.android.common.midi.synth.SynthEngine; 24 25 public class MidiSynthDeviceService extends MidiDeviceService { 26 27 private static final String TAG = MainActivity.TAG; 28 private SynthEngine mSynthEngine = new SynthEngine(); 29 private boolean mSynthStarted = false; 30 31 @Override onCreate()32 public void onCreate() { 33 super.onCreate(); 34 } 35 36 @Override onDestroy()37 public void onDestroy() { 38 mSynthEngine.stop(); 39 super.onDestroy(); 40 } 41 42 @Override onGetInputPortReceivers()43 public MidiReceiver[] onGetInputPortReceivers() { 44 return new MidiReceiver[]{mSynthEngine}; 45 } 46 47 /** 48 * This will get called when clients connect or disconnect. 49 */ 50 @Override onDeviceStatusChanged(MidiDeviceStatus status)51 public void onDeviceStatusChanged(MidiDeviceStatus status) { 52 if (status.isInputPortOpen(0) && !mSynthStarted) { 53 mSynthEngine.start(); 54 mSynthStarted = true; 55 } else if (!status.isInputPortOpen(0) && mSynthStarted) { 56 mSynthEngine.stop(); 57 mSynthStarted = false; 58 } 59 } 60 61 } 62