JNIEXPORT jint JNICALL Java_com_axelby_mp3decoders_MPG123_getSeekFrameOffset (JNIEnv *env, jclass c, jlong mp3file, jfloat seconds) { MP3File *mp3 = (MP3File *)mp3file; mpg123_handle *mh = mp3->handle; size_t fill; off_t step; off_t* offsets; mpg123_index(mh, &offsets, &step, &fill); int target_frame = (int) (seconds / mp3->secs_per_frame); // the frame number to seek to int target_index = target_frame / step; // the closest index to the target frame // say so if there aren't enough entries in the index if (target_index >= fill) return -1; return offsets[target_index]; }
static int print_index(mpg123_handle *mh) { int err; size_t c, fill; off_t *index; off_t step; err = mpg123_index(mh, &index, &step, &fill); if(err == MPG123_ERR) { fprintf(stderr, "Error accessing frame index: %s\n", mpg123_strerror(mh)); return err; } for(c=0; c < fill;++c) fprintf(stderr, "[%lu] %lu: %li (+%li)\n", (unsigned long) c, (unsigned long) (c*step), (long) index[c], (long) (c ? index[c]-index[c-1] : 0)); return MPG123_OK; }