com.polyvi.xface.extension.audio.XAudioExt.java Source code

Java tutorial

Introduction

Here is the source code for com.polyvi.xface.extension.audio.XAudioExt.java

Source

/*
 This file was modified from or inspired by Apache Cordova.
    
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements. See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership. The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License. You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied. See the License for the
 specific language governing permissions and limitations
 under the License.
*/

package com.polyvi.xface.extension.audio;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;

import com.polyvi.xface.extension.XCallbackContext;
import com.polyvi.xface.extension.XExtension;
import com.polyvi.xface.extension.XExtensionResult;
import com.polyvi.xface.util.XLog;

public class XAudioExt extends XExtension {
    private static final String CLASS_NAME = "XAudioExt";

    /**  Audio ??js???*/
    private static final String COMMAND_PLAY = "play";
    private static final String COMMAND_STOP = "stop";
    private static final String COMMAND_PAUSE = "pause";
    private static final String COMMAND_SEEKTO = "seekTo";
    private static final String COMMAND_RELEASE = "release";
    private static final String COMMAND_GETCURRENTPOSITION = "getCurrentPosition";
    private static final String COMMAND_STARTRECORDING = "startRecording";
    private static final String COMMAND_STOPRECORDING = "stopRecording";
    private static final String COMMAND_SETVOLUME = "setVolume";

    private HashMap<String, XAudioImpl> mPlayersMap; //  app ? appId ??? app  map

    // TODO:???audio;?????audio

    public XAudioExt() {
        mPlayersMap = new HashMap<String, XAudioImpl>();
    }

    @Override
    public void sendAsyncResult(String result) {
    }

    @Override
    public boolean isAsync(String action) {
        if (action.equals(COMMAND_GETCURRENTPOSITION)) {
            return false;
        }
        return true;
    }

    @Override
    public void onPageStarted() {
        destroy();
    }

    @Override
    public void destroy() {
        if (!mPlayersMap.isEmpty()) {
            for (XAudioImpl audio : mPlayersMap.values()) {
                audio.destroy();
            }
            mPlayersMap.clear();
        }
    }

    @Override
    public XExtensionResult exec(String action, JSONArray args, XCallbackContext callbackCtx) throws JSONException {
        String appId = mWebContext.getApplication().getAppId();
        XExtensionResult.Status status = XExtensionResult.Status.NO_RESULT;
        if (action.equals(COMMAND_PLAY)) {
            XAudioStatusChangeListener listener = new XAudioStatusChangeListener(callbackCtx);
            play(args.getString(0), args.getString(1), mWebContext.getWorkSpace(), listener);
        } else if (action.equals(COMMAND_STOP)) {
            stop(args.getString(0));
        } else if (action.equals(COMMAND_PAUSE)) {
            pause(args.getString(0));
        } else if (action.equals(COMMAND_SEEKTO)) {
            seekTo(args.getString(0), args.getInt(1));
        } else if (action.equals(COMMAND_RELEASE)) {
            boolean ret = release(args.getString(0));
            return new XExtensionResult(status, ret);
        } else if (action.equals(COMMAND_GETCURRENTPOSITION)) {
            int pos = getCurrentPosition(args.getString(0), appId);
            return new XExtensionResult(XExtensionResult.Status.OK, pos);
        } else if (action.equals(COMMAND_STARTRECORDING)) {
            XAudioStatusChangeListener listener = new XAudioStatusChangeListener(callbackCtx);
            startRecording(args.getString(0), args.getString(1), mWebContext.getWorkSpace(), listener);
        } else if (action.equals(COMMAND_STOPRECORDING)) {
            stopRecording(args.getString(0));
        } else if (action.equals(COMMAND_SETVOLUME)) {
            setVolume(args.getString(0), Float.parseFloat(args.getString(1)));
        }

        return new XExtensionResult(status);
    }

    /**
     * Start  resume .
     *
     * @param playerId          audio player  id
     * @param fileAbsPath       ?
     * @param workspacePath     workspcae 
     */
    private void play(String playerId, String fileAbsPath, String workspacePath,
            XAudioStatusChangeListener listener) {
        XAudioImpl audio = mPlayersMap.get(playerId);
        if (audio == null) {
            audio = new XAudioImpl(playerId, workspacePath, listener);
            mPlayersMap.put(playerId, audio);
        }
        audio.start(fileAbsPath);
    }

    /**
     * ? audio.
     *
     * @param playerId    audio player  id
     * @param appId        app  id
     */
    private void stop(String playerId) {
        XAudioImpl audio = mPlayersMap.get(playerId);
        if (audio != null) {
            audio.stop();
        }
    }

    /**
     * ?.
     *
     * @param playerId    audio player  id
     * @param appId        app  id
     */
    private void pause(String playerId) {
        XAudioImpl audio = mPlayersMap.get(playerId);
        if (audio != null) {
            audio.pause();
        }
    }

    /**
     *  audio ??.
     *
     * @param playerId    audio player  id
     * @param miliseconds ??
     * @param appId        app  id
     */
    private void seekTo(String playerId, int milliseconds) {
        XAudioImpl audio = mPlayersMap.get(playerId);
        if (audio != null) {
            audio.seekTo(milliseconds);
        }
    }

    /**
     * ? playback ??.
     *
     * @param playerId    audio player  id
     * @param miliseconds ??
     * @param appId        app  id
     * @return            position  -1
     */
    public int getCurrentPosition(String playerId, String appId) {
        XAudioImpl audio = mPlayersMap.get(playerId);
        if (audio != null) {
            return audio.getCurrentPosition();
        }
        return -1;
    }

    /**
     *  audio .
     *
     * @param playerId    audio player  id
     * @param appId        app  id
     */
    private boolean release(String playerId) {
        XAudioImpl audio = mPlayersMap.get(playerId);
        if (audio != null) {
            mPlayersMap.remove(playerId);
            audio.destroy();
            return true;
        }
        return false;
    }

    /**
     * ?.
     *
     * @param playerId          audio player  id
     * @param outputFileNanem    file nameJS
     * @param appId              app  id
     * @param workspacePath     workspace ?
     */
    private void startRecording(String playerId, String outputFileNanem, String workspacePath,
            XAudioStatusChangeListener listener) {
        // ? recording
        XAudioImpl audio = mPlayersMap.get(playerId);
        if (audio != null) {
            return;
        }

        audio = new XAudioImpl(playerId, workspacePath, listener);
        mPlayersMap.put(playerId, audio);
        audio.startRecording(getContext(), outputFileNanem);
    }

    /**
     * ????workspace.
     *
     * @param playerId        audio player  id
     * @param appId            app  id
     */
    private void stopRecording(String playerId) {
        XAudioImpl audio = mPlayersMap.get(playerId);

        if (audio != null) {
            audio.stopRecording();
            mPlayersMap.remove(playerId);
        }
    }

    /**
     * ?
     *
     * @param playerId   audio player id
     * @param appId      app id
     * @param value      ??
     */
    private void setVolume(String playerId, float value) {
        XAudioImpl audio = mPlayersMap.get(playerId);
        if (audio != null) {
            audio.setVolume(value);
        } else {
            XLog.e(CLASS_NAME, "setVolume() Error: Unknown Audio Player " + playerId);
        }
    }
}