Can have a play to suspend the function such as
In addition can also trigger seekbar to control music playback effect
But a running app is error
False alarm,
Under Caused by: Java. Lang. NullPointerException: Attempt to invoke virtual method 'void android. Widget. The SeekBar. SetOnSeekBarChangeListener (android. The widget. The SeekBar $OnSeekBarChangeListener)' on a null object reference
At com. Example. User. Lab2_leemingchak. MainActivity. OnCreate (43) MainActivity. Java:
Package com. Example. User. Lab2_leemingchak;
The import android. Media. AudioManager;
The import android. Media. MediaPlayer;
The import android.net.Uri;
The import android. OS. Handler;
The import android. Support. V7. App. AppCompatActivity;
The import android. OS. Bundle;
The import android. View. The view;
The import android. Widget. The Button;
The import android. Widget. ImageButton;
The import android. Widget. ProgressBar;
The import android. Widget. SeekBar;
Public class MainActivity extends AppCompatActivity implements MediaPlayer OnPreparedListener,
MediaPlayer. OnCompletionListener {
Private MediaPlayer MediaPlayer.
Private SeekBar SeekBar.
Private Runnable Runnable.
Private Handler handble;
Private int I;
ImageButton playbtn stopbtn;
@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
setContentView(R.layout.activity_main);
//the initialize button the UIs
Playbtn=(ImageButton) the findViewById (R.i d.p layBtn);
Stopbtn=(ImageButton) the findViewById (R.i d.s topBtn);
//convert the String url to Uri format. Using the parse ()
Uri path=Uri. Parse (" android resource://"+//set the music file in res/raw as default
GetPackageName () + "/" + R.r aw. Enm);
//disable the Play and Stop buttons using the setEnabled ()
Playbtn. SetEnabled (false);
Stopbtn. SetEnabled (false);
//new a MediaPlayer object
Mediaplayer=new mediaplayer ();
//set OnPreparedListener () and OnCompletionListener () to the MediaPlayer object
Mediaplayer. SetOnPreparedListener (this);
Mediaplayer. SetOnCompletionListener (this);
Seekbar. SetOnSeekBarChangeListener (new seekbar. OnSeekBarChangeListener () {
@ Override
Public void onProgressChanged (SeekBar SeekBar, int progress, Boolean fromUser) {
I=progress;
If (fromUser) {
Mediaplayer. SeekTo (progress);
}
}
@ Override
Public void onStartTrackingTouch (SeekBar SeekBar) {
}
@ Override
Public void onStopTrackingTouch (SeekBar SeekBar) {
}
});
//prepare the and download the audio file, the onPrepared () method will be run when ready to play
Try {
Mediaplayer. Reset ();
Mediaplayer. SetDataSource (this, path);
Mediaplayer. SetAudioStreamType (AudioManager. STREAM_MUSIC);//in streaming mode
Mediaplayer. PrepareAsync ();
} the catch (Exception e) {
e.printStackTrace();
}
Seekbar=(seekbar) the findViewById (R.i d.s eekBar);
}
//the when Play/Pause button is clicked
//check been the MediaPlayer is playing or not and start playing the if not, otherwise, the pause playback
Public void onPlayUrlClick View (View) {
{if (mediaplayer. IsPlaying ())
Mediaplayer. Pause ();
Playbtn. SetEnabled (true);
Stopbtn. SetEnabled (true);
Playbtn. SetImageResource (R.d rawable. Play);
} else {
Mediaplayer. Start ();
Playbtn. SetEnabled (true);
Stopbtn. SetEnabled (true);
Playbtn. SetImageResource (R.d rawable. Puase);
ChangeSeekbar ();
}
}
//when the Stop button is clicked
Public void onStopClick View (View) {
Mediaplayer. SeekTo (0);
Playbtn. SetEnabled (true);
Stopbtn. SetEnabled (true);
}
@ Override
//when the audio file is ready to play
//enable the Play button and disable the Stop button
Public void onPrepared (MediaPlayer MediaPlayer) {
Seekbar. SetMax (mediaplayer. GetDuration ());
ChangeSeekbar ();
Playbtn. SetEnabled (true);
Stopbtn. SetEnabled (false);
}
Private void changeSeekbar () {
Seekbar. SetProgress (mediaplayer. GetCurrentPosition ());
{if (mediaplayer. IsPlaying ())
A runnable=new runnable () {
@ Override
Public void the run () {
ChangeSeekbar ();
}
};
Handble. PostDelayed (runnable, 1000);
}
};
@ Override
//when the audio file plays to the end
Public void onCompletion (MediaPlayer MediaPlayer) {
Mediaplayer. SeekTo (0);
}
Public void onbackClick View (View) {
I=I - 5;
Mediaplayer. SeekTo (I);
}
Public void onskipClick View (View) {
I=I + 5;
Mediaplayer. SeekTo (I);
}
}
The UI interface
<? The XML version="1.0" encoding="utf-8"?>
XMLNS: tools="http://schemas.android.com/tools"
Android: layout_width="match_parent"
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull