我现在不知道该怎么做,我是Android的新手。
package com.example.macbookpro.myplayer;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class AudioPlayer extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, MediaPlayer.OnCompletionListener {
ImageView btn_play;
Button btn_pause;
ImageView btn_next;
ImageView btn_prev;
ImageView btn_forward;
ImageView btn_backward;
TextView song_title;
int currentsongindex;
int seekForwardTime = 5000;
int seekBackwardTime = 5000;
private Utilities utilities;
Handler mHandler1=new Handler();
SeekBar song_progressbar;
final ArrayList<File> arrayList = new ArrayList<File>();
MediaPlayer mp = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_player);
btn_play = (ImageView) findViewById(R.id.btn_play);
btn_next = (ImageView) findViewById(R.id.btn_next);
btn_prev = (ImageView) findViewById(R.id.btn_prev);
btn_forward = (ImageView) findViewById(R.id.btn_forward);
btn_backward = (ImageView) findViewById(R.id.btn_backwrd);
song_title = (TextView) findViewById(R.id.tw);
song_progressbar=(SeekBar)findViewById(R.id.song_progressbar);
song_progressbar.setOnSeekBarChangeListener(this);
mp.setOnCompletionListener(this);
try {
PlaySongtwo(getIntent().getStringExtra("index"));
} catch (IOException e) {
e.printStackTrace();
}
final String[] sec = getIntent().getStringArrayExtra("index2");
currentsongindex = Integer.parseInt((getIntent().getStringExtra("positionofcurrentsong")));
utilities = new Utilities();
// Button for playing the song
btn_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (mp.isPlaying()) {
if (mp != null) {
mp.pause();
btn_play.setImageResource(R.drawable.button_play);
} else {
if (mp != null) {
mp.start();
btn_play.setImageResource(R.drawable.button_pause);
}
}
} else {
if (mp != null) {
mp.start();
btn_play.setImageResource(R.drawable.button_pause);
}
}
}
});
// Button for the next song in the list
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.e("currenttt song is ",currentsongindex+"");
if(currentsongindex < (sec.length-1)){
int cc=currentsongindex+1;
Log.e("value in cc",cc+"");
try {
PlaySongtwo(sec[cc]);
} catch (IOException e) {
e.printStackTrace();
}
Log.e("next song number is ",cc +"");
currentsongindex = currentsongindex +1;
}else {
try {
PlaySongtwo(sec[0]);
} catch (IOException e) {
e.printStackTrace();
}
currentsongindex = 0;
}
}
});
//Button for the previous song in the list
btn_prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(currentsongindex > 0){
int prevsong= currentsongindex-1;
try {
PlaySongtwo(sec[prevsong]);
} catch (IOException e) {
e.printStackTrace();
}
Log.e("prev song",prevsong+"");
currentsongindex = prevsong;
}else {
try {
PlaySongtwo(String.valueOf((sec.length-1)));
} catch (IOException e) {
e.printStackTrace();
}
currentsongindex = sec.length-1;
}
}
});
//Button for fast-forwarding the song
btn_forward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
int currentPosition = mp.getCurrentPosition();
if(currentPosition + seekForwardTime <= mp.getDuration() ){
mp.seekTo(currentPosition + seekForwardTime);
}else {
mp.seekTo(mp.getDuration());
}
}
});
//Button for fast-backwarding the song
btn_backward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
int currentPostion = mp.getCurrentPosition();
if(currentPostion - seekBackwardTime >= 0){
mp.seekTo(currentPostion - seekBackwardTime);
}else {
mp.seekTo(0);
}
}
});
}
private void PlaySongtwo(String path) throws IOException {
try {
String songname = path.substring(path.lastIndexOf("/") + 1);
song_title.setText(songname);
mp.reset();
Uri myUri = Uri.parse(path);
mp.setDataSource(AudioPlayer.this, myUri);
mp.prepare();
mp.start();
btn_play.setImageResource(R.drawable.button_pause);
song_progressbar.setProgress(0);
song_progressbar.setMax(100);
updateProgressBar1();
} catch (IOException e) {
e.printStackTrace();
}
}
private void updateProgressBar1() {
mHandler1.postDelayed(mUpdateTimeTask1,100);
}
private Runnable mUpdateTimeTask1=new Runnable() {
@Override
public void run() {
long totalDuration = mp.getDuration();
long currentDuration=mp.getCurrentPosition();
int progress=(int)(utilities.getProgresspercentage(currentDuration,totalDuration));
song_progressbar.setProgress(progress);
mHandler1.postDelayed(this,100);
}
};
@Override
protected void onDestroy ()
{
super.onDestroy();
mp.release();
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
mHandler1.removeCallbacks(mUpdateTimeTask1);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mHandler1.removeCallbacks(mUpdateTimeTask1);
int totalDuration=mp.getDuration();
int currentPosition=utilities.progressToTimer(seekBar.getProgress(),totalDuration);
mp.seekTo(currentPosition);
updateProgressBar1();
}
@Override
public void onCompletion(MediaPlayer mp1) {
enter code here
}
}
这是我面临的错误。
11-06 13:30:03.935 1060-1060/com.example.macbookpro.myplayer E/MediaPlayer: Should have subtitle controller already set
11-06 13:30:04.031 1060-1060/com.example.macbookpro.myplayer E/MediaPlayer: Should have subtitle controller already set
11-06 13:30:22.260 1060-1060/com.example.macbookpro.myplayer D/AndroidRuntime: Shutting down VM
11-06 13:30:22.261 1060-1060/com.example.macbookpro.myplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.macbookpro.myplayer, PID: 1060
java.lang.IllegalStateException
at android.media.MediaPlayer.getDuration(Native Method)
at com.example.macbookpro.myplayer.AudioPlayer$6.run(AudioPlayer.java:220)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
我的猜测是,在MediaPlayer准备好使用之前,您正尝试使用DarDuration。 确保您正在设置与媒体播放器互动的所有控件。