Java I';我正在构建一个媒体播放器应用程序,我的应用程序遇到了一个致命的异常

Java I';我正在构建一个媒体播放器应用程序,我的应用程序遇到了一个致命的异常,java,android,android-mediaplayer,Java,Android,Android Mediaplayer,我不知道现在该怎么办,我对安卓系统还不熟悉 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; i

我不知道现在该怎么办,我对安卓系统还不熟悉

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
    }
}

我的猜测是,您正在尝试在MediaPlayer准备好使用之前获取持续时间。
确保您正在设置与mediaplayer交互的所有控件。

提示:有关堆栈溢出的问题需要花费一些精力。闲聊的标题,请停止我的乞讨,txtspk,所有大写的叫喊,正文中没有段落文本,以及未格式化的日志摘录将鼓励读者滚动眼睛并点击向下投票按钮。这也是理想的,当你收到一个异常时,你做了一些研究——不要把问问题作为你做的第一件事!在你花了半个小时左右的时间研究这个问题之前,你不会陷入困境。