Android在Service里播放与控制音乐

Wesley13
• 阅读 248

文章原创,转载请注明出处:http://blog.csdn.net/feifei454498130/article/details/9306631

设计如下:

Android在Service里播放与控制音乐

 Service的代码如下,通过Binder去控制播放。监听来电没写,在Service里监听就行了。

public class MediaService extends Service {

    static final int PLAY_LIST[] = {
        R.raw.bigben
    };
    
    private MediaPlayer mediaPlayer;
    private HashSet<OnMediaChangeListener> listeners;
    
    private MediaBinder mBinder;
    
    @Override
    public void onCreate() {
        super.onCreate();
        
        listeners = new HashSet<OnMediaChangeListener>();
        mBinder = new MediaBinder();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        listeners.clear();
        try {
            mediaPlayer.stop();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(intent != null && intent.getAction() != null) {
            Intent i = new Intent(intent.getAction());
            sendBroadcast(i);
        }
        return super.onStartCommand(intent, flags, startId);
    }
    
    private void notifyAllPlay() {
        Iterator<OnMediaChangeListener> iterator = listeners.iterator();
        while(iterator.hasNext()) {
            iterator.next().onMediaPlay();
        }
    }
    
    private void notifyAllPause() {
        Iterator<OnMediaChangeListener> iterator = listeners.iterator();
        while(iterator.hasNext()) {
            iterator.next().onMediaPause();
        }
    }
    
    private void notifyAllStop() {
        Iterator<OnMediaChangeListener> iterator = listeners.iterator();
        while(iterator.hasNext()) {
            iterator.next().onMediaStop();
        }
    }
    
    private void notifyAllCompletion() {
        Iterator<OnMediaChangeListener> iterator = listeners.iterator();
        while(iterator.hasNext()) {
            iterator.next().onMediaCompletion();
        }
    }
    
    MediaPlayer.OnCompletionListener mOnCompletionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            notifyAllCompletion();
        }
    };
    
    public class MediaBinder extends Binder {
        public void addOnMediaChangeListener(OnMediaChangeListener listener) {
            if(listener != null) {
                listeners.add(listener);
            }
        }
        
        public void removeOnMediaChangeListener (OnMediaChangeListener listener) {
            if(listener != null) {
                listeners.remove(listener);
            }
        }
        
        public void play() {
            if(mediaPlayer == null) {
                mediaPlayer = MediaPlayer.create(MediaService.this, PLAY_LIST[0]);
                mediaPlayer.setOnCompletionListener(mOnCompletionListener);
            }
            mediaPlayer.start();
            notifyAllPlay();
        }
        
        public void pause() {
            if(mediaPlayer != null) {
                mediaPlayer.pause();
            }
            notifyAllPause();
        }
        
        public void stop() {
            if(mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer = null;
            }
            notifyAllStop();
        }
        
        public boolean isPlaying() {
            if(mediaPlayer != null) {
                return mediaPlayer.isPlaying();
            }
            return false;
        }
        
        public int getDuration() {
            if(mediaPlayer != null) {
                return mediaPlayer.getDuration();
            }
            return -1;
        }
        
        public int getCurrentPosition() {
            if(mediaPlayer != null) {
                return mediaPlayer.getCurrentPosition();
            }
            return -1;
        }
        
        public void seek(int sec) {
            if(mediaPlayer != null) {
                try {
                    mediaPlayer.seekTo(sec);
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

源码下载地址:http://download.csdn.net/detail/feifei454498130/5740835

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
java多线程(二)锁对象
转载请注明出处:http://blog.csdn.net/xingjiarong/article/details/47679007(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fblog.csdn.net%2Fxingjiarong%2Farticle%2Fdetails%2F4
Wesley13 Wesley13
2年前
Java之——实现微信小程序加密数据解密算法
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/79450115(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fblog.csdn.net%2Fl1028386804%2Farticle%2Fdetails%2F79450
Stella981 Stella981
2年前
Gif开发笔记(一):gif介绍、编译和工程模板
若该文为原创文章,转载请注明原文出处本文章博客地址:https://blog.csdn.net/qq21497936/article/details/110530966(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fqq21497936%2Fartic
Wesley13 Wesley13
2年前
Android 切换系统语言源码分析(上)
转载请标明出处:http://blog.csdn.net/u011974987/article/details/50793343(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fblog.csdn.net%2Fu011974987%2Farticle%2Fdetails%2F5079
Stella981 Stella981
2年前
OpenCV开发笔记(七十):红胖子带你傻瓜式编译VS2017x64版本的openCV4
若该文为原创文章,未经允许不得转载,经允许后转载请注明原文地址本文章博客地址:https://blog.csdn.net/qq21497936/article/details/107837715(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fqq214
Stella981 Stella981
2年前
Linux下安装rabbitmq
原文地址,转载请注明出处: http://blog.csdn.net/qq\_34021712/article/details/72567786(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fblog.csdn.net%2Fqq_34021712%2Farticle%2Fdetails%
Stella981 Stella981
2年前
DeepLearning (三) 预处理:主成分分析与白化
【原创】Liu\_LongPo(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fblog.csdn.net%2Fllp1992) 转载请注明出处 【CSDN】http://blog.csdn.net/llp1992(https://www.oschina.net/action/G
Stella981 Stella981
2年前
Android gradle plugin和 Gradle版本关系-Gradle version 1.10 is required. Current version is 2.0
转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51087827(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fblog.csdn.net%2Fxx326664162%2Farticle%2Fdetails%2F51087
Stella981 Stella981
2年前
Eclipse背景颜色修改
本文属转载文章,原出处请查看下方原文:http://blog.csdn.net/songxingfeng/article/details/7790582(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fblog.csdn.net%2Fsongxingfeng%2Farticle%2F
Wesley13 Wesley13
2年前
Android 框架炼成 教你如何写组件间通信框架EventBus
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41096639(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fblog.csdn.net%2Flmj623565791%2Farticle%2Fdetails%2F41