Android播放音乐
jopen
11年前
mainActivity如下:
package cn.com; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.TextView; import android.app.Activity; /** * 重要错误总结: * Eclipse报错prepareAsync called in state 8 * 原因: * 在initView()方法中: * mMediaPlayer=MediaPlayer.create(MainActivity.this, R.raw.big); * 为此mMediaPlayer设置了要播放的资源 * 但是在点击stop按钮以后,执行了mMediaPlayer.release(); * 这样的话就释放了资源.所以报错 * 解决办法: * 在每次点击play的时候,都执行MediaPlayer.create() * 注意: * 可以在每次点击暂停的时候保存现在播放的位置即: * position=mMediaPlayer.getCurrentPosition(); * 然后在每次播放的时候 * mMediaPlayer.seekTo(position); * 这样就会从暂停处继续播放 * * */ public class MainActivity extends Activity { private TextView mTextView; private MediaPlayer mMediaPlayer; private ImageView mPlayerImageView; private ImageView mPauseImageView; private ImageView mStopImageView; private boolean isPause=false; private boolean isStop=false; private boolean isStart=false; private int currentPosition=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); } private void initView(){ mTextView=(TextView) findViewById(R.id.textView); mPlayerImageView=(ImageView) findViewById(R.id.playerImageButton); mPlayerImageView.setOnClickListener(new ButtonOnClickListener()); mPauseImageView=(ImageView) findViewById(R.id.pauseImageButton); mPauseImageView.setOnClickListener(new ButtonOnClickListener()); mStopImageView=(ImageView) findViewById(R.id.stopImageButton); mStopImageView.setOnClickListener(new ButtonOnClickListener()); mMediaPlayer=new MediaPlayer(); // mMediaPlayer=MediaPlayer.create(MainActivity.this, R.raw.big); mMediaPlayer.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer mp) { mMediaPlayer.release(); mTextView.setText("音乐播放完毕"); } }); mMediaPlayer.setOnErrorListener(new OnErrorListener() { public boolean onError(MediaPlayer mp, int what, int extra) { mMediaPlayer.release(); mTextView.setText("音乐播放错误"); return false; } }); } private class ButtonOnClickListener implements OnClickListener { public void onClick(View v) { switch (v.getId()) { case R.id.playerImageButton: if (!isStart) { try { mMediaPlayer=MediaPlayer.create(MainActivity.this, R.raw.big); //mMediaPlayer.prepare(); mMediaPlayer.seekTo(currentPosition); mMediaPlayer.start(); mMediaPlayer.setLooping(true); mTextView.setText("开始音乐播放"); isStart = true; isPause = false; isStop = false; } catch (Exception e) { } } break; case R.id.pauseImageButton: if (mMediaPlayer != null) { if (!isStop) { if (!isPause) { mMediaPlayer.pause(); mTextView.setText("暂停音乐播放"); currentPosition=mMediaPlayer.getCurrentPosition(); isPause = true; isStop = false; isStart = false; } else { mMediaPlayer.start(); mTextView.setText("继续播放音乐"); isStart = true; isPause = false; isStop = false; } } } break; case R.id.stopImageButton: if (mMediaPlayer != null) { if (isStart) { if (!isStop) { mMediaPlayer.stop(); mMediaPlayer.release(); mTextView.setText("停止音乐播放"); currentPosition=0; isStop = true; isPause = false; isStart = false; } } } break; default: break; } } } }main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="测试MediaPlayer" android:textSize="30sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="70dip" android:orientation="horizontal" android:layout_below="@id/textView" > <ImageView android:id="@+id/playerImageButton" android:layout_width="70dip" android:layout_height="70dip" android:src="@drawable/player" /> <ImageView android:id="@+id/pauseImageButton" android:layout_width="70dip" android:layout_height="70dip" android:src="@drawable/pause" android:layout_marginLeft="20dip" /> <ImageView android:id="@+id/stopImageButton" android:layout_width="70dip" android:layout_height="70dip" android:src="@drawable/stop" android:layout_marginLeft="20dip" /> </LinearLayout> </RelativeLayout>