音乐播放(游戏中的音效)

音乐播放(游戏中的音效)_第1张图片

上篇中我们学习了指定Sdcard中音乐播放,但是游戏中的音效如何实现呢?

游戏中是肯定和程序打包发布的。这样的音乐就没有放在Sdcard中,一般会放在应用目录下的“res/raw”下。那如何实现音乐播放呢?

其实很简单呐,我只不过把setDataSource()方法改成create()方法来指定资源索引的MediaPlayer对象。

音乐播放(游戏中的音效)_第2张图片

 

 

package xiaosi.mp3;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class Mp32Activity extends Activity
{
	/** Called when the activity is first created. */
	private TextView text = null;
	private ImageButton Start = null;
	private ImageButton Pause = null;
	private ImageButton Stop = null;
	private MediaPlayer mediaPlayer = null;
	private boolean isPlaying = false;
	private boolean isPaused = false;
	private boolean isReleased = false;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 构建MediaPlayer对象
		mediaPlayer = new MediaPlayer();
		
		text = (TextView)findViewById(R.id.text);
		
		Start = (ImageButton) findViewById(R.id.Start);
		Start.setOnClickListener(new StartListener());
		
		Pause = (ImageButton) findViewById(R.id.Pause);
		Pause.setOnClickListener(new PauseListener());
		
		Stop = (ImageButton) findViewById(R.id.Stop);
		Stop.setOnClickListener(new StopListener());
	}
	// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	// 开始播放
	private class StartListener implements OnClickListener
	{
		public void onClick(View v)
		{
			if(!isPlaying){
				
				mediaPlayer = MediaPlayer.create(Mp32Activity.this, R.raw.f4);
				mediaPlayer.setLooping(true);
				mediaPlayer.start();
				text.setText("音乐正在播放中......");
				isPlaying = true;
				isReleased = false;
				isPaused = false;
				mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
					public void onCompletion(MediaPlayer mp)
					{
						mediaPlayer.release();
					}
				});
			}
		}
	}
	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	// 暂停
	private class PauseListener implements OnClickListener
	{
		public void onClick(View v)
		{
			if(mediaPlayer != null){
				if(!isReleased){
					if(!isPaused){
						mediaPlayer.pause();
						isPaused = true;
						isPlaying = true;
						text.setText("播放暂停中......");
					}
					else
					{
						mediaPlayer.start();
						isPaused = false;
						isPlaying = true;
						text.setText("播放重新播放......");
					}
				}
			}
		}
	}

	// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	// 停止
	private class StopListener implements OnClickListener
	{
		public void onClick(View v)
		{
			if(isPlaying){
				if(!isReleased){
					mediaPlayer.stop();
					mediaPlayer.release();
					isReleased = true;
				}
				isPlaying = false;
				text.setText("播放停止......");
			}
		}
	}
	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			if(!isReleased){
				mediaPlayer.stop();
				mediaPlayer.release();
				isReleased = true;
			}
			this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}


 

 

源代码下载:点击打开链接

 

 

 

 

你可能感兴趣的:(游戏,null,Class,音乐,import)