Getting an error in intellij that states I have not defined a onPrepared method in my setOnPreparedListener class. Have reviewed the documentation and my code for the onPrepared method matches what's defined.
Code:
import android.app.IntentService;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.util.Log;
import java.io.IOException;
/**
* Created by tim on 4/19/2017.
*
*/
public class TonePlayerIntentService extends IntentService implements MediaPlayer.OnErrorListener,
MediaPlayer.OnPreparedListener,
MediaPlayer.OnCompletionListener{
private static final String LOG_TAG = "actotracker " + TonePlayerIntentService.class.getSimpleName();
MediaPlayer mMediaPlayer = null;
Uri chime = null;
public TonePlayerIntentService() {
super(LOG_TAG);
}
/**
* called everytime an intent is launched
*/
@Override
public void onCreate() {
super.onCreate();
chime = Uri.parse("android.resource://"+getApplicationContext().getPackageName()+"//raw//chime.mp3");
}
/**
* Handles incoming intents.
* @param intent The Intent is provided (inside a PendingIntent) when requestActivityUpdates()
* is called.
*/
@Override
protected void onHandleIntent(Intent intent) {
Log.d(LOG_TAG,"IN onHandleIntent");
this.onStartCommand();
}
public int onStartCommand(){
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setLooping(false);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mMediaPlayer.setDataSource(getApplicationContext(), chime);
}catch (IOException ie){
DatabaseProcessor.getInstance().logErrorEvent(ie);
}
mMediaPlayer.setOnPreparedListener(new OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
mMediaPlayer.prepareAsync();
mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener(){
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
cleanUpPlay();
return true;
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
cleanUpPlay();
}
});
return 1;
}
public void cleanUpPlay(){
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
The error message states that onPrepared(MediaPlayer) must be defined in the setOnPreparedListener class.
Would appreciate information on what I did wrong. Happily provide more information if requested.
Updated with complete code.
Try calling mMediaPlayer.prepareAsync(); after you have set your OnPreparedListener
Edit: remove implements and the classnames behind it. Your class is not implementing any interfaces, since you are implementing them inline, and remove this line as well.
mMediaPlayer.setOnCompletionListener(this);