[关闭]
@MasterLeng 2015-04-28T06:15:51.000000Z 字数 2817 阅读 2363

简易音乐播放器

MediaPlayer配合Service使用


android开发里,音乐播放器开发算是初学者基本都会要接触一下的内容,开发过一遍的话会对MediaPlayer和Service的使用有一个大致的了解,程序设计逻辑地深入理解也有一定的益处。不多说,上代码。
MediaPlayer需要放到Service里进行播放,因为是耗时操作,这是明确的。此处的播放器,需要实现打开APP的时候直接从第一首开始播放,选择列表某一首歌播放,可以选择上一首,下一首 播放,暂停操作等。
public class PlayService extends Service
{
    private ArrayList<String> urlList;
    private String action;//定义执行的操作类型
    private int playPostion;//定义播放的位置
    private MediaPlayer mp;
    private int playPosition=0;//播放位置
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return binder;
    }

    //一旦执行startService就会执行此方法
    public void onStartCommand(Intent intent,int flags,int startId)
    {
        urlList = intent.getStringArrayListExtra("urlList");
        action = intent.getAction();
        if(action.equals("com.playservice.play"))
        {
            playPosition = intent.getIntExtra("position");
            play(urlList.get(position));
        }
        else if(action.equals("com.playservice.lastaction"))
        {
            last();
        }else if(action.equals("com.playservice.nextaction"))
        {
            next();
        }else if(action.equals("com.playservice.pause"))
        {
            pause();
        }
        return START_NOT_STICKY;
    }

    public void play(String url)
    {
        if(mp==null)
        {
            mp = new MediaPlayer();
            mp.setDataSource(url);
            mp.prepare();
            mp.start();
            //设置播放完一首自动播放下一首
            vmp.setOnCompletionListener(new onCompletionListener() {
            public void onCompletion(MediaPlayer mp) {                              next();
            }
            });
        }
    }

    //下一首
    public void next()
    {
        playPosition = playPosition + 1;
        if(playPosition<=urlList.size())
        {
            play(urlList.get(playPosition));
        }else
        {
            playPosition = 0;
            play(urlList.get(playPosition));
        }
    }

    //上一首
    public void last()
    {
        playPosition = playPosition - 1;
        if(playPosition >=0)
        {
            play(urlList.get(playPosition));
        }
        else
        {
            playPosition = urlList.size()-1;
            play(urlList.get(playPosition));
        }
    }

    public void onDestroy()
    {
        if(mp!=null)
        {
            mp.stop();
            mp.reset();
            mp.release();
        }
    }

    @Override
    public boolean bindService(Intent service, ServiceConnection conn, int flags) {
        // TODO Auto-generated method stub
        songList = service.getStringArrayListExtra("urlList");
        playPosition = 0;
        play(songList.get(0));
        return super.bindService(service, conn, flags);
}
}

//播放器的封装类
class MediaInstance
{
    ArrayList<String> urlList;
    Intent intent;
    PlayService playService;
    Context context;
    public MediaInstance(ArrayList<String> urlList,Context context,PlayService playService)
    {
        this.urlList = urlList;
        intent = new Intent();
        this.playService = playService;
        this.context = context;
    }

    //自动播放
    public void autoPlay()
    {
        intent.setClass(context, playService.getClass());
        intent.putStringArrayListExtra("urlList", urlList);
        context.bindService(intent,connection,Context.BIND_AUTO_CREATE);
    }

    //下一首
    public void nextSong()
    {
        intent.setClass(context, playService.getClass());
        intent.putStringArrayListExtra("urlList", urlList);
        intent.setAction("com.playservice.nextaction");
        context.startService(intent);
    }

    //上一首
    public void lastSong()
    {
        intent.setClass(context, playService.getClass());
        intent.putStringArrayListExtra("urlList", urlList);
        intent.setAction("com.playservice.lastaction");
        context.startService(intent);
    }

    //关闭服务
    public void stopService()
    {
        if(mBound)
        {
            unbindService();
        }
        context.stopService();
    }
}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注