音乐播放器的代码

1,界面设计的代码

<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"
    tools:context=".MainActivity"
    android:orientation="vertical" >

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/music_text" />

    <EditText
        android:id="@+id/musicTv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="My Love.mp3"/>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*" >

        <TableRow >

            <Button
                android:id="@+id/playBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/play_text" />
            <Button
                android:id="@+id/pauseBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/pause_text" />
            <Button
                android:id="@+id/resetBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/reset_text" />
            <Button
                android:id="@+id/stopBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/stop_text" />
        </TableRow>
    </TableLayout>

</LinearLayout>

2.activity的代码

package com.example.musicplayer;

import java.io.File;
import java.io.IOException;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
 Button playBtn, pauseBtn, resetBtn, stopBtn;
 EditText musicTv;

 String musicName = "";
 MediaPlayer player = null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  player = new MediaPlayer();
  
  musicTv = (EditText) findViewById(R.id.musicTv);
  
  playBtn = (Button) findViewById(R.id.playBtn);
  pauseBtn = (Button) findViewById(R.id.pauseBtn);
  resetBtn = (Button) findViewById(R.id.resetBtn);
  stopBtn = (Button) findViewById(R.id.stopBtn);
  
  ButtonOnClickListener btnOnClick = new ButtonOnClickListener();
  playBtn.setOnClickListener(btnOnClick);
  pauseBtn.setOnClickListener(btnOnClick);
  resetBtn.setOnClickListener(btnOnClick);
  stopBtn.setOnClickListener(btnOnClick);

 }

 class ButtonOnClickListener implements OnClickListener {

  @Override
  public void onClick(View v) {
   musicName = musicTv.getText().toString();

   // 判断有没有sd卡
   if (Environment.getExternalStorageState().equals(
     Environment.MEDIA_MOUNTED)) {
    File file = new File(Environment.getExternalStorageDirectory(),
      musicName);
    // 判断有没有要播放的文件
    if (file.exists()) {
     try {
      switch (v.getId()) {
      case R.id.playBtn:
       playMusic(file);
       break;
      case R.id.pauseBtn:
       if (player == null)
        break;
       if (player.isPlaying()) {
        player.pause();
        pauseBtn.setText(R.string.continue_text);
       } else {
        player.start();
        pauseBtn.setText(R.string.pause_text);
       }
       break;
      case R.id.resetBtn:
       if (player.isPlaying()) {
        player.seekTo(0);
       } else {
        playMusic(file);
       }
       break;
      case R.id.stopBtn:
       if (player.isPlaying()) {
        player.stop();
       }
       break;
      }
     } catch (Exception e) {
      Log.e("TAG", e.toString());
     }
    } else {
     Toast.makeText(MainActivity.this, R.string.nofile, 1)
       .show();
    }
   } else {
    Toast.makeText(MainActivity.this, R.string.SDCarderror, 1)
      .show();
   }

  }

  private void playMusic(File file) {
   // TODO Auto-generated method stub
   if (player != null) {
    player.reset();
    try {
     player.setDataSource(file.getAbsolutePath());
     player.prepare();
    } catch (Exception e) {
     e.printStackTrace();
    }
    player.start();
   }

  }

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值