音,视频播放资源

资源:
Android MediaPlayer的生命周期
http://syab11.iteye.com/blog/761660
Android提高第二十一篇之MediaPlayer播放网络视频
http://blog.csdn.net/hellogv/article/details/6429455



一定要先弄明白状态图:
http://book.51cto.com/art/200912/173820.htm
我说怎么总是感觉isPlaying()不太准确......晕

引用
3. 播放、暂停和停止

调用start()方法,MediaPlayer将进入到started状态。isPlaying()方法可以用来判断MediaPlayer是否处在started状态。当MediaPlayer从网络上播放多媒体文件时,可以通过onBufferingUpdateListener.onBufferingUpdate(MediaPlayer mp,int percent)来监听缓冲的进度,其中percent是0~100的整数,代表已经缓冲好的多媒体数据的百分比。

调用pause()方法,MediaPlayer将进入到paused状态。需要注意的是,从started到paused、从paused到started状态的转换是异步过程,也就是说,可能经过一段时间才能更新MediaPlayer的状态。在调用isPlaying()来查询播放器的状态时需要考虑这一点。

调用stop()方法,MediaPlayer将进入到stopped状态。一旦MediaPlayer进入stopped状态,必须再次调用prepare()或者prepareAsyn()才能使其进入到prepared状态,以便复用此MediaPlayer对象,再次播放多媒体文件。



http://blog.csdn.net/kesenhoo/article/details/6547726
MediaPlayer使用示例:
/**
 * 音频播放器
 * @author kay
 */
public class PlayActivity extends Activity 
{
	private static final String TAG = "PlayActivity";
    private EditText filenameText;
    private MediaPlayer mediaPlayer;
    private String filename;
    private int position;
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        filenameText = (EditText)this.findViewById(R.id.filename);
       
        //对mediaPlayer进行实例化
        mediaPlayer = new MediaPlayer();
        ButtonClickListener listener = new ButtonClickListener();
        //获取4个按钮
        Button playButton = (Button)this.findViewById(R.id.play);
        Button pauseButton = (Button)this.findViewById(R.id.pause);
        Button resetButton = (Button)this.findViewById(R.id.reset);
        Button stopButton = (Button) this.findViewById(R.id.stop);
        //设置4个按钮的监听
        playButton.setOnClickListener(listener);
        pauseButton.setOnClickListener(listener);
        resetButton.setOnClickListener(listener);
        stopButton.setOnClickListener(listener);
        Log.i(TAG, "onCreate()");
    }
    
    
    @Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) 
    {
		this.filename = savedInstanceState.getString("filename");
		this.position = savedInstanceState.getInt("position");
		super.onRestoreInstanceState(savedInstanceState);
		Log.i(TAG, "onRestoreInstanceState()");
	}
	@Override
	protected void onSaveInstanceState(Bundle outState) 
	{
		outState.putString("filename", filename);
		outState.putInt("position", position);
		super.onSaveInstanceState(outState);
		Log.i(TAG, "onSaveInstanceState()");
	}
	
	private void play() throws IOException 
	{
		//获取文件路径
		File audioFile = new File(Environment.getExternalStorageDirectory(),filename);
		mediaPlayer.reset();
		mediaPlayer.setDataSource(audioFile.getAbsolutePath());
		mediaPlayer.prepare();
		mediaPlayer.start();
	}
	
	//如果突然电话到来,停止播放音乐
	@Override
	protected void onPause() 
	{
		if(mediaPlayer.isPlaying())
		{
			//保存当前播放点
			position = mediaPlayer.getCurrentPosition();
			mediaPlayer.stop();
		}
		super.onPause();
	}
	
	//
	@Override
	protected void onResume() 
	{
		//如果电话结束,继续播放音乐
		if(position>0 && filename!=null)
		{
			try 
			{
				play();
				mediaPlayer.seekTo(position);
				position = 0;
			}
			catch (IOException e)
			{
				Log.e(TAG, e.toString());
			}
		}
		super.onResume();
	}
	
	//对mediaPlayer进行摧毁
	@Override
	protected void onDestroy() 
	{
    	mediaPlayer.release();     
		super.onDestroy();
		Log.i(TAG, "onDestroy()");
	}
	private final class ButtonClickListener implements View.OnClickListener
	{
		@Override
		public void onClick(View v) 
		{
			//先得到文本框中的内容
			filename = filenameText.getText().toString();
			//得到button
			Button button = (Button) v; 
			try 
			{
				//通过传过来的Buttonid可以判断Button的类型
				switch (v.getId()) 
				{
					case R.id.play:
						play();
						break;
	
					case R.id.pause:
						if(mediaPlayer.isPlaying())
						{
							mediaPlayer.pause();
							button.setText(R.string.continue1);//让这个按钮上的文字显示为继续
						}
						else
						{
							mediaPlayer.start();//继续播放
							button.setText(R.string.pause);
						}
						break;
						
					case R.id.reset:
						if(mediaPlayer.isPlaying())
						{
							mediaPlayer.seekTo(0);//让它从0开始播放
						}
						else
						{
							play();//如果它没有播放,就让它开始播放
						}
						break;
						
					case R.id.stop:
						//如果它正在播放的话,就让他停止
						if(mediaPlayer.isPlaying()) 
							mediaPlayer.stop();
						break;
				}
			} 
			catch (Exception e)
			{	
				Log.e(TAG, e.toString());
			}
		}		
    }	
}


其他资源:
1. http://blog.csdn.net/Android_Tutor/article/details/5743183
2. http://blog.csdn.net/Android_Tutor/article/details/5058217

你可能感兴趣的:(视频)