2 xiaoyan 12 xiaoyan_12 于 2013.01.22 18:05 提问

在android系统中如何播放、暂停、停止歌曲?

我在程序中设置了播放、暂停、停止三个按钮。
当我点击播放按钮,这个按钮就消失了,显示的是暂停按钮。反之亦然。
当我点击播放按钮时,程序不出错。再点击暂停按钮时,就会出错。
贴出代码:

package com.mpIlango;

 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 MpIlangoActivity extends Activity implements OnCheckedChangeListener {
/** Called when the activity is first created. */


MediaPlayer song1,song2,song3;
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.rgMusic);

    songIds = new ArrayList<Integer>();

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


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

    bPause.setVisibility(View.GONE);

    rgMusic.setOnCheckedChangeListener(this);

    bPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {           

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

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

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

              switch (whatsong) {

                case 1:

                    try {
                        song1 = MediaPlayer.create(MpIlangoActivity.this, songIds.get(0));
                        song1.prepare();
                    } catch (IllegalStateException e) {                 
                        e.printStackTrace();
                    } catch (IOException e) {                   
                        e.printStackTrace();
                    }
                    song1.start();
                    bPlay.setVisibility(View.GONE);
                    bPause.setVisibility(View.VISIBLE);
                    break;  

                case 2:
                    try {
                        song2 = MediaPlayer.create(MpIlangoActivity.this, songIds.get(1));
                        song2.prepare();
                    } catch (IllegalStateException e) {                 
                        e.printStackTrace();
                    } catch (IOException e) {                   
                        e.printStackTrace();
                    }
                    song2.start();
                    bPlay.setVisibility(View.GONE);
                    bPause.setVisibility(View.VISIBLE);
                    break;

                case 3:
                    try {
                        song3 = MediaPlayer.create(MpIlangoActivity.this, songIds.get(2));
                        song3.prepare();
                    } catch (IllegalStateException e) {                     
                        e.printStackTrace();
                    } catch (IOException e) {               
                        e.printStackTrace();
                    }
                    song3.start();
                    bPlay.setVisibility(View.GONE);
                    bPause.setVisibility(View.VISIBLE);
                    break;
                }
            }               
    });

    bPause.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

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

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

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

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

    bStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

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

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

              if(song3!=null){
                   song3.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;

    }       
}
}

3个回答

nana4ever
nana4ever   2013.01.23 10:23
已采纳

我估计你在下面这些代码处获得异常 NullPointerException

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

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

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

使用转化功能可能是这段代码出错了吧

switch (whatsong) {

    case 1: 
        if(song1.isPlaying()){
            song1.pause();          
        }

或者在别的地方初始化你的歌曲,不能让它们为空
我也推荐使用MediaPlayer.

MediaPlayer song;

bPlay code:

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

switch (whatsong) {

    case 1:

        try {
            song = MediaPlayer.create(MpIlangoActivity.this, songIds.get(0));
            song.prepare();
        } catch (IllegalStateException e) {                 
            e.printStackTrace();
        } catch (IOException e) {                   
            e.printStackTrace();
        }

}

song.start();
bPlay.setVisibility(View.GONE);
bPause.setVisibility(View.VISIBLE);

bPause code:

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

if(song != null && song.isPlaying()){
    song.pause();          
}
zlQQhs
zlQQhs   2013.02.26 00:25

http://blog.csdn.net/zlqqhs/article/details/8600819

我自己写的播放器,可以参考一下

w294785608
w294785608   2013.08.12 21:56

我今天也正在整一张图片上一个语音播放暂停,但是暂停加上去就死。。。不知道如何加,希望楼主给予指点,谢谢

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!