mediaplayer getduration returns 0 only in my server files

限于喜欢 提交于 2021-02-20 03:49:38

问题


i'm trying to get the audio file duration from my server using media player

MediaPlayer mp = new MediaPlayer();
    try {
        mp.reset();
        mp.setDataSource("link here");
        mp.prepare();

    }catch (IOException e){
        e.printStackTrace();
    }


    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            Log.e("time",mp.getDuration()+" ..");
        }
    });

when i put my server file link , it always return 0 but when i use another link from the web it gives me the right duration

do i need to do some configuration for that ?


回答1:


If you cannot get duration of an audio link from you server, then you can try this library

https://github.com/wseemann/FFmpegMediaMetadataRetriever

To retrieve meta data (including duration) from an input media file.

First write a method to get duration.

private int getDurationInMilliseconds(String path) {
    FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
    mmr.setDataSource(path);
    int duration = Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION));
    mmr.release();
    return duration;
}

Then change your code

final String path = "http://dleelbaha.com/fayziah/download/sound/1536950612.mp3";
final MediaPlayer mp = new MediaPlayer();
try {
    mp.reset();
    mp.setDataSource(path);
    mp.prepare();
} catch (IOException e) {
    e.printStackTrace();
}

mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        int duration = mp.getDuration();
        if (duration <= 0) {
            duration = getDurationInMilliseconds(path);
        }
        Log.i("time", duration + " ms");
    }
});



回答2:


Looks like your stream is not providing the duration.

For clarification checkout the below log from mediaCodec.

D/MediaCodec: (0xf0aba000) configure format: AMessage(what = 0x00000000) = {
                                                    string mime = "audio/mpeg"
                                                    int32_t bitrate = 64000
                                                    int32_t channel-count = 2
                                                    int32_t sample-rate = 22050
                                                    int32_t ape-sample-per-frame = 576
                                                    int32_t priority = 0
                                                  }

Same logs for working stream.

09-15 21:25:16.253 1908-26415/? D/MediaCodec: (0xf0aba000) configure format: AMessage(what = 0x00000000) = {
                                                    string mime = "audio/raw"
                                                    int64_t durationUs = 888163
                                                    int32_t channel-count = 1
                                                    int32_t sample-rate = 44100
                                                    int32_t channel-mask = 0
                                                    int32_t pcm-encoding = 2
                                                    int32_t endian = 2
                                                    int32_t bit-width = 16
                                                    int32_t pcm-type = 1
                                                    int32_t numerical-type = 1
                                                    int32_t max-input-size = 32768
                                                    int32_t max-queue-buffer = 2
                                                    int32_t input-buffer-number = 4
                                                    int32_t priority = 0
}

Alternate approach would be downloading the raw data and embedding it in the application.

mp = MediaPlayer.create(this, R.raw.example);


来源:https://stackoverflow.com/questions/52345298/mediaplayer-getduration-returns-0-only-in-my-server-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!