MediaPlayer停止按钮崩溃应用程序

时间:2016-03-31 22:22:22

标签: android android-mediaplayer

我有一个工作正常的MediaPlayer,停止按钮工作正常。但是,如果用户进入活动并按下停止按钮而不播放任何音乐,则应用程序崩溃。如果播放了一首歌曲,用户可以按任意次数按停止按钮,它可以正常工作。任何帮助都会很棒!

public class Website extends AppCompatActivity {

private MediaPlayer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridviewmusic);

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new MyAdapter(this));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            switch (position) {
                case 0:
                    stopPlaying();
                    mp = MediaPlayer.create(Website.this, R.raw.sound1);
                    mp.start();

                    // This will make  sound
                    break;
                case 1:
                    stopPlaying();
                    mp = MediaPlayer.create(Website.this, R.raw.sound2);
                    mp.start();
                    break;
                case 2:
                    stopPlaying();
                    mp = MediaPlayer.create(Website.this, R.raw.sound3);
                    mp.start();
                    break;
                case 3:
                    stopPlaying();
                    mp = MediaPlayer.create(Website.this, R.raw.sound4);
                    mp.start();
                    break;
                case 4:
                    stopPlaying();
                    mp = MediaPlayer.create(Website.this, R.raw.sound5);
                    mp.start();
                    break;
                case 5:
                    Intent i = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("https://play.google.com/store/music/"));
                    startActivity(i);
                    break;

            }

        }
    });
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mp.isPlaying()) {
                mp.pause();
                mp.seekTo(0);
            }
        }
    });
}


private void stopPlaying() {
    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;
    }
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    if(mp!= null)
    {
        mp.stop();
        mp=null;
    }
    this.finish();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            if(mp!= null)
            {
                mp.stop();
                mp=null;
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);


    }
}
}

1 个答案:

答案 0 :(得分:2)

试试这个:

fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mp!=null && mp.isPlaying()) {
            mp.pause();
            mp.seekTo(0);
        }
    }
});