Android mediaPlayer.getDuration()返回0或-1 [英] Android mediaPlayer.getDuration() return 0 or -1

查看:280
本文介绍了Android mediaPlayer.getDuration()返回0或-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建在线媒体播放器,我们将在其中使用 URL 播放歌曲.在 Media Player 中,我们显示歌曲的总播放时间,为了显示时间,我们使用了 mediaPlayer.getDuration()函数,但它始终返回 -1

I am creating online media player where we are playing song with the help of URL. In Media Player we are displaying Total time of song, for displaying time we are using mediaPlayer.getDuration() function but it always returning -1.

当我传递投递箱URL时,它可以正常工作,但是当我传递服务器URL(https://)时,则不能正常工作.

When I am passing Drop Box URL then it working but when I pass my server URL (https://) then it not working.

2021-01-25 14:50:33.777 11438-11438/com.dd.xyz W/MediaPlayer: Use of stream types is deprecated for operations other than volume control
2021-01-25 14:50:33.777 11438-11438/com.dd.xyz W/MediaPlayer: See the documentation of setAudioStreamType() for what to use instead with android.media.AudioAttributes to qualify your playback use case
2021-01-25 14:50:33.778 11438-11438/com.dd.xyz V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@6a1c34b): Cookies: null
2021-01-25 14:50:33.783 11438-11663/com.dd.xyz V/MediaHTTPService: makeHTTPConnection: CookieHandler (java.net.CookieManager@ea97e0b) exists.
2021-01-25 14:50:33.783 11438-11663/com.dd.xyz V/MediaHTTPService: makeHTTPConnection(android.media.MediaHTTPService@6a1c34b): cookieHandler: java.net.CookieManager@ea97e0b Cookies: null
2021-01-25 14:50:34.616 11438-11438/com.dd.xyz I/mediaFileLength: -1

下面是代码-

public class MainActivity extends AppCompatActivity {

    private ImageView imageViewPlayPause;
    private TextView textCurrenTime,textTotalDuration;
    private SeekBar playerSeekBar;
    private MediaPlayer mediaPlayer;
    private Handler handler =new Handler();
    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageViewPlayPause=findViewById(R.id.imagePlayPause);
        textCurrenTime=findViewById(R.id.textCurrentTime);
        textTotalDuration=findViewById(R.id.textTotalDuration);
        playerSeekBar=findViewById(R.id.playerSeekBar);

        mediaPlayer=new MediaPlayer();
        playerSeekBar.setMax(100);

        imageViewPlayPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mediaPlayer.isPlaying()){
                    handler.removeCallbacks(updater);
                    mediaPlayer.pause();
                    imageViewPlayPause.setImageResource(R.drawable.ic_play);
                }
                else{
                    mediaPlayer.start();
                    imageViewPlayPause.setImageResource(R.drawable.ic_pause);
                    updateSeekBar();
                }
            }
        });
        prepareMediaPlayer();
        playerSeekBar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                SeekBar seekBar=(SeekBar) view;
                int playPosition=(mediaPlayer.getDuration()/100)*seekBar.getProgress();
                mediaPlayer.seekTo(playPosition);
                textCurrenTime.setText(milliSecondsToTimer(mediaPlayer.getCurrentPosition()));
                return false;
            }
        });
    }

    private void prepareMediaPlayer(){
        try{
            mediaPlayer.setDataSource("https://varkarisanskruti.com/sample.mp3");
            mediaPlayer.prepare();
            textTotalDuration.setText(milliSecondsToTimer(mediaPlayer.getDuration()));
            Log.d("musicPlayerTimehere", String.valueOf(mediaPlayer.getDuration()));
        }
        catch(Exception e)
        {
            Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }

    private Runnable updater=new Runnable() {
        @Override
        public void run() {
            updateSeekBar();
            long currentDuration=mediaPlayer.getCurrentPosition();
            textCurrenTime.setText(milliSecondsToTimer(currentDuration));
        }
    };


    private void updateSeekBar(){
        if(mediaPlayer.isPlaying()){
            playerSeekBar.setProgress((int)(((float) mediaPlayer.getCurrentPosition()/mediaPlayer.getDuration())*100));
            handler.postDelayed(updater,1000);
        }
    }



    private String milliSecondsToTimer(long milliSeconds){
        String timerString="";
        String secondString;
        int hour=(int) (milliSeconds/(1000*60*60));
        int minutes=(int)(milliSeconds%(1000*60*60))/(1000*60);
        int seconds=(int)((milliSeconds%(1000*60*60))%(1000*60)/1000);
        if(hour>0){
            timerString=hour+":";
        }
        if(seconds<10){
            secondString="0"+seconds;
        }
        else {
            secondString=""+seconds;
        }
        timerString=timerString+minutes+":"+secondString;
        return timerString;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我尝试了,但这是为了零,由于隐私/公司政策,我无法使用第三方lib.

I tried this but this is for Zero and I can't use third party lib due to privacy/company policies.

请帮助...

在此先感谢您的帮助!!

Thank you in advance for helping...!

推荐答案

我尝试了 stackoverflow 的多个答案,以下是一些参考,但没有用-

I tried multiple answers from stackoverflow, below are some for your reference but nothing is working -

如何获取mp3文件的持续时间Http通过Android MediaPlayer流式传输

mediaPlayer.getDuration()在setDataSource崩溃后返回0应用

MediaPlayer getDuration返回-1412558917

因此,我分析了 .mp3 文件属性,发现如果 Bit Rate 128kbps ,则 mediaPlayer.getDuration()始终返回 0 -1 .

So I analyzed .mp3 file properties and found that if Bit Rate is 128kbps then mediaPlayer.getDuration() always returns 0 or -1.

我知道您可能不相信这就是为什么我录制了视频并将其上传到Youtube的原因.这是供您参考的链接. https://youtu.be/-AKw59eb6nM

I know you might don't believe that's why I recorded the video and uploaded it on Youtube. Here is link for your reference. https://youtu.be/-AKw59eb6nM

对于我来说,将mp3文件从 128kbps 转换为 320kbps 是解决方案.

For me converting mp3 file to 320kbps from 128kbps is the solution.

这篇关于Android mediaPlayer.getDuration()返回0或-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆