一个简单地音乐播放程序

一个简单地音乐播放程序_第1张图片

package com.example.day13_player;

import java.io.File;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
private EditText et_address;


private MediaPlayer mediaPlayer;


// 记录视频播放的位置
private int currentPosition;
private boolean isPlay;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建的mediaPlayer对象
mediaPlayer = new MediaPlayer();


et_address = (EditText) findViewById(R.id.et_address);


}


/**
* 音乐播放

* @param v
*/
public void mediaPlay(View v) {


try {
String path = et_address.getText().toString();
File sdCardFile = Environment.getExternalStorageDirectory();
path = sdCardFile.getPath() + "/Music/" + path;
System.out.println("输出path的路径为:" + path);
if (TextUtils.isEmpty(path) || "".equals(path)) {
Toast.makeText(this, "音乐播放文件的路径不能为空", Toast.LENGTH_LONG).show();


} else {
// 播放准备的过程中
if (!isPlay) {
// 创建的mediaPlayer对象
mediaPlayer = new MediaPlayer();


/**
* 视频播放完成出发的事件
*/
mediaPlayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {


@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
isPlay = false;
mp = null;


}
});


/**
* 播放出差错的时候触发的事件
*/
mediaPlayer
.setOnErrorListener(new MediaPlayer.OnErrorListener() {


@Override
public boolean onError(MediaPlayer mp,
int what, int extra) {
mp.release();
mp = null;
isPlay = false;
return false;
}
});
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();// might take long! (for buffering,
// etc)
mediaPlayer.start();


// 让isPlay变为真
isPlay = true;
}
}


} catch (Exception e) {
e.printStackTrace();
}


}


/**
* 音乐暂停

* @param v
*/
public void mediaPause(View v) {


if (mediaPlayer != null && isPlay) {
mediaPlayer.pause();


}
Toast.makeText(this, "音乐暂停", Toast.LENGTH_LONG).show();
}


/**
* 音乐继续播放

* @pram v
*/
public void mediaResumePlay(View v) {


if (mediaPlayer != null && isPlay) {
mediaPlayer.start();
}
Toast.makeText(this, "音乐继续播放", Toast.LENGTH_LONG).show();
}


/**
* 音乐停止

* @param v
*/
public void mediaStop(View v) {
if (mediaPlayer != null && isPlay) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
isPlay = false;
}
Toast.makeText(this, "音乐停止", Toast.LENGTH_LONG).show();


}


@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (mediaPlayer != null && isPlay) {
mediaPlayer.pause();
currentPosition = mediaPlayer.getCurrentPosition();


}


}


@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
if (mediaPlayer != null && isPlay) {
mediaPlayer.seekTo(currentPosition);
mediaPlayer.start();


}
}


@Override
protected void onDestroy() {
super.onDestroy();
// 释放资源
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}


}

====================================================================================

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">day13-player</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="btn_tvplay">播放</string>
    <string name="btn_pausetv">暂停</string>
    <string name="btn_replay">继续播放</string>
    <string name="btn_stoptv">停止</string>
    <string name="fileName">suyan.mp3</string>


</resources>

==========================================================================================

<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=".PlayerActivity" >


    <EditText
        android:id="@+id/et_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:text="@string/fileName" >


        <requestFocus />
    </EditText>


    <Button
        android:id="@+id/btn_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/et_address"
        android:text="@string/btn_tvplay" 
        android:onClick="mediaPlay"/>


    <Button
        android:id="@+id/btn_replay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_address"
        android:layout_toRightOf="@+id/btn_pause"
        android:text="@string/btn_replay" 
        android:onClick="mediaResumePlay"/>


    <Button
        android:id="@+id/btn_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btn_play"
        android:layout_alignBottom="@+id/btn_play"
        android:layout_toRightOf="@+id/btn_play"
        android:onClick="mediaPause"
        android:text="@string/btn_pausetv" />


    <Button
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btn_replay"
        android:layout_alignBottom="@+id/btn_replay"
        android:layout_alignParentRight="true"
        android:onClick="mediaStop"
        android:text="@string/btn_stoptv" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_play"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/btn_stop"
        android:layout_below="@+id/btn_play"
        android:layout_marginTop="19dp"
        android:text="TextView" />


</RelativeLayout>

一个简单地音乐播放程序_第2张图片

你可能感兴趣的:(一个简单地音乐播放程序)