Search in sources :

Example 11 with SongInfo

use of com.lzx.musiclibrary.aidl.model.SongInfo in project NiceMusic by lizixian18.

the class SearchPresenter method searchMusic.

@Override
public void searchMusic(String keyword) {
    Disposable disposable = RetrofitHelper.getMusicApi().searchMusic(keyword).map(responseBody -> {
        JSONObject jsonObject = new JSONObject(responseBody.string());
        JSONArray array = jsonObject.getJSONObject("showapi_res_body").getJSONObject("pagebean").getJSONArray("contentlist");
        List<SongInfo> list = new ArrayList<>();
        for (int i = 0; i < array.length(); i++) {
            JSONObject object = array.getJSONObject(i);
            SongInfo info = new Gson().fromJson(object.toString(), SongInfo.class);
            list.add(info);
        }
        return list;
    }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(infoList -> mView.searchSuccess(infoList), throwable -> {
        LogUtil.i("-->" + throwable.getMessage());
    });
    addSubscribe(disposable);
}
Also used : Disposable(io.reactivex.disposables.Disposable) SongInfo(com.lzx.musiclibrary.aidl.model.SongInfo) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) JSONObject(org.json.JSONObject) LogUtil(com.lzx.musiclibrary.utils.LogUtil) Gson(com.google.gson.Gson) BasePresenter(com.lzx.nicemusic.base.mvp.factory.BasePresenter) Schedulers(io.reactivex.schedulers.Schedulers) RetrofitHelper(com.lzx.nicemusic.network.RetrofitHelper) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) List(java.util.List) ArrayList(java.util.ArrayList) SongInfo(com.lzx.musiclibrary.aidl.model.SongInfo)

Example 12 with SongInfo

use of com.lzx.musiclibrary.aidl.model.SongInfo in project NiceMusic by lizixian18.

the class PlaybackManager method switchToPlayback.

public void switchToPlayback(Playback playback, boolean resumePlaying) {
    if (playback == null) {
        throw new IllegalArgumentException("Playback cannot be null");
    }
    // Suspends current state.
    int oldState = mPlayback.getState();
    long pos = mPlayback.getCurrentStreamPosition();
    String currentMediaId = mPlayback.getCurrentMediaId();
    mPlayback.stop(false);
    playback.setCallback(this);
    playback.setCurrentMediaId(currentMediaId);
    playback.seekTo(pos < 0 ? 0 : pos);
    playback.start();
    // Swaps instance.
    mPlayback = playback;
    switch(oldState) {
        case PlaybackStateCompat.STATE_BUFFERING:
        case PlaybackStateCompat.STATE_CONNECTING:
        case PlaybackStateCompat.STATE_PAUSED:
            mPlayback.pause();
            break;
        case PlaybackStateCompat.STATE_PLAYING:
            SongInfo currentMusic = mQueueManager.getCurrentMusic();
            if (resumePlaying && currentMusic != null) {
                mPlayback.play(currentMusic);
            } else if (!resumePlaying) {
                mPlayback.pause();
            } else {
                mPlayback.stop(true);
            }
            break;
        case PlaybackStateCompat.STATE_NONE:
            break;
        default:
            break;
    }
}
Also used : SongInfo(com.lzx.musiclibrary.aidl.model.SongInfo)

Example 13 with SongInfo

use of com.lzx.musiclibrary.aidl.model.SongInfo in project NiceMusic by lizixian18.

the class PlaybackManager method handlePlayRequest.

/**
 * 播放
 */
public void handlePlayRequest() {
    SongInfo currentMusic = mQueueManager.getCurrentMusic();
    if (currentMusic != null) {
        String mediaId = currentMusic.getSongId();
        boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
        if (mediaHasChanged) {
            mCurrentMediaId = mediaId;
            notifyPlaybackSwitch(currentMusic);
        }
        // 播放
        mPlayback.play(currentMusic);
        // 更新媒体信息
        mQueueManager.updateMetadata();
        updatePlaybackState(null);
    }
}
Also used : SongInfo(com.lzx.musiclibrary.aidl.model.SongInfo)

Example 14 with SongInfo

use of com.lzx.musiclibrary.aidl.model.SongInfo in project MusicLibrary by lizixian18.

the class PlaybackManager method handlePlayRequest.

/**
 * 播放
 */
public void handlePlayRequest() {
    SongInfo currentMusic = mQueueManager.getCurrentMusic();
    if (currentMusic != null) {
        String mediaId = currentMusic.getSongId();
        boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
        if (mediaHasChanged) {
            mCurrentMediaId = mediaId;
            notifyPlaybackSwitch(currentMusic);
        }
        // 播放
        mPlayback.play(currentMusic);
        // 更新媒体信息
        mQueueManager.updateMetadata();
        updatePlaybackState(null);
    }
}
Also used : SongInfo(com.lzx.musiclibrary.aidl.model.SongInfo)

Example 15 with SongInfo

use of com.lzx.musiclibrary.aidl.model.SongInfo in project MusicLibrary by lizixian18.

the class PlaybackManager method updatePlaybackState.

public void updatePlaybackState(String error) {
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null && mPlayback.isConnected()) {
        position = mPlayback.getCurrentStreamPosition();
    }
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder().setActions(getAvailableActions());
    // 获取播放状态
    // int state = mPlayback.getState();
    int state = mPlayback.getState();
    // 如果是播放失败
    if (error != null) {
        // 设置错误信息
        stateBuilder.setErrorMessage(error);
        state = State.STATE_ERROR;
        if (mServiceCallback != null) {
            mServiceCallback.onPlaybackError(error);
        }
    }
    // 设置播放状态
    stateBuilder.setState(state == State.STATE_PLAYING ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED, position, 1.0f, SystemClock.elapsedRealtime());
    // Set the activeQueueItemId if the current index is valid.
    SongInfo currentMusic = mQueueManager.getCurrentMusic();
    if (currentMusic != null) {
        stateBuilder.setActiveQueueItemId(currentMusic.getTrackNumber());
    }
    if (mServiceCallback != null) {
        // 回调状态更新
        mServiceCallback.onPlaybackStateUpdated(state, stateBuilder.build());
        // 播放/暂停状态就通知通知栏更新
        if (state == State.STATE_PLAYING || state == State.STATE_PAUSED) {
            mServiceCallback.onNotificationRequired();
        }
    }
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) SongInfo(com.lzx.musiclibrary.aidl.model.SongInfo)

Aggregations

SongInfo (com.lzx.musiclibrary.aidl.model.SongInfo)31 ArrayList (java.util.ArrayList)9 AlbumInfo (com.lzx.musiclibrary.aidl.model.AlbumInfo)6 List (java.util.List)5 JSONObject (org.json.JSONObject)5 Uri (android.net.Uri)4 RetrofitHelper (com.lzx.nicemusic.network.RetrofitHelper)4 JSONArray (org.json.JSONArray)4 Cursor (android.database.Cursor)3 Gson (com.google.gson.Gson)3 LogUtil (com.lzx.musiclibrary.utils.LogUtil)3 BasePresenter (com.lzx.nicemusic.base.mvp.factory.BasePresenter)3 DataHelper (com.lzx.nicemusic.helper.DataHelper)3 Observable (io.reactivex.Observable)3 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)3 Disposable (io.reactivex.disposables.Disposable)3 Schedulers (io.reactivex.schedulers.Schedulers)3 Bitmap (android.graphics.Bitmap)2 AnimationDrawable (android.graphics.drawable.AnimationDrawable)2 Bundle (android.os.Bundle)2