h6ah4i / android-openslmediaplayer

Re-implementation of Android's MediaPlayer and audio effect classes based on OpenSL ES APIs.
https://openslmediaplayer.h6ah4i.com
Apache License 2.0
417 stars 97 forks source link

onCompletion callback not called #18

Closed davidgassner closed 8 years ago

davidgassner commented 8 years ago

The onCompletion method isn't being called when an audio cue is finished playing. I've tried it with a couple of different audio files and seen the same thing. The same code and files do work with a standard MediaPlayer. My code:

mPlayerFactory = new OpenSLMediaPlayerFactory(mContext);
mMediaPlayer = mPlayerFactory.createMediaPlayer();
mMediaPlayer.setOnPreparedListener(new IBasicMediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(IBasicMediaPlayer mp) {
         mPrepared = true;
         mService.onPrepared(mCue.getCueId());
           startCue(mMediaPlayer);
      }
   });
   mMediaPlayer.setOnCompletionListener(new IBasicMediaPlayer.OnCompletionListener() {
     @Override
     public void onCompletion(IBasicMediaPlayer mp) {
         Log.i(TAG, "onCompletion");
         stopAndRemove();
     }
 });

FileInputStream stream = null;
try {
    stream = new FileInputStream(fullFileName);
    mMediaPlayer.setDataSource(stream.getFD());
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setVolume(this.mVolume, this.mVolume);
    mMediaPlayer.prepare();
} catch (IOException e) {
    e.printStackTrace();
    return false;
} finally {
    if (stream != null) {
        try {
            stream.close();
        } catch (IOException ignore) {
        }
    }
}

Any advice is appreciated, thanks!

davidgassner commented 8 years ago

Disregard this, I changed the call to setDataSource() to use a Uri and it now works.