Android mediaPlayer.getDuration() return 0 或 -1
Android mediaPlayer.getDuration() return 0 or -1
我正在创建在线媒体播放器,我们在 URL
的帮助下播放歌曲。在 Media Player
中,我们显示歌曲的总时间,为了显示时间,我们使用 mediaPlayer.getDuration()
函数,但它总是返回 -1
。
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);
}
}
我尝试了 ,但这是针对零的,由于 privacy/company 政策,我无法使用第三方库。
请帮忙...
提前感谢您的帮助...!
我尝试了 Whosebug
的多个答案,下面是一些供您参考但没有任何效果 -
所以我分析了.mp3
文件属性,发现如果Bit Rate
是128kbps
那么mediaPlayer.getDuration()
总是returns0
或者-1
.
我知道你可能不相信这就是我录制视频并将其上传到 Youtube 的原因。这是link供您参考。 https://youtu.be/-AKw59eb6nM
对我来说,将 mp3 文件从 128kbps 转换为 320kbps 是解决方案。
我正在创建在线媒体播放器,我们在 URL
的帮助下播放歌曲。在 Media Player
中,我们显示歌曲的总时间,为了显示时间,我们使用 mediaPlayer.getDuration()
函数,但它总是返回 -1
。
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);
}
}
我尝试了
请帮忙...
提前感谢您的帮助...!
我尝试了 Whosebug
的多个答案,下面是一些供您参考但没有任何效果 -
所以我分析了.mp3
文件属性,发现如果Bit Rate
是128kbps
那么mediaPlayer.getDuration()
总是returns0
或者-1
.
我知道你可能不相信这就是我录制视频并将其上传到 Youtube 的原因。这是link供您参考。 https://youtu.be/-AKw59eb6nM
对我来说,将 mp3 文件从 128kbps 转换为 320kbps 是解决方案。