• 今日总结


    package me.wcy.music.activity;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AlertDialog;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ListView;
    
    import me.wcy.music.R;
    import me.wcy.music.adapter.OnMoreClickListener;
    import me.wcy.music.adapter.PlaylistAdapter;
    import me.wcy.music.model.Music;
    import me.wcy.music.service.AudioPlayer;
    import me.wcy.music.service.OnPlayerEventListener;
    import me.wcy.music.utils.binding.Bind;
    
    /**
     * 播放列表
     */
    public class PlaylistActivity extends BaseActivity implements AdapterView.OnItemClickListener, OnMoreClickListener, OnPlayerEventListener {
        @Bind(R.id.lv_playlist)
        private ListView lvPlaylist;
    
        private PlaylistAdapter adapter;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_playlist);
        }
    
        @Override
        protected void onServiceBound() {
    
            adapter.setIsPlaylist(true);
            adapter.setOnMoreClickListener(this);
            lvPlaylist.setAdapter(adapter);
            lvPlaylist.setOnItemClickListener(this);
            AudioPlayer.get().addOnPlayEventListener(this);
        }
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            AudioPlayer.get().play(position);
        }
    
        @Override
        public void onMoreClick(int position) {
            String[] items = new String[]{"移除"};
            Music music = AudioPlayer.get().getMusicList().get(position);
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle(music.getTitle());
            dialog.setItems(items, (dialog1, which) -> {
                AudioPlayer.get().delete(position);
                adapter.notifyDataSetChanged();
            });
            dialog.show();
        }
    
        @Override
        public void onChange(Music music) {
            adapter.notifyDataSetChanged();
        }
    
        @Override
        public void onPlayerStart() {
        }
    
        @Override
        public void onPlayerPause() {
        }
    
        @Override
        public void onPublish(int progress) {
        }
    
        @Override
        public void onBufferingUpdate(int percent) {
        }
    
        @Override
        protected void onDestroy() {
            AudioPlayer.get().removeOnPlayEventListener(this);
            super.onDestroy();
        }
    }
    package me.wcy.music.activity;
    
    import android.os.Bundle;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.widget.SearchView;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.TextView;
    
    import java.io.File;
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.List;
    
    import me.wcy.music.R;
    import me.wcy.music.adapter.OnMoreClickListener;
    import me.wcy.music.adapter.SearchMusicAdapter;
    import me.wcy.music.enums.LoadStateEnum;
    import me.wcy.music.executor.DownloadSearchedMusic;
    import me.wcy.music.executor.PlaySearchedMusic;
    import me.wcy.music.executor.ShareOnlineMusic;
    import me.wcy.music.http.HttpCallback;
    import me.wcy.music.http.HttpClient;
    import me.wcy.music.model.Music;
    import me.wcy.music.model.SearchMusic;
    import me.wcy.music.service.AudioPlayer;
    import me.wcy.music.utils.FileUtils;
    import me.wcy.music.utils.ToastUtils;
    import me.wcy.music.utils.ViewUtils;
    import me.wcy.music.utils.binding.Bind;
    
    public class SearchMusicActivity extends BaseActivity implements SearchView.OnQueryTextListener
            , AdapterView.OnItemClickListener, OnMoreClickListener {
        @Bind(R.id.lv_search_music_list)
        private ListView lvSearchMusic;
        @Bind(R.id.ll_loading)
        private LinearLayout llLoading;
        @Bind(R.id.ll_load_fail)
        private LinearLayout llLoadFail;
        private List<SearchMusic.Song> searchMusicList = new ArrayList<>();
        private SearchMusicAdapter mAdapter = new SearchMusicAdapter(searchMusicList);
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_search_music);
        }
    
        @Override
        protected void onServiceBound() {
            lvSearchMusic.setAdapter(mAdapter);
            TextView tvLoadFail = llLoadFail.findViewById(R.id.tv_load_fail_text);
            tvLoadFail.setText(R.string.search_empty);
    
            lvSearchMusic.setOnItemClickListener(this);
            mAdapter.setOnMoreClickListener(this);
        }
    
        @Override
        protected int getDarkTheme() {
            return R.style.AppThemeDark_Search;
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_search_music, menu);
            SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
            searchView.setMaxWidth(Integer.MAX_VALUE);
            searchView.onActionViewExpanded();
            searchView.setQueryHint(getString(R.string.search_tips));
            searchView.setOnQueryTextListener(this);
            searchView.setSubmitButtonEnabled(true);
            try {
                Field field = searchView.getClass().getDeclaredField("mGoButton");
                field.setAccessible(true);
                ImageView mGoButton = (ImageView) field.get(searchView);
                mGoButton.setImageResource(R.drawable.ic_menu_search);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return super.onCreateOptionsMenu(menu);
        }
    
        @Override
        public boolean onQueryTextSubmit(String query) {
            ViewUtils.changeViewState(lvSearchMusic, llLoading, llLoadFail, LoadStateEnum.LOADING);
            searchMusic(query);
            return false;
        }
    
        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    
        private void searchMusic(String keyword) {
            HttpClient.searchMusic(keyword, new HttpCallback<SearchMusic>() {
                @Override
                public void onSuccess(SearchMusic response) {
                    if (response == null || response.getSong() == null) {
                        ViewUtils.changeViewState(lvSearchMusic, llLoading, llLoadFail, LoadStateEnum.LOAD_FAIL);
                        return;
                    }
                    ViewUtils.changeViewState(lvSearchMusic, llLoading, llLoadFail, LoadStateEnum.LOAD_SUCCESS);
                    searchMusicList.clear();
                    searchMusicList.addAll(response.getSong());
                    mAdapter.notifyDataSetChanged();
                    lvSearchMusic.requestFocus();
                    handler.post(() -> lvSearchMusic.setSelection(0));
                }
    
                @Override
                public void onFail(Exception e) {
                    ViewUtils.changeViewState(lvSearchMusic, llLoading, llLoadFail, LoadStateEnum.LOAD_FAIL);
                }
            });
        }
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            new PlaySearchedMusic(this, searchMusicList.get(position)) {
                @Override
                public void onPrepare() {
                    showProgress();
                }
    
                @Override
                public void onExecuteSuccess(Music music) {
                    cancelProgress();
                    AudioPlayer.get().addAndPlay(music);
                    ToastUtils.show("已添加到播放列表");
                }
    
                @Override
                public void onExecuteFail(Exception e) {
                    cancelProgress();
                    ToastUtils.show(R.string.unable_to_play);
                }
            }.execute();
        }
    
        @Override
        public void onMoreClick(int position) {
            final SearchMusic.Song song = searchMusicList.get(position);
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle(song.getSongname());
            String path = FileUtils.getMusicDir() + FileUtils.getMp3FileName(song.getArtistname(), song.getSongname());
            File file = new File(path);
            int itemsId = file.exists() ? R.array.search_music_dialog_no_download : R.array.search_music_dialog;
            dialog.setItems(itemsId, (dialog1, which) -> {
                switch (which) {
                    case 0:// 分享
                        share(song);
                        break;
                    case 1:// 下载
                        download(song);
                        break;
                }
            });
            dialog.show();
        }
    
        private void share(SearchMusic.Song song) {
            new ShareOnlineMusic(this, song.getSongname(), song.getSongid()) {
                @Override
                public void onPrepare() {
                    showProgress();
                }
    
                @Override
                public void onExecuteSuccess(Void aVoid) {
                    cancelProgress();
                }
    
                @Override
                public void onExecuteFail(Exception e) {
                    cancelProgress();
                }
            }.execute();
        }
    
        private void download(final SearchMusic.Song song) {
            new DownloadSearchedMusic(this, song) {
                @Override
                public void onPrepare() {
                    showProgress();
                }
    
                @Override
                public void onExecuteSuccess(Void aVoid) {
                    cancelProgress();
                    ToastUtils.show(getString(R.string.now_download, song.getSongname()));
                }
    
                @Override
                public void onExecuteFail(Exception e) {
                    cancelProgress();
                    ToastUtils.show(R.string.unable_to_download);
                }
            }.execute();
        }
    }
  • 相关阅读:
    ANDROID STUDIO系列教程一--下载与安装
    Linux发邮件之mail命令
    Linux/CentOS关闭图形界面(X-window)和启用图形界面命令
    SVN四部曲之SVN设置详解深入
    构造函数 (C++)
    C++的构造函数和析构函数
    当你输入一个网址的时候,实际会发生什么?
    C++ 风格与技术 FAQ(中文版)
    二分查找算法(递归与非递归两种方式)
    c++模板
  • 原文地址:https://www.cnblogs.com/feng747/p/14910513.html
Copyright © 2020-2023  润新知