Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

METADATA_KEY_DURATION returns 0 for mp3 on android 6.0.1 but works on other versions #276

Open
zkrige opened this issue Feb 7, 2023 · 2 comments

Comments

@zkrige
Copy link

zkrige commented Feb 7, 2023

                try {
                    FFmpegMediaMetadataRetriever metadataRetriever = new FFmpegMediaMetadataRetriever();
                    ZipMediaDataSource zipMediaDataSource = new ZipMediaDataSource(zipFile, zipEntry);
                    metadataRetriever.setDataSource(zipMediaDataSource);
                    String duration;
                    try {
                        duration = metadataRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);
                        long durationMs = Long.parseLong(duration);
                        chapter.duration = durationFormat(durationMs);
                    } catch (Exception e) {
                        chapter.duration = "n/a";
                    }
                    metadataRetriever.release();
                } catch (IOException e) {
                    chapter.duration = "n/a";
                }

on android 6.0.1 im getting "0" as duration, but on other devices I get the actual duration of the mp3.

@wseemann
Copy link
Owner

Can you post ZipMediaDataSource? The issue is likely due to come API specific behavior in whatever that class is doing.

@zkrige
Copy link
Author

zkrige commented Feb 17, 2023

Turns out im getting 0 duration on all android platforms. I managed to get it working with MediaMetadataRetriever, so I dont think its ZipMediaDatasource that is the problem - also ZipMediaDatasource works fine with MediaMetadataRetriever

@RequiresApi(api = Build.VERSION_CODES.M)
public class InputStreamDataSource extends MediaDataSource {
    protected InputStream inputStream;
    protected ZipFile zipFile;
    protected ZipEntry zipEntry;
    private long currentPosition = 0;

    @Override
    public int readAt(long position, byte[] buffer, int offset, int size) throws IOException {
        if (currentPosition > position) {
            Timber.v("Resetting stream: " + zipEntry.getName());
            reset();
        }
        if (position > 0) {
            long toSkip = position - currentPosition;
            long skipped = inputStream.skip(toSkip);
            while (skipped < toSkip) {
                skipped = skipped + inputStream.skip(toSkip - skipped);
            }
        }
        int bytesRead = inputStream.read(buffer, offset, size);
        if (size != bytesRead) {
            Timber.d("Requested: " + size + " but only got " + bytesRead);
        }
        if (position >= 0) {
            currentPosition = currentPosition + position + bytesRead;
            Timber.v("Current position for " + zipEntry.getName() + ":" + currentPosition);
        }
        return bytesRead;
    }

    private void reset() throws IOException {
        inputStream.close();
        inputStream = zipFile.getInputStream(zipEntry);
        currentPosition = 0;
    }

    @Override
    public long getSize() throws IOException {
        return inputStream.available();
    }

    @Override
    public void close() throws IOException {
        if (inputStream != null) {
            inputStream.close();
            inputStream = null;
        }

    }
}

@RequiresApi(api = Build.VERSION_CODES.M)
public class ZipMediaDataSource extends InputStreamDataSource {

    public ZipMediaDataSource(ZipFile file, ZipEntry zipEntry) throws IOException {
        this.zipFile = file;
        this.zipEntry = zipEntry;
        inputStream = file.getInputStream(zipEntry);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants