Uploaded image for project: 'Apache Cordova'
  1. Apache Cordova
  2. CB-6144

Media Plugin throws Null Pointer Exception - Android 4.4.2

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 3.3.0
    • None
    • cordova-plugin-media
    • None
    • Android 4.4.2 - Nexus 7

    Description

      Here's my scenario

      1) Start Recording using the Media plugin
      theMedia = new Media(localFile, mediaSuccess, mediaError, mediaStatus);
      theMedia.startRecord();
      2) Continue for 1 minute
      3) Stop recording and confirm that the file exists on the sdcard.
      theMedia.stopRecord();
      4) Play the file recorded in steps 1 - 3.

      Stacktrace is attached to the issue.

      theMedia.play();
      5) The plugin throws a Null Pointer Exception at the line marked with //--> THROWS A NPE.
      6) I have also shown a possible solution at the line marked with // POSSIBLE SOLUTION.

      <code>
      /**

      • attempts to initialize the media player for playback
      • @param file the file to play
      • @return false if player not ready, reports if in wrong mode or state
        */
        private boolean readyPlayer(String file) {
        if (playMode()) {
        switch (this.state) {
        case MEDIA_NONE:
        if (this.player == null) { this.player = new MediaPlayer(); }

        try

        { this.loadAudioFile(file); }

        catch (Exception e)

        Unknown macro: { this.handler.webView.sendJavascript("cordova.require('org.apache.cordova.media.Media').onStatus('" + this.id + "', "+MEDIA_ERROR+", { \"code\":"+MEDIA_ERR_ABORTED+"});"); }

        return false;
        case MEDIA_LOADING:
        //cordova js is not aware of MEDIA_LOADING, so we send MEDIA_STARTING instead
        Log.d(LOG_TAG, "AudioPlayer Loading: startPlaying() called during media preparation: " + STATE.MEDIA_STARTING.ordinal());
        this.prepareOnly = false;
        return false;
        case MEDIA_STARTING:
        case MEDIA_RUNNING:
        case MEDIA_PAUSED:
        return true;
        case MEDIA_STOPPED:
        //if we are readying the same file
        if (this.audioFile.compareTo(file) == 0) {
        //reset the audio file

      /*** POSSIBLE SOLUTION: TO FIX NPE */
      if (player == null) {
      player = new MediaPlayer();
      try

      { this.loadAudioFile(file); }

      catch (Exception e) {
      this.handler.webView.sendJavascript("cordova.require('org.apache.cordova.media.Media').onStatus('" + this.id + "', "MEDIA_ERROR",

      { \"code\":"+MEDIA_ERR_ABORTED+"}

      );");
      }
      }
      player.seekTo(0); //---> THROWS NULL POINTER EXCEPTION
      player.pause();
      return true;
      } else {
      //reset the player
      this.player.reset();
      try

      { this.loadAudioFile(file); }

      catch (Exception e) {
      this.handler.webView.sendJavascript("cordova.require('org.apache.cordova.media.Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ",

      { \"code\":" + MEDIA_ERR_ABORTED + "}

      );");
      }
      //if we had to prepare= the file, we won't be in the correct state for playback
      return false;
      }
      default:
      Log.d(LOG_TAG, "AudioPlayer Error: startPlaying() called during invalid state: " + this.state);
      this.handler.webView.sendJavascript("cordova.require('org.apache.cordova.media.Media').onStatus('" + this.id + "', " + MEDIA_ERROR + ",

      { \"code\":" + MEDIA_ERR_ABORTED + "}

      );");
      }
      }
      return false;
      }
      </code>

      Attachments

        1. stacktrace.txt
          1 kB
          Ashwin Desai

        Activity

          People

            Unassigned Unassigned
            ashwinde Ashwin Desai
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: