package com.example.twentycode;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @author HD
* @date 2015-12-7
* @package_name com.example.twentycode
* @file_name MainActivity.java
*/
public class MainActivity extends Activity implements OnClickListener {
private Button btnPlay;
private Button btnPause;
private Button btnReset;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();
initView();
initMediaPlay();
}
private void initView() {
btnPlay = (Button) findViewById(R.id.btnPlay);
btnPause = (Button) findViewById(R.id.btnPause);
btnReset = (Button) findViewById(R.id.btnReset);
btnPlay.setOnClickListener(this);
btnPause.setOnClickListener(this);
btnReset.setOnClickListener(this);
}
private void initMediaPlay() {
File file = new File(Environment.getExternalStorageDirectory(),
"水边的阿狄丽娜.mp3");
Log.i("hhhd", file.getPath());
try {
mediaPlayer.setDataSource(file.getPath());
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnPlay:
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
break;
case R.id.btnPause:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
break;
case R.id.btnReset:
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
initMediaPlay();
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.twentycode.MainActivity" >
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="104dp"
android:text="播放" />
<Button
android:id="@+id/btnPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="54dp"
android:text="暂停" />
<Button
android:id="@+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnPlay"
android:layout_marginTop="109dp"
android:layout_toLeftOf="@+id/btnPlay"
android:text="停止" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.twentycode"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>