Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

寻求一下帮助。 #45

Closed
Janzjoy opened this issue Mar 29, 2023 · 9 comments
Closed

寻求一下帮助。 #45

Janzjoy opened this issue Mar 29, 2023 · 9 comments

Comments

@Janzjoy
Copy link
Contributor

Janzjoy commented Mar 29, 2023

我用ijkplayer写一个自定义播放器,但是存在一些问题:一开始播放需要点击两次才能正常播放,切换音质后也存在该问题。

代码
`public class IjkMusicPlayer extends AbstractMusicPlayer {

private String TAG = "JkPlayer";
private IjkMediaPlayer exoMediaPlayer;

private final Context mContext;
private final Uri mUri;

@Nullable
private OnCompletionListener mCompletionListener;

@Nullable
private OnStalledListener mStalledListener;

@Nullable
private OnErrorListener mErrorListener;
@Nullable
private OnRepeatListener mRepeatListener;

private boolean mStalled;
private boolean mInvalid;
private boolean mLooping;

private boolean mPreparing;
private boolean mPlayerReady;

public IjkMusicPlayer(Context context, Uri uri) {

    mContext = context;
    mUri = uri;

    exoMediaPlayer = new IjkMediaPlayer();
    mInvalid = false;
    exoMediaPlayer.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        exoMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "soundtouch", 0);
    }else{
        exoMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "soundtouch", 1);
    }
    exoMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT,"analyzemaxduration",100L);
    exoMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0);
    exoMediaPlayer.setOption( IjkMediaPlayer.OPT_CATEGORY_FORMAT, "dns_cache_clear", 1);

    exoMediaPlayer.setOnErrorListener(new IMediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(IMediaPlayer iMediaPlayer, int i, int i1) {
            Log.e(TAG, "MediaPlayer Error[what: " + i + ", extra: " + i1 + "]");

            setInvalid();

            if (mErrorListener != null) {
                mErrorListener.onError(IjkMusicPlayer.this, toErrorCode(i1));
            }
            return true;
        }
    });

    exoMediaPlayer.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
        @Override
        public boolean onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
            switch (i) {
                case IMediaPlayer.MEDIA_INFO_BUFFERING_START:
                    setStalled(true);
                    return true;
                case IMediaPlayer.MEDIA_INFO_BUFFERING_END:
                    setStalled(false);
                    return true;
            }
            return false;
        }
    });

    exoMediaPlayer.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(IMediaPlayer iMediaPlayer) {
            if (mLooping) {
                iMediaPlayer.start();
                notifyOnRepeat();
                return;
            }

            notifyOnComplete();
        }
    });
}

private int toErrorCode(int extra) {
    switch (extra) {
        case IMediaPlayer.MEDIA_ERROR_IO:    
        case IMediaPlayer.MEDIA_ERROR_TIMED_OUT:
            return ErrorCode.DATA_LOAD_FAILED;
        case IMediaPlayer.MEDIA_ERROR_MALFORMED:  
        case IMediaPlayer.MEDIA_ERROR_UNSUPPORTED:
            return ErrorCode.PLAYER_ERROR;
        default:
            return ErrorCode.UNKNOWN_ERROR;
    }
}

private void notifyOnRepeat() {
    if (mRepeatListener != null) {
        mRepeatListener.onRepeat(this);
    }
}

private void notifyOnComplete() {
    if (mCompletionListener != null) {
        mCompletionListener.onCompletion(this);
    }
}

private void setStalled(boolean stalled) {
    mStalled = stalled;
    if (mStalledListener != null) {
        mStalledListener.onStalled(mStalled);
    }
}

private synchronized void setInvalid() {
    mInvalid = true;
}

@Override
public void startEx() {
    Log.e(TAG,"播放");
    exoMediaPlayer.start();
}

@Override
public void pauseEx() {
    exoMediaPlayer.pause();
}

@Override
public void stopEx() {
    exoMediaPlayer.stop();
}

@Override
public void releaseEx() {
    setInvalid();
    if (exoMediaPlayer != null) {
        exoMediaPlayer.reset();
        exoMediaPlayer.release();
        exoMediaPlayer = null;
    }
}

@Override
public void prepare() throws Exception {
    if (isInvalid()) {
        return;
    }
    try {
        exoMediaPlayer.setDataSource(mContext, mUri);
        exoMediaPlayer.prepareAsync();
    } catch (Exception e) {
        setInvalid();
        throw e;
    }
}

@Override
public void setLooping(boolean looping) {
    mLooping = looping;
    exoMediaPlayer.setLooping(looping);
}

@Override
public boolean isLooping() {
    return exoMediaPlayer.isLooping();
}

@Override
public boolean isStalled() {
    return mStalled;
}

@Override
public boolean isPlaying() {
    return exoMediaPlayer.isPlaying();
}

@Override
public int getDuration() {
    return (int) exoMediaPlayer.getDuration();
}

@Override
public int getProgress() {
    return (int) exoMediaPlayer.getCurrentPosition();
}

@Override
public void seekTo(int pos) {
    exoMediaPlayer.seekTo(pos);
}

@Override
public void setVolume(float leftVolume, float rightVolume) {
    exoMediaPlayer.setVolume(leftVolume, rightVolume);
}

@Override
public void setSpeed(float speed) {
    exoMediaPlayer.setSpeed(speed);
}

@Override
public boolean isInvalid() {
    return mInvalid;
}

@Override
public int getAudioSessionId() {
    return exoMediaPlayer.getAudioSessionId();
}

@Override
public void setOnPreparedListener(@Nullable OnPreparedListener listener) {
    if (listener == null) {
        exoMediaPlayer.setOnPreparedListener(null);
        return;
    }

    exoMediaPlayer.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(IMediaPlayer iMediaPlayer) {
            listener.onPrepared(IjkMusicPlayer.this);
        }
    });
}

@Override
public void setOnCompletionListener(@Nullable OnCompletionListener listener) {
    mCompletionListener = listener;
}

@Override
public void setOnRepeatListener(@Nullable OnRepeatListener listener) {
    mRepeatListener = listener;
}

@Override
public void setOnSeekCompleteListener(@Nullable OnSeekCompleteListener listener) {
    if (listener == null) {
        exoMediaPlayer.setOnSeekCompleteListener(null);
        return;
    }

    exoMediaPlayer.setOnSeekCompleteListener(new IMediaPlayer.OnSeekCompleteListener() {
        @Override
        public void onSeekComplete(IMediaPlayer iMediaPlayer) {
            listener.onSeekComplete(IjkMusicPlayer.this);
        }
    });
}

@Override
public void setOnStalledListener(@Nullable OnStalledListener listener) {
    mStalledListener = listener;
}

@Override
public void setOnBufferingUpdateListener(@Nullable OnBufferingUpdateListener listener) {
    if (listener == null) {
        exoMediaPlayer.setOnBufferingUpdateListener(null);
        return;
    }

    exoMediaPlayer.setOnBufferingUpdateListener(new IMediaPlayer.OnBufferingUpdateListener() {
        @Override
        public void onBufferingUpdate(IMediaPlayer iMediaPlayer, int i) {
            listener.onBufferingUpdate(IjkMusicPlayer.this, i, true);
        }
    });
}

@Override
public void setOnErrorListener(@Nullable OnErrorListener listener) {
    mErrorListener = listener;
}

}`

@jrfeng
Copy link
Owner

jrfeng commented Mar 29, 2023

将代码放到你 fork 的仓库的 debug 模块中吧,不然这样我也没法调试 😂 😂 😂

@Janzjoy
Copy link
Contributor Author

Janzjoy commented Mar 29, 2023

将代码放到你 fork 的仓库的 debug 模块中吧,不然这样我也没法调试😂 😂 😂

不知道怎么弄😵‍💫

@jrfeng
Copy link
Owner

jrfeng commented Mar 29, 2023

你将代码提交到你 fork 的 snow 仓库里面就行,这样我克隆下来就可以调试了

@Janzjoy
Copy link
Contributor Author

Janzjoy commented Mar 29, 2023

你将代码提交到你 fork 的 snow 仓库里面就行,这样我克隆下来就可以调试了

好了,已提交到debug模块中了

@jrfeng
Copy link
Owner

jrfeng commented Mar 29, 2023

jcenter 已经挂了,ijk 的依赖下载不了 😂

@Janzjoy
Copy link
Contributor Author

Janzjoy commented Mar 29, 2023

jcenter 已经挂了,ijk 的依赖下载不了 😂

哈?

@Janzjoy
Copy link
Contributor Author

Janzjoy commented Mar 29, 2023

jcenter 已经挂了,ijk 的依赖下载不了 😂

exoplayer和MediaMusicPlayer在播放部分flac文件会报错,有部分解决吗

@jrfeng
Copy link
Owner

jrfeng commented Mar 29, 2023

ijkplayer 都几年不维护了,不推荐使用,优先使用 exoplayer 吧。

抱歉,flac 这个问题我帮不了你😂 ,很久没接触 exoplayer 了,建议查一下 exoplayer 的文档。

@Janzjoy
Copy link
Contributor Author

Janzjoy commented Mar 29, 2023

ijkplayer 都几年不维护了,不推荐使用,优先使用 exoplayer 吧。

抱歉,flac 这个问题我帮不了你😂 ,很久没接触 exoplayer 了,建议查一下 exoplayer 的文档。

好吧,谢谢

@Janzjoy Janzjoy closed this as completed Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants