You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by "Ashwin Desai (JIRA)" <ji...@apache.org> on 2014/03/01 13:17:19 UTC
[jira] [Updated] (CB-6144) Media Plugin throws Null Pointer
Exception - Android 4.4.2
[ https://issues.apache.org/jira/browse/CB-6144?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Ashwin Desai updated CB-6144:
-----------------------------
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.
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) {
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>
was:
Here's my scenario
1) Start Recording using the Media plugin
2) Continue for 1 minute
3) Stop recording and confirm that the file exists on the sdcard.
4) call media.play() to play the file recorded in steps 1 - 3.
5) The plugin throws a Null Pointer Exception.
<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) {
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
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);
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>
> Media Plugin throws Null Pointer Exception - Android 4.4.2
> ----------------------------------------------------------
>
> Key: CB-6144
> URL: https://issues.apache.org/jira/browse/CB-6144
> Project: Apache Cordova
> Issue Type: Bug
> Components: Plugin Media
> Affects Versions: 3.3.0
> Environment: Android 4.4.2 - Nexus 7
> Reporter: Ashwin Desai
> Attachments: stacktrace.txt
>
>
> 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.
> 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) {
> 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>
--
This message was sent by Atlassian JIRA
(v6.1.5#6160)