概述
上篇中我们学习了指定Sdcard中音乐播放,但是游戏中的音效如何实现呢?
游戏中是肯定和程序打包发布的。这样的音乐就没有放在Sdcard中,一般会放在应用目录下的“res/raw”下。那如何实现音乐播放呢?
其实很简单呐,我只不过把setDataSource()方法改成create()方法来指定资源索引的MediaPlayer对象。
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);
}
}
源代码下载:点击打开链接
最后
以上就是天真皮卡丘为你收集整理的音乐播放(游戏中的音效)的全部内容,希望文章能够帮你解决音乐播放(游戏中的音效)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复