android 有声小说播放器(可以离线在线播放)

使用service实现播放器

播放器:bilibili IJKplayer 的 exoplayer 框架


播放器



播放器service代码:

public class PlayerService extends Service {
    private IjkExoMediaPlayer player;
    public static final int PLAYING = 1;//正在播放
    public static final int PLAYSTOP = 2;//停止
    public static final int PLAYPAUSE = 3;// 暂停
    public int playerState = PLAYSTOP;
    public ArrayList<ChapterItem> playerList;//播放资源列表
    public int position;
    public int currentTime;
    public int seekTime = 0;
    public int update = 0;
    public int intenetState = NOINTENET;

    public static final int NOINTENET = 0;//网络状态
    public static final int WIFY = 1;
    public static final int PHONE = 2;

    public int buffering = 0;//缓冲进度
    public int clock = 0; // 定时
    public boolean isPauseForCall = false; //是否因为来电导致播放暂停
    public int allTims=0; //播放总时长

    PhoneStateListener phoneStateListener = new PhoneStateListener() {// 来电监听  来电暂停 结束继续播放
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                int ringvolume = audioManager
                        .getStreamVolume(AudioManager.STREAM_RING);
                if (ringvolume > 0) {
                    if (player != null && player.isPlaying()) {
                        isPauseForCall = true;
                        pause();
                    }
                }
            } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                if (player != null && player.isPlaying()) {
                    isPauseForCall = true;
                    pause();
                }
            } else if (state == TelephonyManager.CALL_STATE_IDLE) {
                if (isPauseForCall) { //是否是电话导致播放器暂停
                    reStart();
                    isPauseForCall = false;
                }
            }
        }
    };

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Common.GET_OK:
                    setData(msg.obj.toString());
                    break;
                case Common.INIT_OK:
                    handler.sendEmptyMessageDelayed(Common.INIT_OK, 1000);
                    if (player != null && player.getCurrentPosition() > 0) {//广播发送 缓冲 和播放时间
                        buffering=player.getBufferedPercentage();
                        currentTime = (int) player.getCurrentPosition();
                        Intent intent = new Intent();
                        intent.setAction(Common.PlayerTime);
                        intent.putExtra("currentTime", currentTime);
                        intent.putExtra("buffering", buffering);
                        sendBroadcast(intent);
                    }

                  
                    if (update == 0) {//播放记录
                        update++;
                        if (sharedPreferences != null) {// 用户最后一次播放记录
                     

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值