2011.08.12——— android MediaPlayer

2011.08.12——— android MediaPlayer

写一个简单的播放mp3的程序,一直报错

DEBUG/MediaPlayer(3662): java.io.IOException: Prepare failed.: status=0x1


很是郁闷

后来感觉是
mMediaPlayer.prepare();

必须单独try catch起来 当我爸try catch去掉时 一样报上面的错误

不是很明白

package com.lp;


import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private Button play;
	private Button stop;
	private boolean 	isReleased		= false;
	private boolean 	isPlaying		= false;
	private MediaPlayer mMediaPlayer = new MediaPlayer();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        play = (Button)findViewById(R.id.main_play);
        stop = (Button)findViewById(R.id.main_stop);
        play.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try
				{
					if ( !isPlaying )
					{
						/* 装载资源中的音乐 */
						mMediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.aa);
						isPlaying = true;
						isReleased = false;
						/* 设置是否循环 */
						mMediaPlayer.setLooping(true);
						try
						{
							mMediaPlayer.prepare();
						}
						catch (IllegalStateException e)
						{
							e.printStackTrace();
						}
						catch (IOException e)
						{
							e.printStackTrace();
						}
						
						mMediaPlayer.start();
						
					}
				}
				catch (IllegalStateException e)
				{
					e.printStackTrace();
				}
			}
        });
        
        stop.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(isPlaying&&!isReleased){
					isReleased = true;
					mMediaPlayer.stop();
					mMediaPlayer.release();
					isPlaying = false;
				}
			}
		});
    }
				
}

你可能感兴趣的:(android)