小型Mp3播放器



 

 

准备三张图片,名字分别为: play、pause、stop。

 

将一个名为Mp3的文件放入res/raw文件夹中。

 

 

在main.xml中:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#000000"

    android:gravity="center_horizontal"

    android:orientation="vertical">

    <TextView

        android:id="@+id/info"

        android:gravity="center_horizontal"

        android:layout_marginTop="8dp"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textColor="#ffffff"

        android:text="等待音频播放"/>

    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="20dp"

        android:gravity="center_horizontal">

        <ImageButton

            android:id="@+id/play"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/play"/>

        <ImageButton

            android:id="@+id/pause"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/pause"/>

        <ImageButton

            android:id="@+id/stop"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/stop"/>

    </LinearLayout>

    <SeekBar

        android:id="@+id/seekbar"

        android:layout_margin="8dp"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"/>

</LinearLayout>

 

 

 

 

 

 

 

在MyMediaPlayerDemo.java中:

 

package com.li.mediaplayer;

 

import android.app.Activity;

import android.media.MediaPlayer;

import android.media.MediaPlayer.OnCompletionListener;

import android.os.AsyncTask;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ImageButton;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

import android.widget.TextView;

 

public class MyMediaPlayerDemo extends Activity {

  private ImageButton play = null ;

  private ImageButton pause = null ;

  private ImageButton stop = null ;

  private TextView info = null ;

  private MediaPlayer myMediaPlayer = null ;

  private SeekBar seekbar = null ;

  private boolean playFlag = true ;  // 播放标记

  private boolean pauseFlag = true ; // 暂停标记

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.info = (TextView) super.findViewById(R.id.info) ;

     this.play = (ImageButton) super.findViewById(R.id.play) ;

     this.pause = (ImageButton) super.findViewById(R.id.pause) ;

     this.stop = (ImageButton) super.findViewById(R.id.stop) ;

     this.seekbar = (SeekBar) super.findViewById(R.id.seekbar) ;

     this.play.setOnClickListener(new PlayOnClickListener()) ;

     this.pause.setOnClickListener(new PauseOnClickListener()) ;

     this.stop.setOnClickListener(new StopOnClickListener()) ;

     this.seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImpl()) ;

  }

 

private class UpdateSeekBar extends AsyncTask<Integer, Integer, String> {

    

     @Override

     protected void onPostExecute(String result) {

       super.onPostExecute(result);

     }

 

     @Override

     protected void onProgressUpdate(Integer... values) {

       MyMediaPlayerDemo.this.seekbar.setProgress(values[0]) ;

     }

 

     @Override

     protected String doInBackground(Integer... params) {

       while(MyMediaPlayerDemo.this.playFlag) {  // 播放的话,进度要增长

         try {

            Thread.sleep(params[0]) ;

         } catch (InterruptedException e) {

         }

         this.publishProgress(MyMediaPlayerDemo.this.myMediaPlayer.getCurrentPosition()) ;

       }

       return null ;

     }

    

  }

 

  private class PlayOnClickListener implements OnClickListener{

 

     public void onClick(View v) {

       MyMediaPlayerDemo.this.myMediaPlayer = MediaPlayer.create(

            MyMediaPlayerDemo.this, R.raw.mp3);  // 要播放的文件

       if (MyMediaPlayerDemo.this.myMediaPlayer != null) {

         MyMediaPlayerDemo.this.myMediaPlayer.stop(); // 停止操作

       }

       MyMediaPlayerDemo.this.seekbar.setMax(MyMediaPlayerDemo.this.myMediaPlayer.getDuration()) ;  // 设置拖拽条的长度

       UpdateSeekBar update = new UpdateSeekBar() ;

       update.execute(1000) ;

      

       MyMediaPlayerDemo.this.myMediaPlayer.setOnCompletionListener(new OnCompletionListener(){

 

         public void onCompletion(MediaPlayer mp) {

            MyMediaPlayerDemo.this.playFlag = false ; // 播放完毕

            MyMediaPlayerDemo.this.myMediaPlayer.release() ;  // 释放资源

         }}) ;

       try {

         MyMediaPlayerDemo.this.myMediaPlayer.prepare() ;

         MyMediaPlayerDemo.this.myMediaPlayer.start() ;

         MyMediaPlayerDemo.this.info.setText("正在播放音频文件...") ;

       } catch (Exception e) {

         MyMediaPlayerDemo.this.info.setText("文件播放出现异常," + e) ;

       }

      

     }

    

  }

  private class PauseOnClickListener implements OnClickListener{

 

     public void onClick(View v) {

       if(MyMediaPlayerDemo.this.myMediaPlayer != null) {

         if (MyMediaPlayerDemo.this.pauseFlag) { // 现在暂停

            MyMediaPlayerDemo.this.myMediaPlayer.start();

            MyMediaPlayerDemo.this.pauseFlag = false ;

         } else {

            MyMediaPlayerDemo.this.myMediaPlayer.pause(); // 暂停

            MyMediaPlayerDemo.this.pauseFlag = true ;

         }

       }

     }

    

  }

  private class StopOnClickListener implements OnClickListener{

 

     public void onClick(View v) {

       if(MyMediaPlayerDemo.this.myMediaPlayer != null) {

         MyMediaPlayerDemo.this.myMediaPlayer.stop() ;  // 停止

         MyMediaPlayerDemo.this.info.setText("停止播放音频文件...") ;

       }

     }

  }

  private class OnSeekBarChangeListenerImpl implements OnSeekBarChangeListener {

 

     public void onProgressChanged(SeekBar seekBar, int progress,

         boolean fromUser) {

      

     }

 

     public void onStartTrackingTouch(SeekBar seekBar) {

      

     }

 

     public void onStopTrackingTouch(SeekBar seekBar) {

       MyMediaPlayerDemo.this.myMediaPlayer.seekTo(seekBar.getProgress()) ;

     }

    

  }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值