2011-09-19 171 views
1

我正在为Android制作应用程序,并且我制作了三个按钮来播放,暂停和停止。如何在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; 

    }  

} 

    } 
+0

请问您可以从logcat中添加错误详细信息以及... 另外,请始终尝试在您认为可能出错的部分代码中添加Log.D。 – Urban

+0

没有错误信息,我们无法做任何事 – DallaRosa

回答

1

我猜你会在这里得到一个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代码:

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代码:

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

if(song != null && song.isPlaying()){ 
    song.pause();   
} 

所有这些代码是未经测试!

+0

感谢chrizzz,因为暂停按钮您的编码完全正常,但暂停按钮作为停止工作,我通过测试检查了它,第二件事是为播放按钮创建媒体播放器songIds.get(0),它会一直返回第一首歌曲。那么暂停这首歌的解决方案是什么? – Jayesh

+0

由于您在按“播放”时释放歌曲,然后创建新的MediaPlayer,所以暂停的行为与“停止”相似。尝试初始化其他地方的歌曲,并在按下停止按钮时使用song.stop()。 songIds.get(0)实际上是获取第一个请求的请求。使用songIds.get(whichsong-1) – cwin

+0

好的,谢谢哥们。 – Jayesh

0

这里是一个SoundManager类/ audiomanager类,希望它可以帮助或指向你在正确的方向;)

import java.util.HashMap; 
import java.util.Random; 

import android.content.Context; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.media.SoundPool; 

public class SoundManager { 

public boolean   audio; 
public boolean   audioSoundFX; 
public boolean   audioMusic; 

private SoundPool  mSoundPool; 
private HashMap<Integer, Integer>  mSoundPoolMap; 
private AudioManager mAudioManager; 
private Context   mContext; 
private Random   random = new Random(); 

private MediaPlayer  menuMusic; 
private MediaPlayer  gameMusic; 
private int    menuMusicCurrentPos = 0; 
private int    gameMusicCurrentPos = 0; 

public static final SoundManager INSTANCE = new SoundManager(); 

private SoundManager() { 
} 

public void setAudio(boolean audioOn){ 
    if(audioOn){ 
     audio = true; 
    } 
    if(!audioOn) { 
     audio = false; 
    } 
} 

public void setSoundFX(boolean soundFX){ 
    if(soundFX){ 
     audioSoundFX = true; 
    } 
    if(!soundFX) { 
     audioSoundFX = false; 
    } 
} 

public void setMusic(boolean music){ 
    if(music){ 
     audioMusic = true; 
    } 
    if(!music) { 
     audioMusic = false; 
    } 
} 


public void initSounds(Context theContext) { 

    if (mSoundPool == null){ 
     mContext = theContext; 
     mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); 
     mSoundPoolMap = new HashMap<Integer, Integer>(); 
     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE); 

     mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.ufo_laser, 1)); 
     mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.enemy_hunter_one_laser, 1)); 
     mSoundPoolMap.put(3, mSoundPool.load(mContext, R.raw.enemyhuntertwomissile, 1)); 
     mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.enemy_hunter_three_laser, 1)); 
     mSoundPoolMap.put(5, mSoundPool.load(mContext, R.raw.enemy_drone, 1)); 
     mSoundPoolMap.put(6, mSoundPool.load(mContext, R.raw.kamikaze, 1)); 

     mSoundPoolMap.put(14, mSoundPool.load(mContext, R.raw.exploastroidshard, 1)); 

     mSoundPoolMap.put(11, mSoundPool.load(mContext, R.raw.health, 1)); 
     mSoundPoolMap.put(12, mSoundPool.load(mContext, R.raw.energy, 1)); 
     mSoundPoolMap.put(13, mSoundPool.load(mContext, R.raw.shield, 1)); 

     mSoundPoolMap.put(15, mSoundPool.load(mContext, R.raw.gameover, 1)); 
     mSoundPoolMap.put(16, mSoundPool.load(mContext, R.raw.gameoverexplo, 1)); 

     mSoundPoolMap.put(17, mSoundPool.load(mContext, R.raw.menu_beep, 1)); 
     mSoundPoolMap.put(20, mSoundPool.load(mContext, R.raw.menu_beep, 1)); 

     menuMusic = MediaPlayer.create(mContext, R.raw.musicmenu); 
    } 
} 

public void playSound(int index, boolean pitching, int loop){ 
    if(audioSoundFX == true && audio == true){ 

     float randomPitch; 

     if (pitching){ 
      randomPitch = (float)(random.nextInt(3) + 9)/10; 
     }else{ 
      randomPitch = 1; 
     } 

     float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     streamVolume = streamVolume/mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
     mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume, 1, loop, randomPitch); 
    } 
} 


public void playMenuMusic(){ 
    if(audioMusic == true && audio == true){ 
     if (menuMusic == null){ 
      if(MediaPlayer.create(mContext, R.raw.musicmenu) != null) { 
       menuMusic = MediaPlayer.create(mContext, R.raw.musicmenu); 
       if(menuMusicCurrentPos != 0){ 
        menuMusic.seekTo(menuMusicCurrentPos);     
       } 
       menuMusic.start(); 
       menuMusic.setVolume(1f , 1f); 
       menuMusic.setLooping(true); 
      } 
     } 
    } 
} 


public void releaseMenuMusic(){ 
    if(menuMusic != null){ 
     this.menuMusicCurrentPos = menuMusic.getCurrentPosition(); 
     menuMusic.release(); 
     menuMusic = null; 
    } 
} 


public void playGameMusic(){ 
    if(audioMusic == true && audio == true){ 
     if (gameMusic == null){ 
      gameMusic = MediaPlayer.create(mContext, R.raw.music_game); 
      if(menuMusicCurrentPos != 0){ 
       gameMusic.seekTo(gameMusicCurrentPos);     
      } 
      gameMusic.start(); 
      gameMusic.setVolume(1f , 1f); 
      gameMusic.setLooping(true); 
     } 
    } 
} 


public void releaseGameMusic(){ 
    if(gameMusic != null){ 
     this.gameMusicCurrentPos = gameMusic.getCurrentPosition(); 
     gameMusic.release(); 
     gameMusic = null; 
    } 
} 

}

2

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

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

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

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

sir MediaPlayer.pause()显示错误 –

相关问题