1.Audio处理类:

public class AudioHandler implements OnCompletionListener, OnPreparedListener, OnErrorListener {
private MediaRecorder recorder;
private boolean isRecording = false;
MediaPlayer mPlayer;
private boolean isPlaying = false;
private String recording;
private String saveFile;
private Context mCtx;


public AudioHandler(String file, Context ctx) {
this.recording = file;
this.mCtx = ctx;
}


protected void startRecording(String file) throws IllegalStateException, IOException {
if (!isRecording) {
saveFile = file;
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(this.recording);
recorder.prepare();
isRecording = true;
recorder.start();
}
}


private void moveFile(String file) {
/* this is a hack to save the file as the specified name */
File f = new File(this.recording);
f.renameTo(new File("/sdcard" + file));
}


protected void stopRecording() {
try {
if ((recorder != null) && (isRecording)) {
isRecording = false;
recorder.stop();
recorder.release();
}
moveFile(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}


protected void startPlaying(String file) {
if (isPlaying == false) {
try {
mPlayer = new MediaPlayer();
isPlaying = true;
Log.d("Audio startPlaying", "audio: " + file);
if (isStreaming(file)) {
Log.d("AudioStartPlaying", "Streaming");
// Streaming prepare async
mPlayer.setDataSource(file);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.prepareAsync();
} else {
Log.d("AudioStartPlaying", "File");
// Not streaming prepare synchronous, abstract base
// directory
mPlayer.setDataSource("/sdcard/" + file);
mPlayer.prepare();
}
mPlayer.setOnPreparedListener(this);
} catch (Exception e) {
e.printStackTrace();
}
}
}


protected void stopPlaying() {
if (isPlaying) {
mPlayer.stop();
mPlayer.release();
isPlaying = false;
}
}


public void onCompletion(MediaPlayer mPlayer) {
mPlayer.stop();
mPlayer.release();
isPlaying = false;
}


protected long getCurrentPosition() {
if (isPlaying) {
return (mPlayer.getCurrentPosition());
} else {
return (-1);
}
}


private boolean isStreaming(String file) {
if (file.contains("http://")) {
return true;
} else {
return false;
}
}


protected long getDuration(String file) {
long duration = -2;
if (!isPlaying & !isStreaming(file)) {
try {
mPlayer = new MediaPlayer();
mPlayer.setDataSource("/sdcard/" + file);
mPlayer.prepare();
duration = mPlayer.getDuration();
mPlayer.release();
} catch (Exception e) {
e.printStackTrace();
return (-3);
}
} else if (isPlaying & !isStreaming(file)) {
duration = mPlayer.getDuration();
} else if (isPlaying & isStreaming(file)) {
try {
duration = mPlayer.getDuration();
} catch (Exception e) {
e.printStackTrace();
return (-4);
}
} else {
return -1;
}
return duration;
}


public void onPrepared(MediaPlayer mPlayer) {
if (isPlaying) {
mPlayer.setOnCompletionListener(this);
mPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mPlayer, int percent) {
/* TODO: call back, e.g. update outer progress bar */
Log.d("AudioOnBufferingUpdate", "percent: " + percent);
}
});
mPlayer.start();
}
}


public boolean onError(MediaPlayer mPlayer, int arg1, int arg2) {
Log.e("AUDIO onError", "error " + arg1 + " " + arg2);
return false;
}


protected void setAudioOutputDevice(int output) {
// Changes the default audio output device to speaker or earpiece
AudioManager audiMgr = (AudioManager) mCtx.getSystemService(Context.AUDIO_SERVICE);
if (output == (2))
audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);
else if (output == (1)) {
audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
} else
Log.e("AudioHandler setAudioOutputDevice", " unknown output device");
}


protected int getAudioOutputDevice() {
AudioManager audiMgr = (AudioManager) mCtx.getSystemService(Context.AUDIO_SERVICE);
if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_EARPIECE)
return 1;
else if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_SPEAKER)
return 2;
else
return -1;
}
}


2.判断网络是否可用

  1. // 判断网络是否正常
  2. public static boolean isNetworkAvailable(Context context) {
  3. ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  4. if (connectivity == null) {
  5. return false;
  6. } else {
  7. NetworkInfo info = connectivity.getActiveNetworkInfo();
  8. if (info == null) {
  9. return false;
  10. } else {
  11. if (info.isAvailable()) {
  12. return true;
  13. }
  14. }
  15. }
  16. return false;
  17. }

3.日期格式化类

/**
* Utility class for formatting and parsing the various date formats we expect
* to encounter.
*/
public class DateUtils
{
private static final String TAG = "DateUtils";

private static final SimpleDateFormat[] dateFormats;
private static final int dateFormat_default = 2;


private DateUtils() { }


static
{
final String[] possibleDateFormats =
{
"EEE, dd MMM yyyy HH:mm:ss z", // RFC_822
"EEE, dd MMM yyyy HH:mm zzzz",
"yyyy-MM-dd'T'HH:mm:ssZ",
"yyyy-MM-dd'T'HH:mm:ss.SSSzzzz", // Blogger Atom feed has millisecs also
"yyyy-MM-dd'T'HH:mm:sszzzz",
"yyyy-MM-dd'T'HH:mm:ss z",
"yyyy-MM-dd'T'HH:mm:ssz", // ISO_8601
"yyyy-MM-dd'T'HH:mm:ss",
"yyyy-MM-dd'T'HHmmss.SSSz",
"yyyy-MM-dd"
};


dateFormats = new SimpleDateFormat[possibleDateFormats.length];
TimeZone gmtTZ = TimeZone.getTimeZone("GMT");


for (int i = 0; i < possibleDateFormats.length; i++)
{
/* TODO: Support other locales? */
dateFormats[i] = new SimpleDateFormat(possibleDateFormats[i],
Locale.ENGLISH);


dateFormats[i].setTimeZone(gmtTZ);
}
}


/**
* Parse a date string. The format of RSS/Atom dates come in many
* different forms, so this method is extremely flexible and attempts to
* understand many different formats.
*
* Copied verbatim from Informa 0.7.0-alpha2, ParserUtils.java.
*
* @param strdate
* Date string to attempt to parse.
*
* @return
* If successful, returns a {@link Date} object representing the parsed
* date; otherwise, null.
*/
public static Date parseDate(String strdate)
{
Date result = null;
strdate = strdate.trim();
if (strdate.length() > 10) {


// TODO deal with +4:00 (no zero before hour)
if ((strdate.substring(strdate.length() - 5).indexOf("+") == 0 || strdate
.substring(strdate.length() - 5).indexOf("-") == 0)
&& strdate.substring(strdate.length() - 5).indexOf(":") == 2) {


String sign = strdate.substring(strdate.length() - 5,
strdate.length() - 4);


strdate = strdate.substring(0, strdate.length() - 5) + sign + "0"
+ strdate.substring(strdate.length() - 4);
// logger.debug("CASE1 : new date " + strdate + " ? "
// + strdate.substring(0, strdate.length() - 5));


}


String dateEnd = strdate.substring(strdate.length() - 6);


// try to deal with -05:00 or +02:00 at end of date
// replace with -0500 or +0200
if ((dateEnd.indexOf("-") == 0 || dateEnd.indexOf("+") == 0)
&& dateEnd.indexOf(":") == 3) {
// TODO deal with GMT-00:03
if ("GMT".equals(strdate.substring(strdate.length() - 9, strdate
.length() - 6))) {
Log.d(TAG, "General time zone with offset, no change");
} else {
// continue treatment
String oldDate = strdate;
String newEnd = dateEnd.substring(0, 3) + dateEnd.substring(4);
strdate = oldDate.substring(0, oldDate.length() - 6) + newEnd;
// logger.debug("!!modifying string ->"+strdate);
}
}
}
int i = 0;
while (i < dateFormats.length) {
try {
result = dateFormats[i].parse(strdate);
// logger.debug("******Parsing Success "+strdate+"->"+result+" with
// "+dateFormats[i].toPattern());
break;
} catch (java.text.ParseException eA) {
i++;
}
}


return result;
}


/**
* Format a date in a manner that would be most suitable for serialized
* storage.
*
* @param date
* {@link Date} object to format.
*
* @return
* Robust, formatted date string.
*/
public static String formatDate(Date date)
{
return dateFormats[dateFormat_default].format(date);
}
}

更多相关文章

  1. android TelephonyManager
  2. android得到系统时间如何判断是白天还是晚上
  3. Android判断文件类型(视频、音频、图片等)
  4. network: Android(安卓)网络判断(wifi、3G与其他)
  5. android判断手机有没有安装微博
  6. android 判断是否连接钢琴和检测钢琴是哪个按键
  7. android 网络获取图片并存储
  8. android判断当前设备的支持哪些Feature
  9. 23_传智播客Android视频教程_网络通信之网络图片查看器

随机推荐

  1. 2011.09.01(2)——— android 处理双击事件
  2. 控制软键盘显示和隐藏
  3. Android使用自定义AlertDialog
  4. android绘画折线图二
  5. android:installLocation简析
  6. Ubuntu14.04下最新Android(安卓)NDK安装
  7. 利用Handler来更新android的UI
  8. Android短信发送器实现方法
  9. What Is ADB And How To Install It With
  10. Android(安卓)Bundle类