当前位置: 首页 > 知识库问答 >
问题:

媒体播放器播放音频和视频同步Android

田焕
2023-03-14

大家好,我正在尝试使用两个独立的媒体播放器实例播放音频和视频文件。当我从一开始播放它时,它工作得很好。但当我寻找时,我可以看到音频和视频的延迟

这是我寻找音频和视频的代码

public class FullscreenVideoView extends RelativeLayout implements SurfaceHolder.Callback, OnPreparedListener, OnErrorListener, OnSeekCompleteListener, OnCompletionListener, OnInfoListener, OnVideoSizeChangedListener {


private final static String TAG = "FullscreenVideoView";

protected Context context;
protected Activity activity; // Used when orientation changes is not static

protected MediaPlayer mediaPlayer;
protected MediaPlayer audioPlayer;
protected SurfaceHolder surfaceHolder;
protected SurfaceView surfaceView;
protected boolean videoIsReady, surfaceIsReady;
protected boolean detachedByFullscreen;
protected State currentState;
protected State audioState;

protected State lastState; // Tells onSeekCompletion what to do

protected View loadingView;

protected ViewGroup parentView; // Controls fullscreen container
protected ViewGroup.LayoutParams currentLayoutParams;

protected boolean fullscreen;
protected boolean shouldAutoplay;
protected boolean shouldPlayAudio;
protected int initialConfigOrientation;
protected int initialMovieWidth, initialMovieHeight;

protected OnErrorListener errorListener;
protected OnPreparedListener preparedListener;
protected AudioPrepareListener mAudioPrepareListener;
protected OnSeekCompleteListener seekCompleteListener;
protected OnCompletionListener completionListener;
protected OnInfoListener infoListener;

public enum State {
    IDLE,
    INITIALIZED,
    PREPARED,
    PREPARING,
    STARTED,
    STOPPED,
    PAUSED,
    PLAYBACKCOMPLETED,
    ERROR,
    END
}

public FullscreenVideoView(Context context) {
    super(context);
    this.context = context;

    init();
}

public FullscreenVideoView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;

    init();
}

public FullscreenVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;

    init();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    resize();
}

@Override
public Parcelable onSaveInstanceState() {
    Log.d(TAG, "onSaveInstanceState");
    return super.onSaveInstanceState();
}

@Override
public void onRestoreInstanceState(Parcelable state) {
    Log.d(TAG, "onRestoreInstanceState");
    super.onRestoreInstanceState(state);
}

@Override
protected void onDetachedFromWindow() {
    Log.d(TAG, "onDetachedFromWindow - detachedByFullscreen: " + detachedByFullscreen);

    super.onDetachedFromWindow();

    if (!detachedByFullscreen) {
        if (mediaPlayer != null) {

            this.mediaPlayer.setOnPreparedListener(null);
            this.mediaPlayer.setOnErrorListener(null);
            this.mediaPlayer.setOnSeekCompleteListener(null);
            this.mediaPlayer.setOnCompletionListener(null);
            this.mediaPlayer.setOnInfoListener(null);


            if (mediaPlayer.isPlaying())
                mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        videoIsReady = false;
        surfaceIsReady = false;
        currentState = State.END;
    }

    if (audioPlayer != null && shouldPlayAudio) {
        this.audioPlayer.setOnPreparedListener(null);
        this.audioPlayer.setOnErrorListener(null);
        this.audioPlayer.setOnSeekCompleteListener(null);
        this.audioPlayer.setOnCompletionListener(null);
        this.audioPlayer.setOnInfoListener(null);

        if (audioPlayer.isPlaying())
            audioPlayer.stop();
        audioPlayer.release();
        audioPlayer = null;

    }
    audioState = State.END;
    detachedByFullscreen = false;
}

@Override
synchronized public void surfaceCreated(SurfaceHolder holder) {
    Log.d(TAG, "surfaceCreated called = " + currentState);

    mediaPlayer.setDisplay(surfaceHolder);

    // If is not prepared yet - tryToPrepare()
    if (!surfaceIsReady) {
        surfaceIsReady = true;
        if (currentState != State.PREPARED &&
                currentState != State.PAUSED &&
                currentState != State.STARTED &&
                currentState != State.PLAYBACKCOMPLETED) {
            tryToPrepare();
            if (shouldPlayAudio) {
                tryToPrepareAudio();
            }
        }
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Log.d(TAG, "surfaceChanged called");
    resize();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d(TAG, "surfaceDestroyed called");
    if (mediaPlayer != null && mediaPlayer.isPlaying())
        mediaPlayer.pause();

    if (audioPlayer != null && audioPlayer.isPlaying())
        audioPlayer.pause();

    surfaceIsReady = false;
}

@Override
synchronized public void onPrepared(MediaPlayer mp) {
    Log.d(TAG, "onPrepared called");
    videoIsReady = true;
    tryToPrepare();

}


private class AudioPrepareListener implements OnPreparedListener {

    @Override
    synchronized public void onPrepared(MediaPlayer mediaPlayer) {
        if (shouldPlayAudio) {
            Logger.e(FullscreenVideoView.class, "Audio on Prepare called ");
            tryToPrepareAudio();
        }
    }
}

/**
 * Restore the last State before seekTo()
 *
 * @param mp the MediaPlayer that issued the seek operation
 */
@Override
public void onSeekComplete(MediaPlayer mp) {
    Log.d(TAG, "onSeekComplete");

    stopLoading();
    if (lastState != null) {
        switch (lastState) {
            case STARTED: {
                start();
            /*    if(shouldPlayAudio) {
                    startAudio();
                }*/
                break;
            }
            case PLAYBACKCOMPLETED: {
                currentState = State.PLAYBACKCOMPLETED;
                if (shouldPlayAudio) {
                    audioState = State.PLAYBACKCOMPLETED;
                }
                break;
            }
            case PREPARED: {
                currentState = State.PREPARED;
                if (shouldPlayAudio) {
                    audioState = State.PREPARED;
                }
                break;
            }
        }
    }

    if (this.seekCompleteListener != null)
        this.seekCompleteListener.onSeekComplete(mp);
}

@Override
public void onCompletion(MediaPlayer mp) {
    if (this.mediaPlayer != null) {
        if (this.currentState != State.ERROR) {
            Log.d(TAG, "onCompletion");
            if (!this.mediaPlayer.isLooping()) {
                this.currentState = State.PLAYBACKCOMPLETED;
            } else {
                start();
            }
            if (shouldPlayAudio) {
                if (!this.audioPlayer.isLooping()) {
                    audioState = State.PLAYBACKCOMPLETED;
                } else {
                    startAudio();
                }
            }
        }
    }

    if (this.completionListener != null)
        this.completionListener.onCompletion(mp);
}

@Override
public boolean onInfo(MediaPlayer mediaPlayer, int what, int extra) {
    Log.d(TAG, "onInfo " + what);

    if (this.infoListener != null)
        return this.infoListener.onInfo(mediaPlayer, what, extra);

    return false;
}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    Log.d(TAG, "onError called - " + what + " - " + extra);

    stopLoading();
    this.currentState = State.ERROR;
    audioState = State.ERROR;

    if (this.errorListener != null)
        return this.errorListener.onError(mp, what, extra);
    return false;
}

@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    Log.d(TAG, "onVideoSizeChanged = " + width + " - " + height);

    if (this.initialMovieWidth == 0 && this.initialMovieHeight == 0) {
        initialMovieWidth = width;
        initialMovieHeight = height;
        resize();
    }
}


protected void init() {
    if (isInEditMode())
        return;

    this.shouldAutoplay = false;
    this.currentState = State.IDLE;
    audioState = State.IDLE;
    this.fullscreen = false;
    this.initialConfigOrientation = -1;
    this.setBackgroundColor(Color.BLACK);

    initObjects();
}

protected void initObjects() {
    this.mediaPlayer = new MediaPlayer();
    this.audioPlayer = new MediaPlayer();
    mAudioPrepareListener = new AudioPrepareListener();

    this.surfaceView = new SurfaceView(context);
    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    layoutParams.addRule(CENTER_IN_PARENT);
    this.surfaceView.setLayoutParams(layoutParams);
    addView(this.surfaceView);

    this.surfaceHolder = this.surfaceView.getHolder();
    //noinspection deprecation
    this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    this.surfaceHolder.addCallback(this);

    this.loadingView = new ProgressBar(context);
    layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(CENTER_IN_PARENT);
    this.loadingView.setLayoutParams(layoutParams);
    addView(this.loadingView);
}

protected void releaseObjects() {
    if (this.surfaceHolder != null) {
        this.surfaceHolder.removeCallback(this);
        this.surfaceHolder = null;
    }

    if (this.mediaPlayer != null) {
        this.mediaPlayer.release();
        this.mediaPlayer = null;
    }

    if (this.audioPlayer != null) {
        this.audioPlayer.release();
        this.audioPlayer = null;
    }

    if (this.surfaceView != null)
        removeView(this.surfaceView);

    if (this.loadingView != null)
        removeView(this.loadingView);
}

/**
 * Calls prepare() method of MediaPlayer
 */
protected void prepare() throws IllegalStateException {
    startLoading();

    this.videoIsReady = false;
    this.initialMovieHeight = -1;
    this.initialMovieWidth = -1;

    this.mediaPlayer.setOnPreparedListener(this);
    this.mediaPlayer.setOnErrorListener(this);
    this.mediaPlayer.setOnSeekCompleteListener(this);
    this.mediaPlayer.setOnInfoListener(this);
    this.mediaPlayer.setOnVideoSizeChangedListener(this);
    this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);


    this.currentState = State.PREPARING;
    this.mediaPlayer.prepareAsync();


}

protected void prepareAudio() {

    this.audioPlayer.setOnPreparedListener(mAudioPrepareListener);
    this.audioPlayer.setOnSeekCompleteListener(this);
    audioState = State.PREPARING;
    this.audioPlayer.prepareAsync();
}

protected void tryToPrepare() {
    if (this.surfaceIsReady && this.videoIsReady) {
        if (this.mediaPlayer != null) {
            this.initialMovieWidth = this.mediaPlayer.getVideoWidth();
            this.initialMovieHeight = this.mediaPlayer.getVideoHeight();
        }

        resize();
        stopLoading();
        currentState = State.PREPARED;

        if (shouldAutoplay)
            start();

        if (this.preparedListener != null) {
            this.preparedListener.onPrepared(mediaPlayer);
        }

    }
}

protected void tryToPrepareAudio() {
    audioState = State.PREPARED;
    startAudio();

}

protected void startLoading() {
    this.loadingView.setVisibility(View.VISIBLE);
}

protected void stopLoading() {
    this.loadingView.setVisibility(View.GONE);
}

synchronized public State getCurrentState() {
    return currentState;
}

/**
 * Returns if VideoView is in fullscreen mode
 *
 * @return true if is in fullscreen mode otherwise false
 * @since 1.1
 */
public boolean isFullscreen() {
    return fullscreen;
}


public void setFullscreen(final boolean fullscreen) throws RuntimeException {

    if (mediaPlayer == null)
        throw new RuntimeException("Media Player is not initialized");

    if (this.currentState != State.ERROR) {
        if (FullscreenVideoView.this.fullscreen == fullscreen) return;
        FullscreenVideoView.this.fullscreen = fullscreen;

        final boolean wasPlaying = mediaPlayer.isPlaying();
        if (wasPlaying) {
            pause();
            if (shouldPlayAudio) {
                pauseAudio();
            }
        }

        if (FullscreenVideoView.this.fullscreen) {
            if (activity != null)
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

            View rootView = getRootView();
            View v = rootView.findViewById(android.R.id.content);
            ViewParent viewParent = getParent();
            if (viewParent instanceof ViewGroup) {
                if (parentView == null)
                    parentView = (ViewGroup) viewParent;

                // Prevents MediaPlayer to became invalidated and released
                detachedByFullscreen = true;

                // Saves the last state (LayoutParams) of view to restore after
                currentLayoutParams = FullscreenVideoView.this.getLayoutParams();

                parentView.removeView(FullscreenVideoView.this);
            } else
                Log.e(TAG, "Parent View is not a ViewGroup");

            if (v instanceof ViewGroup) {
                ((ViewGroup) v).addView(FullscreenVideoView.this);
            } else
                Log.e(TAG, "RootView is not a ViewGroup");
        } else {
            if (activity != null)
                activity.setRequestedOrientation(initialConfigOrientation);

            ViewParent viewParent = getParent();
            if (viewParent instanceof ViewGroup) {
                // Check if parent view is still available
                boolean parentHasParent = false;
                if (parentView != null && parentView.getParent() != null) {
                    parentHasParent = true;
                    detachedByFullscreen = true;
                }

                ((ViewGroup) viewParent).removeView(FullscreenVideoView.this);
                if (parentHasParent) {
                    parentView.addView(FullscreenVideoView.this);
                    FullscreenVideoView.this.setLayoutParams(currentLayoutParams);
                }
            }
        }

        resize();

        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (wasPlaying && mediaPlayer != null) {
                    start();
                }
                if (shouldPlayAudio) {
                    startAudio();
                }
            }
        });
    }
}

/**
 * Binds an Activity to VideoView. This is necessary to keep tracking on orientation changes
 *
 * @param activity The activity that VideoView is related to
 */
public void setActivity(Activity activity) {
    this.activity = activity;
    this.initialConfigOrientation = activity.getRequestedOrientation();
}

public void resize() {
    if (initialMovieHeight == -1 || initialMovieWidth == -1 || surfaceView == null)
        return;

    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {

            View currentParent = (View) getParent();
            if (currentParent != null) {
                float videoProportion = (float) initialMovieWidth / (float) initialMovieHeight;

                int screenWidth = currentParent.getWidth();
                int screenHeight = currentParent.getHeight();
                float screenProportion = (float) screenWidth / (float) screenHeight;

                int newWidth, newHeight;
                if (videoProportion > screenProportion) {
                    newWidth = screenWidth;
                    newHeight = (int) ((float) screenWidth / videoProportion);
                } else {
                    newWidth = (int) (videoProportion * (float) screenHeight);
                    newHeight = screenHeight;
                }

                ViewGroup.LayoutParams lp = surfaceView.getLayoutParams();
                lp.width = newWidth;
                lp.height = newHeight;
                surfaceView.setLayoutParams(lp);

                Log.d(TAG, "Resizing: initialMovieWidth: " + initialMovieWidth + " - initialMovieHeight: " + initialMovieHeight);
                Log.d(TAG, "Resizing: screenWidth: " + screenWidth + " - screenHeight: " + screenHeight);
            }
        }
    });
}

/**
 * Tells if application should autoplay videos as soon as it is prepared
 *
 * @return true if application are going to play videos as soon as it is prepared
 */
public boolean isShouldAutoplay() {
    return shouldAutoplay;
}

/**
 * Tells application that it should begin playing as soon as buffering
 * is ok
 *
 * @param shouldAutoplay If true, call start() method when getCurrentState() == PREPARED. Default is false.
 */
public void setShouldAutoplay(boolean shouldAutoplay) {
    this.shouldAutoplay = shouldAutoplay;
}

/**
 * Toggles view to fullscreen mode
 * It saves currentState and calls pause() method.
 * When fullscreen is finished, it calls the saved currentState before pause()
 * In practice, it only affects STARTED state.
 * If currenteState was STARTED when fullscreen() is called, it calls start() method
 * after fullscreen() has ended.
 *
 * @deprecated As of release 1.1.0, replaced by {@link #setFullscreen(boolean)}
 */
@Deprecated
public void fullscreen() throws IllegalStateException {
    setFullscreen(!fullscreen);
}

/**
 * MediaPlayer method (getCurrentPosition)
 *
 * @see <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#getCurrentPosition%28%29">getCurrentPosition</a>
 */
public int getCurrentPosition() {
    if (mediaPlayer != null)
        return mediaPlayer.getCurrentPosition();
    else throw new RuntimeException("Media Player is not initialized");
}

/**
 * MediaPlayer method (getDuration)
 *
 * @see <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#getDuration%28%29">getDuration</a>
 */
public int getDuration() {
    if (mediaPlayer != null)
        return mediaPlayer.getDuration();
    else throw new RuntimeException("Media Player is not initialized");
}

/**
 * MediaPlayer method (getVideoHeight)
 *
 * @see <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#getVideoHeight%28%29">getVideoHeight</a>
 */
public int getVideoHeight() {
    if (mediaPlayer != null)
        return mediaPlayer.getVideoHeight();
    else throw new RuntimeException("Media Player is not initialized");
}

/**
 * MediaPlayer method (getVideoWidth)
 *
 * @see <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#getVideoWidth%28%29">getVideoWidth</a>
 */
public int getVideoWidth() {
    if (mediaPlayer != null)
        return mediaPlayer.getVideoWidth();
    else throw new RuntimeException("Media Player is not initialized");
}


/**
 * MediaPlayer method (isLooping)
 *
 * @see <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#isLooping%28%29">isLooping</a>
 */
public boolean isLooping() {
    if (mediaPlayer != null)
        return mediaPlayer.isLooping();
    else throw new RuntimeException("Media Player is not initialized");
}

/**
 * MediaPlayer method (isPlaying)
 *
 * @see <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#isPlaying%28%29">isPlaying</a>
 */
public boolean isPlaying() throws IllegalStateException {
    if (mediaPlayer != null)
        return mediaPlayer.isPlaying();
    else throw new RuntimeException("Media Player is not initialized");
}

public boolean isPlayingAudio() throws IllegalStateException {
    if (audioPlayer != null && shouldPlayAudio) {
        return audioPlayer.isPlaying();
    } else throw new RuntimeException("Audio Player is not initialized");
}

/**
 * MediaPlayer method (pause)
 *
 * @see <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#pause%28%29">pause</a>
 */
public void pause() throws IllegalStateException {
    Log.d(TAG, "pause");
    if (mediaPlayer != null) {
        currentState = State.PAUSED;
        mediaPlayer.pause();
    } else throw new RuntimeException("Media Player is not initialized");

}

/**
 * MediaPlayer method (pause)
 *
 * @see <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#pause%28%29">pause</a>
 */
public void pauseAudio() throws IllegalStateException {
    Log.d(TAG, "pause");
    if ((shouldPlayAudio && audioPlayer != null)) {
        if (audioPlayer.isPlaying()) {
            audioState = State.PAUSED;
            audioPlayer.pause();
        }
    } else throw new RuntimeException("Audio Player is not initialized");

}

/**
 * Due to a lack of access of SurfaceView, it rebuilds mediaPlayer and all
 * views to update SurfaceView canvas
 */
public void reset() {
    Log.d(TAG, "reset");

    if (mediaPlayer != null) {
        this.currentState = State.IDLE;
        if (shouldPlayAudio && audioPlayer != null) {
            this.audioState = State.IDLE;
        }
        releaseObjects();
        initObjects();

    } else throw new RuntimeException("Media Player is not initialized");

   /* if (shouldPlayAudio && audioPlayer != null) {

        releaseObjects();
        initObjects();

    } else throw new RuntimeException("Media Player is not initialized");*/
}

/**
 * MediaPlayer method (start)
 *
 * @see <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#start%28%29">start</a>
 */
public void start() throws IllegalStateException {
    Log.d(TAG, "start");

 /*   if (audioPlayer != null) {
        audioState = State.STARTED;
        audioPlayer.setOnCompletionListener(this);
        audioPlayer.start();
    } else throw new RuntimeException("Media Player is not initialized");*/

    if (mediaPlayer != null) {
        currentState = State.STARTED;
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.start();

    } else throw new RuntimeException("Media Player is not initialized");
}

public void startAudio() throws IllegalStateException {
    if (shouldPlayAudio) {
        audioState = State.STARTED;
        audioPlayer.setOnCompletionListener(this);
        audioPlayer.start();
        audioPlayer.seekTo(mediaPlayer.getCurrentPosition());
    } else throw new RuntimeException("Media Player is not initialized");
}

/**
 * MediaPlayer method (stop)
 *
 * @see <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#stop%28%29">stop</a>
 */
public void stop() throws IllegalStateException {
    Log.d(TAG, "stop");

    if (mediaPlayer != null) {
        currentState = State.STOPPED;
        mediaPlayer.stop();
    } else throw new RuntimeException("Media Player is not initialized");

    if (audioPlayer != null && shouldPlayAudio) {
        audioState = State.STOPPED;
        audioPlayer.stop();
    } else throw new RuntimeException("Media Player is not initialized");
}

/**
 * MediaPlayer method (seekTo)
 * It calls pause() method before calling MediaPlayer.seekTo()
 *
 * @param msec the offset in milliseconds from the start to seek to
 * @throws IllegalStateException if the internal player engine has not been initialized
 * @see <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#seekTo%28%29">seekTo</a>
 */
public  void seekTo(int msec) throws IllegalStateException {
    Log.d(TAG, "seekTo = " + msec);

//sikAudio(msec); if(media播放器!=null) { // 没有直播流,如果(medialayer.get持续时间()

    } else throw new RuntimeException("Media Player is not initialized");


    seekAudio(msec);


  /*  Logger.e(FullscreenVideoView.class,"Media player state "+mediaPlayer.getCurrentPosition()+" Audio Player state "+audioPlayer.getCurrentPosition());
    Logger.e(FullscreenVideoView.class,"Media player duration "+mediaPlayer.getDuration()+" Audio Player duration "+audioPlayer.getDuration());*/
}

public void seekAudio(int msec) {
    if ((audioPlayer != null && shouldPlayAudio)) {
        // No live streaming
        if (audioPlayer.getDuration() > -1 && msec <= audioPlayer.getDuration()) {
            lastState = audioState;
            pauseAudio();
            audioPlayer.seekTo(msec);
            startLoading();
        }
    } else throw new RuntimeException("Audio Player is not initialized");
}



/**
 * VideoView method (setVideoURI)
 */
public void setVideoURI(Uri uri) throws IOException, IllegalStateException, SecurityException, IllegalArgumentException, RuntimeException {
    if (mediaPlayer != null) {
        if (currentState != State.IDLE)
            throw new IllegalStateException("FullscreenVideoView Invalid State: " + currentState);

        mediaPlayer.setDataSource(context, uri);
        currentState = State.INITIALIZED;
        prepare();
    } else throw new RuntimeException("Media Player is not initialized");
}

/**
 * VideoView method (setVideoURI)
 */
public void setAudioVideoURI(Uri uri, boolean shouldPlayAudio) throws IOException, IllegalStateException, SecurityException, IllegalArgumentException, RuntimeException {
    this.shouldPlayAudio = shouldPlayAudio;
    Log.e("Audio URI ", "Audio URI " + uri.toString());
    if (audioPlayer != null && shouldPlayAudio) {
        if (audioState != State.IDLE)
            throw new IllegalStateException("FullscreenVideoView Invalid State: " + audioState);

        audioPlayer.setDataSource(context, uri);
        audioState = State.INITIALIZED;
        prepareAudio();
    } else throw new RuntimeException("Media Player is not initialized");
}

}

共有1个答案

汤枫
2023-03-14
public synchronized void seekTo(int msec) throws IllegalStateException {
            Log.d(TAG, "seekTo = " + msec);
            //        seekAudio(msec);
            if (mediaPlayer != null) {
                // No live streaming
                if (mediaPlayer.getDuration() > -1 && msec <= mediaPlayer.getDuration()) {
                    lastState = currentState;
                    pause();
                    mediaPlayer.seekTo(msec);
                    pause();
//                    startLoading();
                }

                if ((audioPlayer != null && shouldPlayAudio)) {
                    // No live streaming
                    if (audioPlayer.getDuration() > -1 && msec <= audioPlayer.getDuration()) {
                        lastState = audioState;
                        pauseAudio();
                        audioPlayer.seekTo(msec);
                        pause();
//                        startLoading();
                    }


                    Handler mHandler = new Handler();
                    mHandler.postDelayed(new Runnable() {
                        public void run() {
                            try {
                                mediaPlayer.start();
                                audioPlayer.start();
                                startLoading();
                            }catch(Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, seconds * 1000);

                } else throw new RuntimeException("Audio Player is not initialized");


            } else throw new RuntimeException("Media Player is not initialized");

          /*  Logger.e(FullscreenVideoView.class,"Media player state "+mediaPlayer.getCurrentPosition()+" Audio Player state "+audioPlayer.getCurrentPosition());
            Logger.e(FullscreenVideoView.class,"Media player duration "+mediaPlayer.getDuration()+" Audio Player duration "+audioPlayer.getDuration());*/
        }
 类似资料:
  • 我正在开发一个包含一些音频播放器的RecyclerView的应用程序。应用程序将下载。3gp文件(如果尚未下载)。 当我单击playAudio按钮时,音频未被播放。 这是我的适配器代码: 我怎样才能解决这个问题?

  • 我正在使用Android Media Player从我的服务器播放音乐。可以使用MediaPlayer将播放音频保存到存储吗?

  • 我正在开发一个音频播放器,它可以在后台播放音频文件。我的问题是,当录像机或视频播放器启动时,我需要暂停音频播放器。 有什么方法可以处理这个任务吗?例如,我有来处理这些调用。当我接到呼叫或wnat呼叫时,我们可以使用呼叫状态暂停播放器。我想为录像机或视频播放器以及相同的场景。当视频/录制开始时,我需要暂停音频播放器。

  • 我已经创建了一个android应用程序,使用媒体播放器播放文本到语音文件,但是如果其他音频/视频开始播放,那么我的音频也会播放,即同时播放两个音频。 有没有办法在开始另一个音频/视频之前停止第一个音频。 是否有任何广播接收器会在其他音频开始时被调用。 我用过- layer.play播放音频。 和暂停音频。 任何帮助都将不胜感激。

  • 这可能不是一个可以接受的问题,但我现在非常绝望。 我需要一个同步java媒体播放器与快速寻找和平衡修改。 脚本: 我有一个javaFX项目,我必须在循环中播放一个非常短(50-100毫秒)的媒体文件。问题是,在重新启动之前,我需要等待一些要求。 简而言之:播放声音- javafx提供了一个我修改过的媒体播放器。 如果有人能为我指出正确的方向(图书馆/我错过的东西),我将不胜感激 ps允许的java

  • 主要内容:本节引言:,1.相关方法详解,2.使用代码示例,3.本节示例代码下载:,本节小结:本节引言: 本节带来的是Android多媒体中的——MediaPlayer,我们可以通过这个API来播放音频和视频 该类是Androd多媒体框架中的一个重要组件,通过该类,我们可以以最小的步骤来获取,解码 和播放音视频。它支持三种不同的媒体来源: 本地资源 内部的URI,比如你可以通过ContentResolver来获取 外部URL(流) 对于Android所支持的的媒体格式列表 对于Androi