framework/src/org/apache/cordova/AudioPlayer.java
/**
     * Start or resume playing audio file.
     * 
     * @param file              The name of the audio file.
     */
Start or resume playing audio file.

@param file              The name of the audio file.
Start or resume playing audio file . @param file The name of the audio file .
public void startPlaying(String file) {
        if (this.recorder != null) {
            Log.d(LOG_TAG, "AudioPlayer Error: Can't play in record mode.");
            this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
        }
        else if ((this.mPlayer == null) || (this.state == MEDIA_STOPPED)) {
            try {
                if (this.mPlayer != null) {
                    this.mPlayer.reset();
                }
                else {
                    this.mPlayer = new MediaPlayer();
                }
                this.audioFile = file;
                if (this.isStreaming(file)) {
                    this.mPlayer.setDataSource(file);
                    this.mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    this.setState(MEDIA_STARTING);
                    this.mPlayer.setOnPreparedListener(this);
                    this.mPlayer.prepareAsync();
                }
                else {
                    if (file.startsWith("/android_asset/")) {
                        String f = file.substring(15);
                        android.content.res.AssetFileDescriptor fd = this.handler.cordova.getActivity().getAssets().openFd(f);
                        this.mPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
                    }
                    else {
                        File fp = new File(file);
                        if (fp.exists()) {
                            FileInputStream fileInputStream = new FileInputStream(file);
                            this.mPlayer.setDataSource(fileInputStream.getFD());
                        }
                        else {
                            this.mPlayer.setDataSource("/sdcard/" + file);
                        }
                    }
                    this.setState(MEDIA_STARTING);
                    this.mPlayer.setOnPreparedListener(this);
                    this.mPlayer.prepare();
                    this.duration = getDurationInSeconds();
                }
            } catch (Exception e) {
                e.printStackTrace();
                this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
            }
        }
        else {
            if ((this.state == MEDIA_PAUSED) || (this.state == MEDIA_STARTING)) {
                this.mPlayer.start();
                this.setState(MEDIA_RUNNING);
            }
            else {
                Log.d(LOG_TAG, "AudioPlayer Error: startPlaying() called during invalid state: " + this.state);
                this.handler.sendJavascript("cordova.require('cordova/plugin/Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});");
            }
        }
    }
public void startPlaying ( String file ) { if ( this . recorder != null ) { Log . d ( LOG_TAG , "AudioPlayer Error: Can't play in record mode." ) ; this . handler . sendJavascript ( "cordova.require('cordova/plugin/Media').onStatus('" + this . id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});" ) ; } else if ( ( this . mPlayer == null ) || ( this . state == MEDIA_STOPPED ) ) { try { if ( this . mPlayer != null ) { this . mPlayer . reset ( ) ; } else { this . mPlayer = new MediaPlayer ( ) ; } this . audioFile = file ; if ( this . isStreaming ( file ) ) { this . mPlayer . setDataSource ( file ) ; this . mPlayer . setAudioStreamType ( AudioManager . STREAM_MUSIC ) ; this . setState ( MEDIA_STARTING ) ; this . mPlayer . setOnPreparedListener ( this ) ; this . mPlayer . prepareAsync ( ) ; } else { if ( file . startsWith ( "/android_asset/" ) ) { String f = file . substring ( 15 ) ; android . content . res . AssetFileDescriptor fd = this . handler . cordova . getActivity ( ) . getAssets ( ) . openFd ( f ) ; this . mPlayer . setDataSource ( fd . getFileDescriptor ( ) , fd . getStartOffset ( ) , fd . getLength ( ) ) ; } else { File fp = new File ( file ) ; if ( fp . exists ( ) ) { FileInputStream fileInputStream = new FileInputStream ( file ) ; this . mPlayer . setDataSource ( fileInputStream . getFD ( ) ) ; } else { this . mPlayer . setDataSource ( "/sdcard/" + file ) ; } } this . setState ( MEDIA_STARTING ) ; this . mPlayer . setOnPreparedListener ( this ) ; this . mPlayer . prepare ( ) ; this . duration = getDurationInSeconds ( ) ; } } catch ( Exception e ) { e . printStackTrace ( ) ; this . handler . sendJavascript ( "cordova.require('cordova/plugin/Media').onStatus('" + this . id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});" ) ; } } else { if ( ( this . state == MEDIA_PAUSED ) || ( this . state == MEDIA_STARTING ) ) { this . mPlayer . start ( ) ; this . setState ( MEDIA_RUNNING ) ; } else { Log . d ( LOG_TAG , "AudioPlayer Error: startPlaying() called during invalid state: " + this . state ) ; this . handler . sendJavascript ( "cordova.require('cordova/plugin/Media').onStatus('" + this . id + "', " + MEDIA_ERROR + ", { \"code\":" + MEDIA_ERR_ABORTED + "});" ) ; } } }
Start or resume playing audio file.
Start or resume playing audio file .
// If this is a new request to play audio, or stopped // If stopped, then reset player // Otherwise, create a new one // If streaming file // If local file // Get duration // If we have already have created an audio player // If player has been paused, then resume playback