1、ctivity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:text="播放音乐" android:onClick="playMusic" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="播放视频_1" android:onClick="playVedio_1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="播放视频_2" android:onClick="playVedio_2" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="播放视频_3" android:onClick="playVedio_3" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="录音" android:onClick="recorder_test" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="录像" android:onClick="recorder_video_test" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
2、MainActivity.java
package com.example.android_media; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //播放音乐 public void playMusic(View view){ Intent intent=new Intent(this,PlayMusicActivity.class); startActivity(intent); } //播放视频一 public void playVedio_1(View view){ Intent intent=new Intent(Intent.ACTION_VIEW);
//定义视频路径 Uri uri= Uri.parse("file:///sdcard/goodmm.mp4");
//根据uri查找指定类型的文件 intent.setDataAndType(uri,"video/mp4"); startActivity(intent); } //播放视频二 public void playVedio_2(View view){ Intent intent=new Intent(this,PlayVideoActivity.class); startActivity(intent); } //播放视频三 public void playVedio_3(View view){ Intent intent=new Intent(this,PlayVideo_2Activity.class); startActivity(intent); } //录音 public void recorder_test(View view){ Intent intent=new Intent(this,RecorderActivity.class); startActivity(intent); } //录像 public void recorder_video_test(View view){ Intent intent=new Intent(this,Recorder_VideoActivity.class); startActivity(intent); } }
3、activity_play_music.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_play_music" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <SeekBar android:id="@+id/sb" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:text="播放" android:onClick="doPlay" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:text="暂停" android:onClick="doPause" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:text="停止" android:onClick="doStop" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
4、PlayMusicActivity.java
package com.example.android_media; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Environment; import android.os.SystemClock; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.SeekBar; import java.io.IOException; public class PlayMusicActivity extends Activity { //拖动条 private SeekBar sb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_play_music); sb=(SeekBar)findViewById(R.id.sb);
//设置进度条改变监听事件 sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { if(mp!=null)
//改变音频播放进度,通过拖动条的位置实现快进和后退 mp.seekTo(seekBar.getProgress()); } }); } //播放音乐
//播放器 MediaPlayer mp; boolean isRun=true; public void doPlay(View view){ if(mp!=null) return; isRun=true;
//实例化播放器 mp=new MediaPlayer();//构建一个播放器对象
//使用reset()方法来恢复一些意外错误 mp.reset();
//获取音频路径 String path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/nobody.mp3"; try {
//给播放器加载资源 mp.setDataSource(path);//设置要播放的文件路径 mp.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置多媒体类型 mp.prepare();//准备就绪
//getDuration() 获取音频文件的总长度,将其设置为进度条的长度 sb.setMax(mp.getDuration());//设置进度条最大值 mp.start();//开始播放 //更新进度 new Thread(new Runnable() { @Override public void run() {
//mp.getCurrentPosition()获取音频当前播放点
//如果正在播放并且当前播放点小于音频的最大长度
while(isRun&&mp.getCurrentPosition()<=sb.getMax()){
//设置拖动条当前的位置和音频播放的当前播放点的位置保持一致 sb.setProgress(mp.getCurrentPosition()); SystemClock.sleep(200); } } }).start(); } catch (IOException e) { e.printStackTrace(); } } public void doPause(View view){ if(mp==null) return; if(mp.isPlaying()){ mp.pause(); ((Button)view).setText("继续播放"); }else{ mp.start(); ((Button)view).setText("暂停"); } } public void doStop(View view){ if(mp!=null&&mp.isPlaying()){ isRun=false; mp.stop(); mp.release(); mp=null; } } @Override protected void onDestroy() { super.onDestroy(); doStop(null); } }
5、activity_play_video.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_play_video" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <VideoView android:id="@+id/videoView" android:background="@drawable/bg" android:layout_width="match_parent" android:layout_height="300dp" /> </LinearLayout>
6、PlayVideoActivity.java
package com.example.android_media; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.os.SystemClock; import android.widget.MediaController; import android.widget.VideoView; public class PlayVideoActivity extends Activity { private VideoView videoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_play_video); videoView=(VideoView)findViewById(R.id.videoView); videoView.setMediaController(new MediaController(this)); Uri uri= Uri.parse("file:///sdcard/goodmm.mp4"); videoView.setVideoURI(uri); new Thread(new Runnable() { @Override public void run() { SystemClock.sleep(5000); runOnUiThread(new Runnable() { @Override public void run() { videoView.setBackground(null); videoView.start(); } }); } }).start(); } }
7、activity_play_video_2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_play_music" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <SurfaceView android:id="@+id/sfView" android:background="@drawable/bg" android:layout_width="match_parent" android:layout_height="260dp" /> <SeekBar android:id="@+id/sb" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:text="播放" android:onClick="doPlay" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:text="暂停" android:onClick="doPause" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:text="停止" android:onClick="doStop" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
8、PlayVideo_2Activity.java
package com.example.android_media; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Environment; import android.os.SystemClock; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import android.widget.SeekBar; import java.io.IOException; public class PlayVideo_2Activity extends Activity { private MediaPlayer mp; private SurfaceView sfView; private SurfaceHolder holder; private SeekBar sb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_play_video_2); sb=(SeekBar)findViewById(R.id.sb); sfView=(SurfaceView)findViewById(R.id.sfView); holder=sfView.getHolder(); sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { if(mp!=null) mp.seekTo(seekBar.getProgress()); } }); } boolean isRun=true; public void doPlay(View view){ if(mp!=null) return; isRun=true; mp=new MediaPlayer(); mp.reset(); String path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/goodmm.mp4"; try { mp.setDataSource(path);//设置数据源 mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.setDisplay(holder);//设置显示位置 mp.prepare(); sb.setMax(mp.getDuration()); sfView.setBackground(null); mp.start(); new Thread(new Runnable() { @Override public void run() { while (isRun&&mp.getCurrentPosition()<=sb.getMax()){ sb.setProgress(mp.getCurrentPosition()); SystemClock.sleep(200); } } }).start(); } catch (IOException e) { e.printStackTrace(); } } public void doPause(View view){ if(mp==null) return; if(mp.isPlaying()){ mp.pause(); ((Button)view).setText("继续播放"); }else{ mp.start(); ((Button)view).setText("暂停"); } } public void doStop(View view){ if(mp!=null&&mp.isPlaying()){ isRun=false; mp.stop(); mp.release(); mp=null; } } @Override protected void onDestroy() { super.onDestroy(); doStop(null); } }
9、activity_recorder_video.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_recorder__video" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <SurfaceView android:id="@+id/sfView" android:layout_width="match_parent" android:layout_height="300dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:text="开始录像" android:onClick="doStart" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:text="停止录像" android:onClick="doStop" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:text="播放录像" android:onClick="doPlay" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
10、Recorder_VideoActivity.java
package com.example.android_media; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import java.io.IOException; public class Recorder_VideoActivity extends Activity { private MediaPlayer mp; private SurfaceView sfView; private SurfaceHolder holder; private MediaRecorder recorder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recorder__video); sfView=(SurfaceView)findViewById(R.id.sfView); holder=sfView.getHolder(); } //开始录像 public void doStart(View view){ recorder=new MediaRecorder(); recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); String path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/luxiang.mp4"; recorder.setOutputFile(path); recorder.setPreviewDisplay(holder.getSurface()); try { recorder.prepare(); recorder.start(); } catch (IOException e) { e.printStackTrace(); } } public void doStop(View view){ if(recorder!=null){ try{ recorder.stop(); }catch (Exception ex){ ex.printStackTrace(); } } } public void doPlay(View view){ if(mp!=null) return; mp=new MediaPlayer(); mp.reset(); String path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/luxiang.mp4"; try { mp.setDataSource(path);//设置数据源 mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.setDisplay(holder);//设置显示位置 mp.prepare(); sfView.setBackground(null); mp.start(); }catch (Exception ex){ ex.printStackTrace(); } } }
11、activity_recorder.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_recorder" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:text="开始录音" android:onClick="doStart" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="结束录音" android:onClick="doStart" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="播放录音" android:onClick="doPlay" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
12、RecorderActivity.java
package com.example.android_media; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.view.View; import java.io.IOException; public class RecorderActivity extends Activity { private MediaRecorder recorder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recorder); } //开始录音 public void doStart(View view){ if(recorder!=null) return; recorder=new MediaRecorder();//构建了一个录音设备 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置声音来源 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//设置录音文件格式 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);//设置声音编码类型 String path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/luyin.3gp"; recorder.setOutputFile(path);//设置输出文件位置 try { recorder.prepare();//录音设备就绪 recorder.start();//开始录音 } catch (IOException e) { e.printStackTrace(); } } //录音结束 public void doStop(View view){ if(recorder!=null){ recorder.stop(); recorder.release(); recorder=null; } } //播放录音 MediaPlayer mp; public void doPlay(View view){ if(mp!=null) return; mp=new MediaPlayer(); mp.reset(); String path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/luyin.3gp"; try { mp.setDataSource(path); mp.prepare(); mp.start(); } catch (IOException e) { e.printStackTrace(); } } @Override protected void onDestroy() { super.onDestroy(); doStop(null); } }
13、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_media"> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.CAMERA" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".PlayMusicActivity" /> <activity android:name=".PlayVideoActivity" /> <activity android:name=".PlayVideo_2Activity" /> <activity android:name=".RecorderActivity" /> <activity android:name=".Recorder_VideoActivity"></activity> </application> </manifest>
14、bg.png