Creating a MediaPlayer
.
The particular class has the functions to play, pause and resume. I am trying to make a SeekBar
which will display the progress.
SeekBar
requires as the value of the maxlength
which is to be obtained from the getDuration()
function which calls the mediaplayer.getDuration()
function and returns the value.
SeekBar
also requires the value of the current position which is obtained from the getCurrentPostion()
function.
Both of them are returning a NullPointerException
.
public class PlayMedia extends AsyncTask<Void, Void, Void>
implements MediaPlayer.OnPreparedListener {
private MediaPlayer mediaPlayer;
int length;
int duration =0;
int currentPosition=0;
@Override
protected Void doInBackground(Void... params) {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/Notate/"+MainActivity.filepath+".wav");
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
public int getDuration(){
duration=mediaPlayer.getDuration();
Log.d("Position",Integer.toString(duration));
return duration;
}
public int getPresentDuration(){
currentPosition=mediaPlayer.getCurrentPosition();
Log.d("CurrentPosition",Integer.toString(currentPosition));
return currentPosition;
}
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
if(mediaPlayer != null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
else if(mediaPlayer != null){
mediaPlayer.start();
}
Log.d("Shivram", "Started Playing");
}
public void pause(){
mediaPlayer.pause();
length=mediaPlayer.getCurrentPosition();
Log.d("Shivram","Paused");
}
public void resume(){
mediaPlayer.seekTo(length);
mediaPlayer.start();
Log.d("Shivram","Resume");
This Fragment Class calls the getDuration()
function, where the exception is created.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
contentViewC = inflater.inflate(R.layout.fragment_fragment_b, container, false);
final SeekBar seekBar;
Button button = (Button) contentViewC.findViewById(R.id.button1);
Button button2 = (Button) contentViewC.findViewById(R.id.button2);
textToChange=(TextView)contentViewC.findViewById(R.id.textView1);
final PlayMedia play=new PlayMedia();
boolean pause=false;
seekBar = (SeekBar) contentViewC.findViewById(R.id.SeekBar01);
final int[] recordingDuration = new int[1];
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (MainActivity.filepath != "Nothing") {
if(play.getStatus()==AsyncTask.Status.FINISHED || play.getStatus()==AsyncTask.Status.RUNNING){
Log.d("Shivram-FragmentA", "Resume");
play.resume();
}
if(play.getStatus()==AsyncTask.Status.PENDING) {
play.execute();
recordingDuration[0] =play.getDuration();
seekBar.setMax(recordingDuration[0]);
}
Frequency getFreq=new Frequency();
getFreq.execute();
}
}
});
The stackTrace is as follows
01-13 21:44:15.182 16263-16263/com.example.shivram.notate E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.shivram.notate, PID: 16263
java.lang.NullPointerException
at com.example.shivram.notate.PlayMedia.getDuration(PlayMedia.java:43)
at com.example.shivram.notate.FragmentC$1.onClick(FragmentC.java:66)
at android.view.View.performClick(View.java:4456)
at android.view.View$PerformClick.run(View.java:18465)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)