Home | History | Annotate | Download | only in midisynth
      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 SynthEngine mSynthEngine = new SynthEngine();
     28     private boolean mSynthStarted = false;
     29 
     30     @Override
     31     public void onCreate() {
     32         super.onCreate();
     33     }
     34 
     35     @Override
     36     public void onDestroy() {
     37         mSynthEngine.stop();
     38         super.onDestroy();
     39     }
     40 
     41     @Override
     42     public MidiReceiver[] onGetInputPortReceivers() {
     43         return new MidiReceiver[]{mSynthEngine};
     44     }
     45 
     46     /**
     47      * This will get called when clients connect or disconnect.
     48      */
     49     @Override
     50     public void onDeviceStatusChanged(MidiDeviceStatus status) {
     51         if (status.isInputPortOpen(0) && !mSynthStarted) {
     52             mSynthEngine.start();
     53             mSynthStarted = true;
     54         } else if (!status.isInputPortOpen(0) && mSynthStarted) {
     55             mSynthEngine.stop();
     56             mSynthStarted = false;
     57         }
     58     }
     59 
     60 }
     61