如何从停止到开始改变歌曲的状态?

时间:2011-10-05 04:18:34

标签: android android-mediaplayer

我在android中创建了一个应用程序,我在xml中使用了三个按钮,在java中我设置了onclick事件,

当点击播放按钮时,准备并播放歌曲, 单击停止按钮时,歌曲停止, 单击暂停按钮时,歌曲暂停。

但我的问题是,当我点击停止按钮后点击播放按钮歌曲后,单击播放按钮后歌曲停止播放,但此时歌曲未播放,

我为此应用创建的代码。如下。

package com.mydemo;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MydemoActivity extends Activity {
/** Called when the activity is first created. */

Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mp = MediaPlayer.create(this, R.raw.fluet);

    bPlay = (Button) findViewById(R.id.bPlay);
    bPause = (Button)findViewById(R.id.bPause);
    bStop = (Button) findViewById(R.id.bStop);

    bPause.setVisibility(View.GONE);

    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer arg0) {

            position = 0;
            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);

        }
    });

    bPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if(position > 0)
            {
                mp.seekTo(position);
                mp.start();
            }

            try {
                mp.prepare();
            } catch (IllegalStateException e)     {                 
                e.printStackTrace();
            } catch (IOException e) {                   
                e.printStackTrace();
            }
            mp.start();

            bPlay.setVisibility(View.GONE);
            bPause.setVisibility(View.VISIBLE);
        }
    });

    bPause.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {            

            if(mp!=null)
            {
                mp.pause();
                position = mp.getCurrentPosition();
            }

            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);
        }
    });        

    bStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            mp.stop();
            position = 0;

            bPlay.setVisibility(View.VISIBLE);
                bPause.setVisibility(View.GONE);                
        }
    });
  }
}

请参阅此更新的代码:

package com.mydemo;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MydemoActivity extends Activity {
/** Called when the activity is first created. */

Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mp = MediaPlayer.create(this, R.raw.fluet);

    bPlay = (Button) findViewById(R.id.bPlay);
    bPause = (Button)findViewById(R.id.bPause);
    bStop = (Button) findViewById(R.id.bStop);

    bPause.setVisibility(View.GONE);

    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer arg0) {

            position = 0;
            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);

        }
    });

    bPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {



            if(mp.isPlaying() == false)
            {
                try {
                    mp.prepare();
                } catch (IllegalStateException e) {                 
                    e.printStackTrace();
                } catch (IOException e) {                   
                    e.printStackTrace();
                }
                mp.start();
            }


            bPlay.setVisibility(View.GONE);
            bPause.setVisibility(View.VISIBLE);
        }
    });

    bPause.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {            

            if(mp!=null)
            {
                mp.pause();
                position = mp.getCurrentPosition();
            }

            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);
        }
    });        

    bStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            mp.stop();
            position = 0;

            bPlay.setVisibility(View.VISIBLE);
             bPause.setVisibility(View.GONE);               
        }
    });
 }
}

2 个答案:

答案 0 :(得分:1)

编辑::
你需要在游戏事件中应用条件,如::

if(mp.isplaying == false)
{
   //play the song
}

答案 1 :(得分:0)

 bStop.setOnClickListener(new View.OnClickListener() 
        {

        @Override
        public void onClick(View arg0) {

            mp.release();
            position = 0;

            bPlay.setVisibility(View.VISIBLE);
             bPause.setVisibility(View.GONE);               
        }
    });
bStop.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {

       if(mp.isplaying == false)
        {
   mp = MediaPlayer.create(this, R.raw.fluet);

        }
    }
});