如何在Android中停止后再次播放歌曲?

时间:2011-09-17 05:54:37

标签: android

你好朋友, 我在android中创建了一个应用程序,其中我在radiogroup中选择了三个单选按钮,然后选择“播放”和“停止”两个按钮。 我的问题是,当我播放这首歌时它播放得很好但是当我点击停止按钮然后点击播放按钮然后歌曲没有播放时, 怎么做 ?如果有人知道,请帮助我。 下面是我写的代码:

 package com.mediaplayer;

import java.io.IOException;

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

public class MediaplayerActivity extends Activity implements OnCheckedChangeListener {

MediaPlayer song1,song2,song3;
int whatsong = 0;

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



    RadioGroup rgMusic = (RadioGroup) findViewById(R.id.radioGroup1);

    song1 = MediaPlayer.create(MediaplayerActivity.this, R.raw.fluet);
    song2 = MediaPlayer.create(MediaplayerActivity.this, R.raw.mogra_na_phool);
    song3 = MediaPlayer.create(MediaplayerActivity.this, R.raw.airtel);


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

    rgMusic.setOnCheckedChangeListener(this);

    bPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {           

            if(song1.isPlaying())
            {
                song1.pause();
                song1.seekTo(0);
            }

            if(song2.isPlaying())
            {
                song2.pause();
                song2.seekTo(0);
            }

            if(song3.isPlaying())
            {
                song3.pause();
                song3.seekTo(0);
            }

            switch (whatsong) {

            case 1:
                try {
                    song1.prepare();
                } catch (IllegalStateException e) {                 
                    e.printStackTrace();
                } catch (IOException e) {                   
                    e.printStackTrace();
                }
                song1.start();
                break;  

            case 2:
                try {
                    song2.prepare();
                } catch (IllegalStateException e) {                 
                    e.printStackTrace();
                } catch (IOException e) {                   
                    e.printStackTrace();
                }
                song2.start();
                break;

            case 3:
                try {
                    song3.prepare();
                } catch (IllegalStateException e) {                     
                    e.printStackTrace();
                } catch (IOException e) {               
                    e.printStackTrace();
                }
                song3.start();
                break;
            }
        }
    });

    bStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {       

            switch (whatsong) {

            case 1:
                song1.stop();
                break;  

            case 2:
                song2.stop();
                break;

            case 3:
                song3.stop();
                break;
            }           
        }
    });                                      
}

@Override
public void onCheckedChanged(RadioGroup group, int rbId) {      

    switch (rbId) {
    case R.id.rbMusic1:
        whatsong = 1;           
        break;
    case R.id.rbMusic2:
        whatsong = 2;           
        break;
    case R.id.rbMusic3:
        whatsong = 3;           
        break;

    }

}

}

4 个答案:

答案 0 :(得分:2)

现在使用此编辑的代码。这段代码可以帮到你。如果运行不正常,请通知我。

package com.mediaplayer;

import java.io.IOException;
import java.util.ArrayList;

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

public class MediaplayerActivity extends Activity implements OnCheckedChangeListener {

MediaPlayer song1;
int whatsong = 0;

private ArrayList<Integer> songIds;

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



    RadioGroup rgMusic = (RadioGroup) findViewById(R.id.radioGroup1);


    songIds=new ArrayList<Integer>();

    songIds.add(R.raw.fluet);
    songIds.add(R.raw.mogra_na_phool);
    songIds.add(R.raw.airtel);

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


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

    rgMusic.setOnCheckedChangeListener(this);

    bPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {           



            if(song1!=null)
            {
               song1.release();
            }


            switch (whatsong) {

            case 1:
                try {
                    song1 = MediaPlayer.create(Nothing.this, songIds.get(0));
                    song1.prepare();
                } catch (IllegalStateException e) {                 
                    e.printStackTrace();
                } catch (IOException e) {                   
                    e.printStackTrace();
                }
                song1.start();
                break;  

            case 2:
                try {
                    song1 = MediaPlayer.create(Nothing.this, songIds.get(1));
                    song1.prepare();
                } catch (IllegalStateException e) {                 
                    e.printStackTrace();
                } catch (IOException e) {                   
                    e.printStackTrace();
                }
                song1.start();
                break;

            case 3:
                try {
                    song1 = MediaPlayer.create(Nothing.this, songIds.get(2));
                    song1.prepare();
                } catch (IllegalStateException e) {                     
                    e.printStackTrace();
                } catch (IOException e) {               
                    e.printStackTrace();
                }
                song1.start();
                break;
            }
        }
    });

    bStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {       

           if(song1!=null){
               song1.release();
           }
        }
    });                                      
}

@Override
public void onCheckedChanged(RadioGroup group, int rbId) {      

    switch (rbId) {
    case R.id.rbMusic1:
        whatsong = 1;           
        break;
    case R.id.rbMusic2:
        whatsong = 2;           
        break;
    case R.id.rbMusic3:
        whatsong = 3;           
        break;

    }

}

}

答案 1 :(得分:0)

这个问题因为你刚刚检测到歌曲是否播放而提高了?但是你也检测到了真假?

喜欢::

if(song1.isPlaying()==true)
 {
    song1.pause(); //or
    song1.stop():
 }
 else{
       song1.start();
     }

更新

暂停我使用的媒体播放器...

Mediaplayer.pause();
length=Mediaplayer.getCurrentPosition();

并且从最近停止的位置恢复玩家是......

Mediaplayer.seekTo(length);
Mediaplayer.start();

答案 2 :(得分:0)

暂停和恢复您使用此媒体播放器。我认为它对你有帮助。

if(song1.isPlaying()==true)
{
   Mediaplayer.pause();
   length=Mediaplayer.getCurrentPosition();
}

else
{
  Mediaplayer.seekTo(length);
  Mediaplayer.start();
}

答案 3 :(得分:0)

         mp.stop();
            try {
                mp.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mp.seekTo(0);