package com.example.playaudiotest;
import android.media.MediaPlayer;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.File;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button start ;
private Button pause ;
private Button stop ;
private MediaPlayer mediaPlayer = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start =(Button) findViewById(R.id.start);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById( R.id.stop);
start.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
initMediaPlayer();//初始化MediaPlayer
}
private void initMediaPlayer(){
try{
File file = new File(Environment.getExternalStorageDirectory(),"music.mp3");
mediaPlayer.setDataSource(file.getPath());//指定音频文件的路径
mediaPlayer.prepare(); //让MediaPlayer进入到准备状态
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.start:
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
break;
case R.id.pause:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
break;
case R.id.stop:
if(mediaPlayer.isPlaying()){
mediaPlayer.reset(); //停止播放;
initMediaPlayer(); //初始化
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.release();//释放资源
}
}
}
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/start"
android:text="start"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="pause"
android:id="@+id/pause"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="stop"
android:id="@+id/stop"/>
</LinearLayout>
/视频播放器
注意:VideoView并不是一个万能的视频播放器,他在视频格式的支持以及播放效率都存在这很大的不足。但用于播放一些视频的片头动画,或某个应用的视频宣传,使用VideoView还是绰绰有余的
package com.example.playvideotest;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
import java.io.File;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private VideoView videoView ;
private Button start ;
private Button pause;
private Button stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
videoView = (VideoView) findViewById(R.id.video_view);
start.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
initVideoPath();
}
private void initVideoPath(){
File file = new File(Environment.getExternalStorageDirectory(),"movie.mp4");
videoView.setVideoPath(file.getPath());//指定音频文件的路径
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.start:
if(!videoView.isPlaying()){
videoView.start();
}
break;
case R.id.pause:
if(videoView.isPlaying()){
videoView.pause();
}
break;
case R.id.stop:
if(videoView.isPlaying()){
videoView.resume();//背后仍然是MediaPlayer控制
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (videoView!=null){
videoView.suspend();
}
}
}
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/start"
android:text="start"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="pause"
android:id="@+id/pause"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="stop"
android:id="@+id/stop"/>
</LinearLayout>
</LinearLayout>