标签:des android blog http java os io 文件
package com.zl.media;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;
import com.zl.listview.R;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;
public class MusicPlayer extends Activity{
private ImageButton btn_play,btn_pause,btn_stop,videoplay;
private MediaPlayer mediaPlayer;
private VideoView videoView;
private MediaRecorder mediarecorder;
private TextView text_play,text_pause;
private Button bt_jump;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.musicplayer);
btn_play = (ImageButton)findViewById(R.id.play);
btn_pause = (ImageButton)findViewById(R.id.pause);
btn_stop = (ImageButton)findViewById(R.id.stop);
videoplay = (ImageButton)findViewById(R.id.videoplay);
videoView = (VideoView)findViewById(R.id.videoview);
text_play = (TextView)findViewById(R.id.text_play);
text_pause = (TextView)findViewById(R.id.text_pause);
bt_jump = (Button)findViewById(R.id.bt_jump);
btn_play.setEnabled(false);
btn_pause.setEnabled(false);
btn_stop.setEnabled(false);
//定义MediaController对象
MediaController mediaController = new MediaController(this);
//把MediaController对象绑定到VideoView上
mediaController.setAnchorView(videoView);
//设置VideoView的控制器是mediaController
videoView.setMediaController(mediaController);
OnClickListener ocl = new OnClickListener() {
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.bt_jump:
// Intent intent = new Intent();
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// ComponentName comp = new ComponentName("com.zl.musicplayer", "com.zl.musicplayer.IYue");
// intent.setComponent(comp);
// intent.setAction("android.intent.action.MAIN");
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// startActivity(intent);
// intent.setComponent(new ComponentName("com.zl.media","com.zl.musicplayer"));
// intent.setAction(Intent.ACTION_VIEW);
// intent.setAction("com.zl.musicplayer");
// startActivity(intent);
Uri uri = Uri.parse("http://blog.sina.com.cn/s/blog_781428870102v0q3.html");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
break;
case R.id.play:
play();
break;
case R.id.pause:
pause();
break;
case R.id.stop:
stop();
break;
case R.id.videoplay:
//这两种方法都可以 videoView.setVideoPath("file:///sdcard/love_480320.mp4");
videoView.setVideoURI(Uri.parse("/sdcard/love_480320.mp4"));
videoView.start();
break;
case R.id.text_play:
File file = new File("/sdcard/"+"ZL"+
new DateFormat().format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA)) + ".amr");
mediarecorder = new MediaRecorder();
// 从麦克风源进行录音
mediarecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
// 设置输出格式
mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
// 设置编码格式
mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
// 设置输出文件
mediarecorder.setOutputFile(file.getAbsolutePath());
try {
// 创建文件
file.createNewFile();
// 准备录制
mediarecorder.prepare();
}catch (IOException e) {
e.printStackTrace();
}
// 开始录制
mediarecorder.start();
text_play.setText("录音中……");
break;
case R.id.text_pause:
if (mediarecorder != null) {
mediarecorder.stop();
mediarecorder.release();
mediarecorder = null;
text_play.setText("录音");
}
break;
}
}
};
btn_play.setOnClickListener(ocl);
btn_pause.setOnClickListener(ocl);
btn_stop.setOnClickListener(ocl);
videoplay.setOnClickListener(ocl);
text_play.setOnClickListener(ocl);
text_pause.setOnClickListener(ocl);
bt_jump.setOnClickListener(ocl);
initMediaPlayer();
}
private void initMediaPlayer(){
mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.song);
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
btn_play.setEnabled(true);
}
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stop();
}
});
}
private void play(){
mediaPlayer.start();
btn_play.setEnabled(false);
btn_pause.setEnabled(true);
btn_stop.setEnabled(true);
}
private void pause(){
mediaPlayer.pause();
btn_play.setEnabled(true);
btn_pause.setEnabled(false);
btn_stop.setEnabled(true);
}
private void stop(){
mediaPlayer.stop();
btn_pause.setEnabled(false);
btn_stop.setEnabled(false);
try {
mediaPlayer.prepare();
mediaPlayer.seekTo(0);
btn_play.setEnabled(true);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
// activity销毁前停止播放
super.onDestroy();
if(btn_stop.isEnabled()){
stop();
}
}
}
标签:des android blog http java os io 文件
原文地址:http://www.cnblogs.com/airry66/p/3914418.html