首页 > 代码库 > 独立开发音乐播放器

独立开发音乐播放器

第一先整体布置好ui布局   开始动手:
  先找好素材, 修改了mainActivity  去掉title

    去掉title 方法之前已近写过了就不多说了!

定义main.xml 进入应用程序的布局  
 

 

搞了一整天  主要是素材难搞

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" >      <ImageView         android:id="@+id/mainIco"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="1"        android:src="@drawable/backg"                />   <SeekBar        android:id="@+id/seekBar"       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:minHeight="3pt"       android:maxHeight="3pt"       android:paddingLeft="2pt"       android:paddingRight="2pt"       android:scrollbars="horizontal"       android:thumb="@drawable/bar"       android:thumbOffset="1pt"       />   <RelativeLayout       android:layout_width="fill_parent"       android:layout_height="fill_parent"       android:layout_weight="1" >       <TextView           android:id="@+id/startTime"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_alignParentLeft="true"           android:layout_alignParentTop="true"           android:text="00:20" />       <TextView           android:id="@+id/startTime"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_alignParentRight="true"           android:layout_alignParentTop="true"           android:text="03:20" />       <ImageButton           android:id="@+id/play"           android:layout_width="10pt"           android:layout_height="10pt"           android:layout_centerHorizontal="true"           android:layout_centerVertical="true"           android:background="@drawable/image_button"           android:src="@drawable/play"            android:onClick="mediaPlay"           />       <ImageButton           android:id="@+id/fast"           android:layout_width="10pt"           android:layout_height="10pt"           android:layout_alignBottom="@id/play"           android:layout_toRightOf="@id/play"           android:background="@drawable/image_button"           android:src="@drawable/fast" />       <ImageButton           android:id="@+id/next"           android:layout_width="10pt"           android:layout_height="10pt"           android:layout_alignBottom="@id/fast"           android:layout_toRightOf="@id/fast"           android:background="@drawable/image_button"           android:src="@drawable/next" />              <ImageButton           android:id="@+id/slow"           android:layout_width="10pt"           android:layout_height="10pt"           android:layout_alignBottom="@id/play"           android:layout_toLeftOf="@id/play"           android:background="@drawable/image_button"           android:src="@drawable/slow" />              <ImageButton           android:id="@+id/last"           android:layout_width="10pt"           android:layout_height="10pt"           android:layout_alignBottom="@id/slow"           android:layout_toLeftOf="@id/slow"           android:background="@drawable/image_button"           android:src="@drawable/last" />          </RelativeLayout>   </LinearLayout>
package codefans.app.service;import java.io.File;import java.io.IOException;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.media.MediaPlayer.OnPreparedListener;import android.os.Binder;import android.os.Environment;import android.os.IBinder;public class Mp3Service extends Service{    private IBinder myBinder = new Mp3IBinder();    private MediaPlayer mediaPlayer;    private File musicFile;        @Override    public void onCreate()      {        if(this.mediaPlayer == null)        {            this.mediaPlayer = new MediaPlayer();        }    }        @Override    public void onDestroy()    {        if(this.mediaPlayer != null)        {            this.mediaPlayer.stop();            this.mediaPlayer.release();            this.mediaPlayer = null;        }        super.onDestroy();    }        @Override    public IBinder onBind(Intent intent)    {        return myBinder;    }    //开始播放    private void playMusci(String fileName)    {        try        {            this.musicFile = new File(Environment.getExternalStorageDirectory(), fileName);            this.mediaPlayer.setDataSource(musicFile.getAbsolutePath());            this.mediaPlayer.prepare();            this.mediaPlayer.setOnPreparedListener(new MyPreParedListenner());        }        catch (IllegalArgumentException e)        {            e.printStackTrace();        }        catch (IllegalStateException e)        {            e.printStackTrace();        }        catch (IOException e)        {            e.printStackTrace();        }            }        //mediaPlayer缓存完执行事件    private class MyPreParedListenner implements OnPreparedListener    {        public void onPrepared(MediaPlayer mp)        {            Mp3Service.this.mediaPlayer.start();        }            }        /**     * 新建一个内部类 Activity调用service时返回此binder     * @author Codefans     */    private class Mp3IBinder extends Binder implements Player    {        public void playMusic(String filePath)        {            Mp3Service.this.playMusci(filePath);        }            }}

service 播放功能

package codefans.app.activity;import codefans.app.service.Player;import codefans.mp3layer.R;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.widget.ImageButton;public class Mp3PlayerActivity extends Activity{    private boolean flag = false;    private ImageButton playBt;    private Player binder;    private ServiceConnection myServiceConnection = new MyServiceConnection();    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        this.playBt = (ImageButton) findViewById(R.id.play);        Intent intent = new Intent("codefans.action.Mp3Player");        bindService(intent, myServiceConnection, Context.BIND_AUTO_CREATE);            }            //播放按钮点击    public void mediaPlay(View v)    {        if(flag == false)  //改变播放按钮图标        {           playBt.setImageResource(R.drawable.stop);           flag = true;                     this.binder.playMusic("qq.mp3");                                         }        else        {             playBt.setImageResource(R.drawable.play);             flag = false;        }            }        @Override    protected void onDestroy()    {                unbindService(myServiceConnection);                super.onDestroy();    }                //启动服务需要类    private class MyServiceConnection implements ServiceConnection    {        public void onServiceConnected(ComponentName name, IBinder service)        {            Mp3PlayerActivity.this.binder = (Player) service;                    }        public void onServiceDisconnected(ComponentName name)        {            Mp3PlayerActivity.this.binder = null;        }    }}

这里用bindservice 好像不行,因为这样如果挂掉Activity 那个service服务也停止了  应该用startservice  ! 这个明天再写了 很迟了(嘿嘿)

 

只写了一个播放功能

 

   

package codefans.app.activity;import codefans.app.util.Mp3Util;import codefans.mp3layer.R;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;import android.widget.ImageButton;import android.widget.SeekBar;public class Mp3PlayerActivity extends Activity{    private boolean flag = false;  //播放  暂停 变量判断        private ImageButton playBt;    private SeekBar seekBar;                private Intent intent;   //播放信息意图    private Bundle bundle;   //意图传递的信息        private SeekBarReceiver receiver;    private int position;   //播放进度条位置            @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                this.playBt = (ImageButton) findViewById(R.id.play);        this.seekBar = (SeekBar) findViewById(R.id.seekBar);                        //意图   启动service使用        this.intent = new Intent("codefans.action.Mp3Player");        bundle = new Bundle();                        //注册广播接受器        receiver = new SeekBarReceiver();          IntentFilter filter = new IntentFilter();        filter.addAction("codefans.intent.action.Mp3Player");        this.registerReceiver(receiver, filter);            }            //播放按钮点击    public void mediaPlay(View v)    {        if(flag == false)  //改变播放按钮图标        {           playBt.setImageResource(R.drawable.stop);           flag = true;           this.bundle.putString("filePath", "qq.mp3");           this.bundle.putInt("operate", Mp3Util.PLAY);           this.intent.putExtras(bundle);           startService(intent);                              }        else        {             playBt.setImageResource(R.drawable.play);             flag = false;                          this.bundle.putInt("operate", Mp3Util.STOP);             this.intent.putExtras(bundle);             startService(intent);                     }            }        @Override    protected void onDestroy()    {        this.unregisterReceiver(receiver);  //解除接受广播                         super.onDestroy();    }                                /**     *   bindService 实现需要  这里使用startservice      * @author Codefans     *            //启动服务需要类    private class MyServiceConnection implements ServiceConnection    {        public void onServiceConnected(ComponentName name, IBinder service)        {            Mp3PlayerActivity.this.binder = (Player) service;                    }        public void onServiceDisconnected(ComponentName name)        {            Mp3PlayerActivity.this.binder = null;        }                    }     */            /**     * 播放进度条 位置广播接受器     * @author Codefans     *     */    private class SeekBarReceiver extends BroadcastReceiver    {        @Override        public void onReceive(Context context, Intent intent)        {            Mp3PlayerActivity.this.position = intent.getIntExtra("position", 0);                      //  Message msg = new Message();            Mp3PlayerActivity.this.seekBar.setProgress(position);                    //    Log.i("result", String.valueOf(position));                   }            }        }
package codefans.app.service;import java.io.File;import java.io.IOException;import codefans.app.util.Mp3Util;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.media.MediaPlayer.OnPreparedListener;import android.os.Bundle;import android.os.Environment;import android.os.IBinder;public class Mp3Service extends Service{    private MediaPlayer mediaPlayer;    private File musicFile;    private String name;    @Override    public void onCreate()    {        if (this.mediaPlayer == null)        {            this.mediaPlayer = new MediaPlayer();        }    }    @Override    public void onDestroy()    {        if (this.mediaPlayer != null)        {            this.mediaPlayer.stop();            this.mediaPlayer.release();            this.mediaPlayer = null;        }        super.onDestroy();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId)    {        Bundle bundle = intent.getExtras();        switch (bundle.getInt("operate"))        {        case Mp3Util.PLAY:            this.playMusci(bundle.getString("filePath"));            break;        case Mp3Util.STOP:            this.stop();            break;        }        return START_STICKY;    }    @Override    public IBinder onBind(Intent intent)    {        return null;    }    // 开始播放    private void playMusci(String fileName)    {        try        {            if (fileName.equals(name))  //播放同文件              {                this.mediaPlayer.start();            }            else   //播放不同文件            {                this.name = fileName;                this.musicFile = new File(                        Environment.getExternalStorageDirectory(), fileName);                this.mediaPlayer.reset();                this.mediaPlayer.setDataSource(musicFile.getAbsolutePath());                this.mediaPlayer.prepare();                this.mediaPlayer                        .setOnPreparedListener(new MyPreParedListenner());                                Thread t = new SendBroadCastThread();                t.start();                                                            }        }        catch (IllegalArgumentException e)        {            e.printStackTrace();        }        catch (IllegalStateException e)        {            e.printStackTrace();        }        catch (IOException e)        {            e.printStackTrace();        }    }    private void stop() //暂停    {        this.mediaPlayer.pause();    }    // mediaPlayer缓存完执行事件    private class MyPreParedListenner implements OnPreparedListener    {        public void onPrepared(MediaPlayer mp)        {            mp.start();        }    }    /**     * 发送播放位置线程, 每隔一秒发送一次广播 改变播放器进度条     * @author Codefans     *     */    private class SendBroadCastThread extends Thread    {                @Override        public void run()        {            while(true)            {                Intent intent = new Intent();                intent.putExtra("position", Mp3Service.this.mediaPlayer.getCurrentPosition());                                intent.setAction("codefans.intent.action.Mp3Player");// action与接收器相同                sendBroadcast(intent);  //发送广播                                try                {                    Thread.sleep(1000);                }                catch (InterruptedException e)                {                    e.printStackTrace();                }                            }        }                    }                /**     * 新建一个内部类 Activity调用service时返回此binder     *      * @author Codefans     *      *         private class Mp3IBinder extends Binder implements Player {     *      *         public void playMusic(String filePath) {     *         Mp3Service.this.playMusci(filePath); }     *      *         }     */}

今天主要写了关闭Activity后台依然在唱歌   还有一个就是进度条跟随播放 前进

独立开发音乐播放器