Android--MediaPlayDemo简单的音乐播放器

简单的音乐播放器,没有加入Service,播放sdcard卡上的音乐文件

Android--MediaPlayDemo简单的音乐播放器_第1张图片

 

 

main.xml代码:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@drawable/test">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />
    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/filenameinit"
        android:id="@+id/filename"/>
    
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="mediaplay"
            android:text="@string/playbutton"
            android:id="@+id/playbutton"/>
        
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="mediaplay"
            android:text="@string/pausebutton"
            android:id="@+id/pausebutton"/>
        
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="mediaplay"
            android:text="@string/resetbutton"
            android:id="@+id/resetbutton"/>
        
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="mediaplay"
            android:text="@string/stopbutton"
            android:id="@+id/stopbutton"/>
    </LinearLayout>

</LinearLayout>


 

 

 

java代码:

 

package cn.mrzhu.mediaplay;

import java.io.File;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MediaPlayActivity extends Activity {

 private EditText nameText;
 private String path;//文件路径
 private MediaPlayer mediaPlayer;
 private boolean pause;//状态标记
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  mediaPlayer = new MediaPlayer();//实例化一个播放器对象
  nameText = (EditText)findViewById(R.id.filename);
 }

 public void mediaplay(View v){
  switch(v.getId()){
  //播放按钮
  case R.id.playbutton:
   String filename = nameText.getText().toString();//取得输入框的内容
   File audio = new File(Environment.getExternalStorageDirectory(), filename);
   if(audio.exists()){
    path = audio.getAbsolutePath();//取得绝对路径
    play();
   }else{
    path = null;
    Toast.makeText(getApplicationContext(), R.string.filenoexist, Toast.LENGTH_LONG).show();
    //提示文件不存在
   }
   break;

  //暂停按钮
  case R.id.pausebutton:
   if(mediaPlayer.isPlaying()){
    mediaPlayer.pause();//暂停
    pause = true;
    ((Button)v).setText(R.string.continues);//将按钮上的内容设置为继续
   }else{
    if(pause){
     mediaPlayer.start();//继续播放
     pause = false;
     ((Button)v).setText(R.string.pausebutton);//将按钮上的内容设置为暂停
    }
   }
   break;

  //重播按钮
  case R.id.resetbutton:
   if(mediaPlayer.isPlaying()){//如果播放器处于播放状态
    mediaPlayer.seekTo(0);//从开始位置开始播放
   }else{
    if(path != null){
     play();
    }
   }
   break;

  //停止按钮
  case R.id.stopbutton:
   if(mediaPlayer.isPlaying()){
    mediaPlayer.stop();
   }
   break;
  }
 }

 private void play(){
  try {
   mediaPlayer.reset();//把各项参数恢复到最初始的状态
   mediaPlayer.setDataSource(path);

   mediaPlayer.prepare();//进行数据缓冲
   mediaPlayer.setOnPreparedListener(new PrepareListener());
  } catch (Exception e) {
   e.printStackTrace();
  } 
 }

 private final class PrepareListener implements OnPreparedListener{

  public void onPrepared(MediaPlayer mp) {
   mediaPlayer.start();//开始播放
  }
 }

 @Override
 protected void onDestroy() {
  mediaPlayer.release();
  mediaPlayer = null;
  super.onDestroy();
 }
}


 

 

String.xml代码:

 

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

    <string name="hello">Hello World, MediaPlayActivity!</string>
    <string name="app_name">MediaPlay</string>
    <string name="Mp3Name">音乐文件</string>
    <string name="playbutton">播放</string>
    <string name="pausebutton">暂停</string>
    <string name="stopbutton">停止</string>
    <string name="resetbutton">重播</string>
    <string name="filenoexist">文件没有发现</string>
    <string name="continues">继续</string>
    <string name="filenameinit">testmp3.mp3</string>

</resources>


 

 

资源下载:http://download.csdn.net/detail/zlqqhs/4810223

 

将要播放的音乐文件放到sdcard卡中(注:不要用中文名)

在输入框中输入音乐文件的名字即可播放

你可能感兴趣的:(android,android,demo,demo,音乐播放器,MediaPlay)