160行flappy bird

http://blog.csdn.net/hellogv/article/details/6406732

在此文章的代码下做部分改动,已适应自己的需要,主要就是改为播放暂停同一个按钮,便于切换。

更主要的是复习定时器,另外在线音频播放不成功以后再研究吧。


播放器控制类

package com.example.mediaplayer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.SeekBar;

public class Player implements OnBufferingUpdateListener, OnCompletionListener,
        MediaPlayer.OnPreparedListener ,OnErrorListener{
    public MediaPlayer mediaPlayer;
    private SeekBar skbProgress;
    private Timer mTimer = new Timer();

    public Player() {


        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnBufferingUpdateListener(this);
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.setOnCompletionListener(this);
        } catch (Exception e) {
            Log.e("mediaPlayer", "error", e);
        }

        mTimer.schedule(mTimerTask, 0, 1000);
    }

    public void setProgressBar(SeekBar skbProgress)
    {
      this.skbProgress = skbProgress;
    }
    
    /**
     * 通过定时器和Handler来更新进度条
     */
    TimerTask mTimerTask = new TimerTask() {
        @Override
        public void run() {
            if (mediaPlayer == null)
                return;
            if (mediaPlayer.isPlaying() && skbProgress.isPressed() == false) {
                handleProgress.sendEmptyMessage(0);
            }
            
        }
    };

    //处理消息
    Handler handleProgress = new Handler() {
        public void handleMessage(Message msg) {

            int position = mediaPlayer.getCurrentPosition();
            int duration = mediaPlayer.getDuration();

            if (duration > 0) {
                int pos = skbProgress.getMax() * position / duration;
                skbProgress.setProgress(pos);
            }
        };
    };


    
    //初始播放
    public void playUrl(String videoUrl) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(videoUrl);
            mediaPlayer.prepare();// prepare之后自动播放
            
//            File file = new File(videoUrl); 
//            FileInputStream fis = new FileInputStream(file); 
//            mediaPlayer.setDataSource(fis.getFD()); 
//            mediaPlayer.prepare(); 
            
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //暂停
    public void pause() {
        mediaPlayer.pause();
    }
    //播放
    public void play() {
        mediaPlayer.start();
    }
    //停止
    public void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            
            mediaPlayer = null;
        }
    }
    //判断是否正在播放
    public boolean isplaying()
    {
    	boolean playing = mediaPlayer.isPlaying();
    	return playing;
    }
    
    

    /**
     * 通过onPrepared播放
     */
    public void onPrepared(MediaPlayer arg0) {
        arg0.start();
        Log.e("mediaPlayer", "onPrepared");
    }

    public void onCompletion(MediaPlayer arg0) {
        Log.e("mediaPlayer", "onCompletion");
    }

    public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {
        skbProgress.setSecondaryProgress(bufferingProgress);
        int currentProgress = skbProgress.getMax()
                * mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration();
        Log.e(currentProgress + "% play", bufferingProgress + "% buffer");
    }

    public boolean onError(MediaPlayer mp, int what, int extra) {
        return false;
    }

}

界面类

package com.example.mediaplayer;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;

public class TestMedia extends Activity {
	private Button btnPause, btnPlayUrl, btnStop;
	private SeekBar skbProgress;
	private Player player;
	private int first;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.setTitle("在线音乐播放---hellogv编写");

		btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl);
		btnPlayUrl.setOnClickListener(new ClickEvent());

		btnPause = (Button) this.findViewById(R.id.btnPause);
		btnPause.setOnClickListener(new ClickEvent());

		btnStop = (Button) this.findViewById(R.id.btnStop);
		btnStop.setOnClickListener(new ClickEvent());

		skbProgress = (SeekBar) this.findViewById(R.id.skbProgress);
		skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent());
		player = new Player();

	}

	class ClickEvent implements OnClickListener {

		public void onClick(View arg0) {

			player.setProgressBar(skbProgress);

			if (arg0 == btnPause) {// 播放暂停按钮

				if (player.isplaying())// 如果正在播放则暂停
				{
					player.pause();
					btnPause.setText("播放");
				} else// 如果暂停则播放
				{
					btnPause.setText("暂停");
					if (first == 0)// 初始播放
					{
						String luyin_Path = Environment
								.getExternalStorageDirectory()
								.getAbsolutePath()
								+ "/" + "she.mp3";

						player.playUrl(luyin_Path);
					} else// 继续播放
					{
						player.play();
					}
				}

				first++;

			} else if (arg0 == btnPlayUrl) {
				// // 在百度MP3里随便搜索到的,大家可以试试别的链接
				// String url = "http://219.138.125.22/myweb/mp3/CMP3/JH19.MP3";
				// player.playUrl(url);

				String music_path = "http://stream19.qqmusic.qq.com/30974808.mp3";

				player.playUrl(music_path);

			} else if (arg0 == btnStop) {
				player.stop();
			}
		}
	}

	class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
		int progress;

		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// 原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()
			this.progress = progress * player.mediaPlayer.getDuration()
					/ seekBar.getMax();
		}

		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		public void onStopTrackingTouch(SeekBar seekBar) {
			// seekTo()的参数是相对与影片时间的数字,而不是与seekBar.getMax()相对的数字
			player.mediaPlayer.seekTo(progress);
		}
	}

}

代码下载

http://download.csdn.net/detail/superjunjin/7070067

160行flappy bird,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。