Android开发之第一个小项目--我的MP3播放器之播放界面的实现(三)

下面是第二个界面播放界面MusicPlayActivity的界面,老规矩,代码解析卸载注释中

import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.verzqli.adapter.MyFrageStatePagerAdapter;
import com.verzqli.fragment.SongImageFragment;
import com.verzqli.fragment.SongListFragment;
import com.verzqli.fragment.SongLrcFragment;
import com.verzqli.model.LoveSong;
import com.verzqli.model.PlaySong;
import com.verzqli.model.Song;
import com.verzqli.service.MusicPlayService;
import com.verzqli.utils.AppControl;
import com.verzqli.utils.FastBlur;
import com.verzqli.utils.LogUtil;
import com.verzqli.utils.LrcView;
import com.verzqli.utils.MusicBitmap;
import com.verzqli.utils.MusicLoader;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.utils.UIHandler;
import cn.sharesdk.tencent.qq.QQ;

public class MusicPlayActivity extends FragmentActivity implements View.OnClickListener, SongLrcFragment.GetLrcListener, PlatformActionListener, Handler.Callback, SongListFragment.GetMyLoveSongListener {
    private Intent intent;
    private Button startButton;                     //开始/暂停按键
    private Button prevButton;                      //上一首按键
    private Button nextButton;                      //下一首按键
    private Button myLoveSong;
    private ImageView back;
    private ImageView imSongList;
    private ImageView imSongImage;
    private ImageView imSongLrc;
    private Button songRepeatState;                   //播放模式
    private LinearLayout linearLayout;
    private TextView musicPlaySongName;
    private TextView musicPlaySongMsg;
    private TextView songDurationText;
    private int repeatSate;
    private final int isCurrentRepeat = 1;            // 单曲循环
    private final int isAllRepeat = 2;                // 全部循环
    private final int isRandomRepeat = 3;             // 全部循环

    private int songListPosition;                     //选择音乐位置
    private List<Song> songList;                      //歌曲列
    private String url;                               //音乐地址
    private int currentTime;                          //当前播放时间
    private SeekBar songBar;                          //歌曲进度条
    private TextView songTimeText;                    //显示时间Textview
    private PlayerReceiver playerReceiver;            //广播
    private Boolean isPlaying;                        //是否在播放
    private Boolean isPause;                          //是否暂停
    private int isOncreate;                           //是否是重建了mainctivity之后进来的
    private int duration;                             //歌曲时间
    private ContentValues values;                     //存入数据库的歌曲状态
    private int stopProgress;                         //停止进度条滑动时的进度
    public static LrcView lrcView;                    //歌词界面
    private ViewPager viewPager;
    private List<Fragment> fragmentList;
    private MyFrageStatePagerAdapter fragmentAdapter;
    private SongListFragment songListFragment;
    private SongImageFragment songImageFragment;
    private SongLrcFragment songLrcFragment;
    private Boolean isDraw;
    private bgThread thread;
    private Boolean isMyLove;
    private Button share;                               //右上角的分享
    private String MSSG;
    private Boolean isBackView = false;                 //这个是判断直接按手机的home键返回时的事件
    private Boolean isBackViewTwo;
    public static final String SONG_STATE_UPDATE = "com.verzqli.SONG_STATE_UPDATE";  //更新动作
    public static final String SONG_STATE = "com.verzqli.SONG_STATE";           //控制动作
    public static final String MUSIC_CURRENT = "com.verzqli.action.MUSIC_CURRENT";  //音乐当前时间改变动作
    public static final String MUSIC_DURATION = "com.verzqli.action.MUSIC_DURATION";//音乐播放长度改变动作




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_music_play);
        //这个说一下,这个是控制小键盘的,打字时,小键盘不会把布局顶上来
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        fragmentList = new ArrayList<Fragment>();
        createView();

    }

    private void createView() {
        values = new ContentValues();
        viewPager = (ViewPager) findViewById(R.id.view_pager_music_play);
        /**
         *   这个是中间那个viewpager下面那3个切换界面时小圆点
         *   这个后面我才发现是蠢办法,viewpager里面有这个方法
         *   需要的自己去查下
         */
        songImageFragment = new SongImageFragment(this);
        songLrcFragment = new SongLrcFragment();
        songListFragment = new SongListFragment(this);
        /**
         *   这个线程是一个重点,他加载的是当前歌曲图片的高斯模糊,就像上面界面中看到的那样。
         *   然后来当背景图
         */
        thread = new bgThread();
        fragmentList.add(songListFragment);
        fragmentList.add(songImageFragment);
        fragmentList.add(songLrcFragment);
        /**
         * 进度条
         * 控制当前歌曲是否加入你喜爱的列表的按键
         * 播放/暂停按键
         * 下一首按键
         * 上一首按键
         * 分享按键
         * 返回按键(这个功能我忘记写了)
         * 歌曲循环状态按键
         */
        songBar = (SeekBar) findViewById(R.id.song_seek_bar);
        myLoveSong = (Button) findViewById(R.id.song_like);
        startButton = (Button) findViewById(R.id.start_button);
        nextButton = (Button) findViewById(R.id.next_button);
        prevButton = (Button) findViewById(R.id.prev_button);
        songRepeatState = (Button) findViewById(R.id.song_repeat_state);
        share = (Button) findViewById(R.id.share);
        back = (ImageView) findViewById(R.id.back);
        imSongList = (ImageView) findViewById(R.id.im_fragment_list);
        imSongImage = (ImageView) findViewById(R.id.im_fragment_image);
        imSongLrc = (ImageView) findViewById(R.id.im_fragment_lrc);
        /**
         * 显示歌曲时间
         * 最上方的那一个linear
         * 标题上显示的播放歌曲名字
         * 标题上显示的播放歌曲信息
         * 显示歌曲总时间
         */
        songTimeText = (TextView) findViewById(R.id.song_time_text);
        linearLayout = (LinearLayout) findViewById(R.id.lin_music_play);
        musicPlaySongName = (TextView) findViewById(R.id.music_play_song_name);
        musicPlaySongMsg = (TextView) findViewById(R.id.music_play_song_message);
        songDurationText = (TextView) findViewById(R.id.song_duration_text);
        back.setOnClickListener(this);
        songRepeatState.setOnClickListener(this);
        startButton.setOnClickListener(this);
        nextButton.setOnClickListener(this);
        prevButton.setOnClickListener(this);
        myLoveSong.setOnClickListener(this);
        share.setOnClickListener(this);
        songBar.setOnSeekBarChangeListener(new SeekBarChangeListener());
        isPlaying = true;
        songList = MusicLoader.getInstance().queryData();
        fragmentAdapter = new MyFrageStatePagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setCurrentItem(1);
        imSongImage.setBackgroundResource(R.drawable.oval_show);
        viewPager.setOnPageChangeListener(new onPageChangeListener());
        // 注册广播
        playerReceiver = new PlayerReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(SONG_STATE_UPDATE);
        filter.addAction(MUSIC_CURRENT);
        filter.addAction(MUSIC_DURATION);
        registerReceiver(playerReceiver, filter);


    }

    /**
     * 这下面一大段是和分享有关的,可以无视他
     * @param platform
     * @param action
     */
    @Override
    public void onCancel(Platform platform, int action) {
        // 取消
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 3;
        msg.arg2 = action;
        msg.obj = platform;
        UIHandler.sendMessage(msg, MusicPlayActivity.this);

    }

    @Override
    public void onComplete(Platform platform, int action, HashMap<String, Object> res) {
        // 成功
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 1;
        msg.arg2 = action;
        msg.obj = platform;
        Platform qzone = ShareSDK.getPlatform(this, QQ.NAME);
        UIHandler.sendMessage(msg, this);


    }

    @Override
    public void onError(Platform platform, int action, Throwable t) {
        // 失敗
        //打印错误信息,print the error msg
        t.printStackTrace();
        //错误监听,handle the error msg
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 2;
        msg.arg2 = action;
        msg.obj = t;
        UIHandler.sendMessage(msg, this);

    }


    private static final int MSG_ACTION_CCALLBACK = 2;

    public boolean handleMessage(Message msg) {
        switch (msg.arg1) {
            case 1: {
                // 成功
                Toast.makeText(MusicPlayActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
                //text.setText(nickname);
                System.out.println("分享回调成功------------");
            }
            break;
            case 2: {
                // 失败
                Toast.makeText(MusicPlayActivity.this, "分享失败", Toast.LENGTH_SHORT).show();
            }
            break;
            case 3: {
                // 取消
                Toast.makeText(MusicPlayActivity.this, "分享取消", Toast.LENGTH_SHORT).show();
            }
            break;
        }

        return false;

    }

    /**
     * 上面都是分享的可以无视他
     *  下面这个是滑动中间的viewpager时给小圆块的图片
     */
    private class onPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            imSongList.setBackgroundResource(R.drawable.oval_normal);
            imSongImage.setBackgroundResource(R.drawable.oval_normal);
            imSongLrc.setBackgroundResource(R.drawable.oval_normal);
            LogUtil.itSelf.Log("onPageSelected" + position);
            switch (position) {
                case 0:
                    imSongList.setBackgroundResource(R.drawable.oval_show);
                    break;
                case 1:
                    imSongImage.setBackgroundResource(R.drawable.oval_show);
                    break;
                case 2:
                    imSongLrc.setBackgroundResource(R.drawable.oval_show);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    /**
     * 按home键退出当前页面时
     * 触发onpause,isbackView设为true
     * 表示在后台运行
     */
    @Override
    protected void onPause() {
        super.onPause();
        isBackView = true;
        LogUtil.itSelf.Log("ONPAUSE");
    }

    @Override
    protected void onResume() {
        super.onResume();
        /**
         * 这个isBackViewTwo是当按home键退出主界面后,后台切换了一首歌
         * 当再次打开界面时,界面能随之变化,
         * 不过这个还是有一点小bug,只有当一首歌在list上你点击过两次时,才能实现更新
         * 其实这个用个广播就能解决,只是这个项目用了广播太多了,我想换个写法
         * 后面的都是对前面那个界面传过来数据的获取
         * isOncreate这个值是第一次打开这个程序时来调用的,
         * 毕竟我的效果是退出了主界面,音乐还在后台播放,当再次进入程序时u,这个时候就不算是第一次打开
         * ,所以我设了个值来判定这是第一次打开程序
         */
        isBackViewTwo = true;
        isDraw = true;
        intent = this.getIntent();
        MSSG = intent.getStringExtra("MSSG");
        if (isBackView) {
            MSSG = AppControl.MEDIA_PLAYING_SONG_IS_PLAYING;
            isBackViewTwo = false;
        }
        if (MSSG.equals(AppControl.MEDIA_PLAYING_SONG_AT_LIST)) {
            url = intent.getExtras().getString("url");
            currentTime = intent.getIntExtra("mainCurrentTime", 0);
            songListPosition = intent.getIntExtra("position", -1);
            repeatSate = intent.getIntExtra("repeatState", -1);
            isPlaying = intent.getBooleanExtra("isPlaying", true);
            duration = intent.getIntExtra("duration", 0);
            play();
        } else if (MSSG.equals(AppControl.MEDIA_PLAYING_SONG_IS_PLAYING)) {
            url = intent.getExtras().getString("url");
            currentTime = intent.getIntExtra("mainCurrentTime", 0);
            duration = (int) intent.getLongExtra("duration", -1);
            LogUtil.itSelf.Log("songListPositionssss1" + songListPosition+isBackViewTwo);
            if(isBackViewTwo){
                LogUtil.itSelf.Log("isBackViewTwo" + songListPosition);
                songListPosition = intent.getIntExtra("position", -1);
            }
            repeatSate = intent.getIntExtra("repeatState", -1);
            isPlaying = intent.getBooleanExtra("isPlaying", true);
            isOncreate = intent.getIntExtra("isOncreate", -1);
            songBar.setMax(duration);
            songBar.setProgress(currentTime);
            if (isOncreate == 0) {
                Intent intent = new Intent(this, MusicPlayService.class);
                intent.setAction("com.verzqli.media.play");
                LogUtil.itSelf.Log("isOncreate" + url + songListPosition + currentTime);
                intent.putExtra("url", url);
                intent.putExtra("position", songListPosition);
                intent.putExtra("progress", currentTime);
                intent.putExtra("MSG", AppControl.MEDIA_PLAYING_SONG_CHANGE_PROGRESS);
                isPlaying = true;
                startService(intent);
            }
            Intent intent2 = new Intent(AppControl.LRC_SONG_CONTINUE);
            intent2.putExtra("position", songListPosition);
            intent2.putExtra("url", songList.get(songListPosition).getData());
            intent2.putExtra("mainCurrentTime", currentTime);
            sendBroadcast(intent2);
        }
        //这是改变标题上的信息,可以不用看他
        changeTitleMsg();
        //家在背景模糊图片
        thread.run();
        //判断 当前这首歌是不是我喜爱的,是的话就让那个爱字变成红色
        judgeMyLove();
        //暂停/播放键的突变变化
        if (isPlaying) {
            startButton.setBackgroundResource(R.drawable.play_normal);
        } else {
            startButton.setBackgroundResource(R.drawable.play_pressed);
        }
        //歌曲循环状态的图标变化
        changePlayState();
        initView();

    }


    private void judgeMyLove() {
        List<LoveSong> loveSongList = DataSupport.where("songPosition = ?", songListPosition + "").find(LoveSong.class);
        if (loveSongList.size() == 0) {
            isMyLove = false;
            myLoveSong.setBackgroundResource(R.drawable.nolove);
        } else {
            isMyLove = true;
            myLoveSong.setBackgroundResource(R.drawable.mylove);
        }
    }

    //这下面一段是和背景模糊,你如果不用 就可以直接删掉,用一张你喜欢的图片替换即可,要用的话,后面的博客我再来讲
    private void applyBlur() {
        linearLayout.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                linearLayout.getViewTreeObserver().removeOnPreDrawListener(this);
                linearLayout.buildDrawingCache(true);
                linearLayout.buildDrawingCache();
                Bitmap bitmap = MusicBitmap.getArtwork(MusicPlayActivity.this, songList.get(songListPosition).getId(), songList.get(songListPosition).getAlbumId(), true, false);
                if (bitmap != null) {
                    Bitmap bmp = bitmap;
                    blur(bmp, linearLayout);
                } else {

                    Resources res = getResources();
                    Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.morenbg);
                    blur(bmp, linearLayout);
                }

                return true;

            }


        });
    }


    private void blur(Bitmap bkg, View view) {
        float scaleFactor = 20;
        float radius = 20;
        Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth() / scaleFactor),
                (int) (view.getMeasuredHeight() / scaleFactor), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(overlay);
        canvas.translate(-view.getLeft() / scaleFactor, -view.getTop() / scaleFactor);
        canvas.scale(1 / scaleFactor, 1 / scaleFactor);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        RectF rectF = new RectF(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());   //w和h分别是屏幕的宽和高,也就是你想让图片显示的宽和高
        canvas.drawBitmap(bkg, null, rectF, null);
        overlay = FastBlur.doBlur(overlay, (int) radius, true);
        linearLayout.setBackgroundDrawable(new BitmapDrawable(getResources(), overlay));


    }

    /**
     * 这是歌词显示界面
     * @param num
     */
    @Override
    public void LrcNum(String num) {
        Intent intent2 = new Intent(AppControl.LRC_DOWNLOAD);
        if (!num.equals("")) {
            intent2.putExtra("songLrdId", num);
            sendBroadcast(intent2);
        }
    }

    //背景模糊的线程
    private class bgThread implements Runnable {
        @Override
        public void run() {
            while (isDraw) {
                songImageFragment.setPosition(songListPosition);
                applyBlur();
                isDraw = false;
            }
        }
    }


    private void initView() {
        switch (repeatSate) {
            case isCurrentRepeat: // 单曲循环
                songRepeatState.setBackgroundResource(R.drawable.danqu);
                break;
            case isAllRepeat: // 全部循环
                songRepeatState.setBackgroundResource(R.drawable.all);
                break;
            case isRandomRepeat: // 无重复
                songRepeatState.setBackgroundResource(R.drawable.suiji);
                break;
        }
    }

    /**
     * 所有按键的事件全在这里
     * 开始/暂停
     * 前一首
     * 后一首
     * 播放模式
     * 收藏当前歌曲
     * 分享按键
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_button:
                if (isPlaying) {
                    Intent intent = new Intent(this, MusicPlayService.class);
                    intent.setAction("com.verzqli.media.play");
                    intent.putExtra("MSG", AppControl.MEDIA_PLAYING_SONG_PAUSE);
                    startService(intent);
                    startButton.setBackgroundResource(R.drawable.play_pressed);
                    isPlaying = false;
                    isPause = true;
                    changePlayState();
                    changeTitleMsg();


                } else {
                    Intent intent = new Intent(this, MusicPlayService.class);
                    intent.setAction("com.verzqli.media.play");
                    intent.putExtra("MSG", AppControl.MEDIA_PLAYING_SONG_CONTINUE);
                    startButton.setBackgroundResource(R.drawable.play_normal);
                    startService(intent);
                    isPlaying = true;
                    isPause = false;
                    changePlayState();
                    changeTitleMsg();

                    values.put("playing", isPlaying);
                    DataSupport.update(PlaySong.class, values, 1);

                }
                break;
            case R.id.song_repeat_state:
                if (repeatSate == isCurrentRepeat) {
                    intent = new Intent(SONG_STATE);
                    intent.putExtra("state", 2);
                    sendBroadcast(intent);
                    repeatSate = isAllRepeat;

                    songRepeatState.setBackgroundResource(R.drawable.all);
                } else if (repeatSate == isAllRepeat) {

                    intent = new Intent(SONG_STATE);
                    intent.putExtra("state", 3);
                    sendBroadcast(intent);
                    repeatSate = isRandomRepeat;

                    songRepeatState.setBackgroundResource(R.drawable.suiji);
                } else if (repeatSate == isRandomRepeat) {

                    intent = new Intent(SONG_STATE);
                    intent.putExtra("state", 1);
                    sendBroadcast(intent);
                    repeatSate = isCurrentRepeat;

                    songRepeatState.setBackgroundResource(R.drawable.danqu);
                }
                break;
            case R.id.next_button:
                nextSong();
                judgeMyLove();

                break;
            case R.id.prev_button:
                prevSong();
                judgeMyLove();
                break;
            case R.id.song_like:

                if (isMyLove) {
                    DataSupport.deleteAll(LoveSong.class, "songPosition = ?", songListPosition + "");
                    myLoveSong.setBackgroundResource(R.drawable.nolove);
                    isMyLove = false;
                } else {
                    isMyLove = true;
                    myLoveSong.setBackgroundResource(R.drawable.mylove);
                    Song song = new Song();
                    song = songList.get(songListPosition);
                    LoveSong loveSong = new LoveSong();
                    loveSong.setSongPosition(songListPosition);
                    loveSong.setAlbum(song.getAlbum());
                    loveSong.setArtist(song.getArtist());
                    loveSong.setDisplayName(song.getDisplayName());
                    loveSong.setDuration(song.getDuration());
                    loveSong.setTitle(song.getTitle());
                    loveSong.setData(song.getData());
                    loveSong.setLove(true);
                    loveSong.save();
                }
                songListFragment.refleshView();
                break;
            case R.id.share:
                Platform.ShareParams qq = new Platform.ShareParams();
                qq.setTitle("分享歌曲");
                qq.setTitleUrl("http://mob.com");
                qq.setText("给你们分享一首歌:" + songList.get(songListPosition).getTitle());
                qq.setImageUrl("http://p0.so.qhimg.com/dmt/73_100_/t01d66f2efb44823d25.jpg");
                qq.setMusicUrl("http://music.baidu.com/search?key=" + songList.get(songListPosition).getTitle());
                Platform qqq = ShareSDK.getPlatform(MusicPlayActivity.this, QQ.NAME);
                qqq.setPlatformActionListener(MusicPlayActivity.this);
                qqq.share(qq);
                break;
            case R.id.back:
                finish();
                break;

        }

    }

    /**
     * 上一首歌曲
     * 这里我们点击上一首歌曲时,要判断当前的播放模式,根据不同的播放模式来改变歌曲
     * 同时也要监听歌曲是否到顶了,然后切换到底部
     * 同时要更新标题信息,歌曲进度,歌曲总时间等信息
     * 然后把这些信息发送给service,让他处理来播放歌曲。
     */
    private void prevSong() {
        switch (repeatSate) {
            case isCurrentRepeat: // 单曲循环
                break;
            case isAllRepeat: // 全部循环
                songListPosition = songListPosition - 1;
                break;
            case isRandomRepeat: // 无重复
                songListPosition = getRandomSongPosition(songList.size() - 1);
                break;
        }

        if (songListPosition >= 0) {
            changeImage();

            isPlaying = true;
            startButton.setBackgroundResource(R.drawable.play_normal);
            changePlayState();
            Song song = songList.get(songListPosition);
            url = song.getData();
            Intent intent = new Intent(this, MusicPlayService.class);
            intent.setAction("com.verzqli.media.play");
            intent.putExtra("position", songListPosition);
            intent.putExtra("url", url);
            intent.putExtra("title", song.getTitle());
            intent.putExtra("MSG", AppControl.MEDIA_PLAYING_PREV_SONG);
            startService(intent);
        } else {
            songListPosition = songList.size() - 1;
            changeImage();

        }
    }

    /**
     * 下一首歌曲,这上面差不多,不做赘述
     */
    private void nextSong() {
        switch (repeatSate) {
            case isCurrentRepeat: // 单曲循环
                break;
            case isAllRepeat: // 全部循环
                songListPosition = songListPosition + 1;
                break;
            case isRandomRepeat: // 无重复
                songListPosition = getRandomSongPosition(songList.size() - 1);
                break;
        }

        if (songListPosition <= songList.size() - 1) {
            changeImage();
            isPlaying = true;
            startButton.setBackgroundResource(R.drawable.play_normal);
            changePlayState();
            Song song = songList.get(songListPosition);
            url = song.getData();
            Intent intent = new Intent(this, MusicPlayService.class);
            intent.setAction("com.verzqli.media.play");
            intent.putExtra("position", songListPosition);
            intent.putExtra("url", url);
            intent.putExtra("title", song.getTitle());
            intent.putExtra("MSG", AppControl.MEDIA_PLAYING_NEXT_SONG);
            startService(intent);
        } else {
            songListPosition = 0;
            changeImage();
        }
    }

    /**
     * 修改播放界面头部信息
     */
    private void changeTitleMsg() {
        LogUtil.itSelf.Log("changeTitleMsg" + songListPosition);
        musicPlaySongName.setText(songList.get(songListPosition).getTitle());
        musicPlaySongMsg.setText(songList.get(songListPosition).getArtist() + " | " + songList.get(songListPosition).getAlbum());
    }

    public void changeImage() {
        isDraw = true;
        thread.run();
        songImageFragment.setPosition(songListPosition);
        songImageFragment.setImage();
        // songDurationText.setText(MusicLoader.formatTime(songList.get(songListPosition).getDuration()));
    }


    /**
     * 当次页面的按键变化时发广告同步在主页面上
     */
    private void changePlayState() {
        values.put("playing", isPlaying);
        DataSupport.update(PlaySong.class, values, 1);
        Intent intent = new Intent(MainActivity.SONG_STATE_PRESS);
        LogUtil.itSelf.Log("changePlayState" + isPlaying);
        intent.putExtra("isPlaying", isPlaying);
        sendBroadcast(intent);

    }

    private void play() {
        Intent bindService = new Intent(this, MusicPlayService.class);
        bindService.setAction("com.verzqli.media.play");
        bindService.putExtra("position", songListPosition);
        bindService.putExtra("url", url);
        startService(bindService);
    }

    //更新进度条
    public class SeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {


        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (fromUser) {
                stopProgress = progress;

            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            setSongToProgress(stopProgress);
        }
    }

    /**
     * 设置拖动进度条 改变音乐进度
     *
     * @param progress
     */
    private void setSongToProgress(int progress) {

        Intent intent = new Intent(this, MusicPlayService.class);
        intent.setAction("com.verzqli.media.play");
        intent.putExtra("url", url);
        intent.putExtra("position", songListPosition);
        intent.putExtra("progress", progress);
        intent.putExtra("MSG", AppControl.MEDIA_PLAYING_SONG_CHANGE_PROGRESS);
        startService(intent);

    }

    /**
     * 获得随机播放的
     *
     * @param all 歌曲总数
     * @return
     */
    private int getRandomSongPosition(int all) {

        return (int) (Math.random() * all);
    }

    /**
     * 用来接收从service传回来的广播的内部类
     *
     * @author wwj
     */
    public class PlayerReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(MUSIC_CURRENT)) {
                currentTime = intent.getIntExtra("currentTime", -1);

                songTimeText.setText(MusicLoader.formatTime(currentTime));

                songBar.setProgress(currentTime);
            } else if (action.equals(MUSIC_DURATION)) {
                duration = intent.getIntExtra("duration", -1);

                songBar.setMax(duration);
                songDurationText.setText(MusicLoader.formatTime(duration));
            } else if (action.equals(SONG_STATE_UPDATE)) {

                //  获取Intent中的current消息,current代表当前正在播放的歌曲
                songListPosition = intent.getIntExtra("currentSongPosition", -1);

                judgeMyLove();
                url = songList.get(songListPosition).getData();
                LogUtil.itSelf.Log(songListPosition + "   " + url);
                if (songListPosition >= 0) {
                    changeTitleMsg();
                    changeImage();
                }
                if (songListPosition == 0) {
                    changeTitleMsg();
                    songTimeText.setText(MusicLoader.formatTime(songList.get(songListPosition).getDuration()));
                    changeImage();

                    isPause = true;
                }

            }

        }
    }
    //设置我喜爱的歌曲(和收藏一个意思,就是设置你喜爱的歌单)
    @Override
    public void myLoveSong(LoveSong loveSong) {
        url = loveSong.getData();
        currentTime = 0;
        songListPosition = loveSong.getSongPosition();
        isMyLove = loveSong.getLove();
        isPlaying = true;
        if (!isMyLove) {

            myLoveSong.setBackgroundResource(R.drawable.nolove);
        } else {

            myLoveSong.setBackgroundResource(R.drawable.mylove);
        }
        duration = (int) loveSong.getDuration();
        play();

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtil.itSelf.Log("onDestroy");
        isBackView = false;
        unregisterReceiver(playerReceiver);
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值