Android Mediaplayer 缓冲未完成

发布于 2024-11-14 17:00:25 字数 7257 浏览 2 评论 0原文

我已经实现了一个标准 MediaPlayer,就像 Android API 示例中的那样。由于某种原因,我的视频仅缓冲到 47% 左右,然后就停止了。我不认为这是互联网连接问题,因为它每次都会停在 46% 或 47%。该文件可以通过本机 Android 视频播放器缓冲并正常播放。它是渐进式下载 mp4。

这是它的代码:

package com.exact.test;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.text.Layout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoPlayer extends RelativeLayout implements
OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {

    private VideoPlayer me = null; 
    //Components that make up the view
    private VideoView videoView = null;
    private ImageSwitcher imageSwitcher = null;
    private ImageButton btnBack = null;
    private ImageButton btnPlay = null;
    private ImageButton btnStop = null;
    private ImageButton btnForward = null;
    private TextView txtBufferStatus = null;

    //Video playback variables
    private static final String TAG = "VideoPlayer";
    private int mVideoWidth;
    private int mVideoHeight;
    private MediaPlayer mMediaPlayer;
    private SurfaceView mPreview;
    private SurfaceHolder holder;
    public String path = "http://mydomain.com/android/test8.mp4";
    private boolean mIsVideoSizeKnown = false;
    private boolean mIsVideoReadyToBePlayed = false;

    public VideoPlayer(Context context) {
        this(context, null);        
    }

    public VideoPlayer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);        
    }

    public VideoPlayer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        ((Activity)getContext())
                .getLayoutInflater()
                .inflate(R.layout.videoplayer, this, true);
        me = this;

        //Get all the components in the layout
        //Layout videoLayout = VideoPlayer.
        videoView = (VideoView)findViewById(R.id.videoView);
        imageSwitcher = (ImageSwitcher)findViewById(R.id.ImageSwitcher);
        btnBack = (ImageButton)findViewById(R.id.btnBack);
        btnPlay = (ImageButton)findViewById(R.id.btnPlay);
        btnStop = (ImageButton)findViewById(R.id.btnStop);
        btnForward = (ImageButton)findViewById(R.id.btnForward);
        txtBufferStatus = (TextView)findViewById(R.id.txtBufferStatus);

        btnBack.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {
            }
        });

        btnPlay.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {
                txtBufferStatus.setText("Initilaising...");
                imageSwitcher.setVisibility(INVISIBLE);             
                videoView.setVisibility(VISIBLE);               
                mPreview = (SurfaceView) videoView;             
                holder = mPreview.getHolder();              
                holder.addCallback(me);             
                holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
                txtBufferStatus.setText("Initilaised");
            }
        });

        btnStop.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {
            }
        });

        btnForward.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
            }
        });
    }

    public void playVideo() {

        doCleanUp();

        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    // Create a new media player and set the listeners
                    txtBufferStatus.setText("Setting up Media Player...");
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDataSource(path);
                    mMediaPlayer.setDisplay(holder);
                    txtBufferStatus.setText("Preparing...");
                    mMediaPlayer.prepareAsync();
                    mMediaPlayer.setOnBufferingUpdateListener(me);
                    mMediaPlayer.setOnCompletionListener(me);
                    mMediaPlayer.setOnPreparedListener(me);
                    mMediaPlayer.setOnVideoSizeChangedListener(me);
                    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                } catch (Exception e) {
                    Log.e(TAG, "error: " + e.getMessage(), e);
                }                   
            }
        };
        Thread t = new Thread(r);
        t.start();
    }

    public void onBufferingUpdate(MediaPlayer arg0, int percent) {
        Log.d(TAG, "onBufferingUpdate percent:" + percent);       
        txtBufferStatus.setText(Integer.toString(percent));
    }

    public void onCompletion(MediaPlayer arg0) {
        Log.d(TAG, "onCompletion called");
    }

    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
        Log.v(TAG, "onVideoSizeChanged called");
        if (width == 0 || height == 0) {
            Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
            return;
        }
        mIsVideoSizeKnown = true;
        mVideoWidth = width;
        mVideoHeight = height;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    public void onPrepared(MediaPlayer mediaplayer) {
        Log.d(TAG, "onPrepared called");
        txtBufferStatus.setText("Prepared");
        mIsVideoReadyToBePlayed = true;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
        Log.d(TAG, "surfaceChanged called");
    }

    public void surfaceDestroyed(SurfaceHolder surfaceholder) {
        Log.d(TAG, "surfaceDestroyed called");
    }


    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated called");
        playVideo();
    }

    public void releaseMediaPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    public void doCleanUp() {
        mVideoWidth = 0;
        mVideoHeight = 0;
        mIsVideoReadyToBePlayed = false;
        mIsVideoSizeKnown = false;
    }

    private void startVideoPlayback() {
        Log.v(TAG, "startVideoPlayback");
        holder.setFixedSize(mVideoWidth, mVideoHeight);
        mMediaPlayer.start();
    }

}

I have implemented a standard MediaPlayer like the one in the Android API sample. For some reason my video only buffers to something like 47% and then stops. I don't think it's an internet connection issue since it stops at 46% or 47% every single time. The file buffers and plays fine through the native android video player. Its a progressive download mp4.

Here is the code for it:

package com.exact.test;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.text.Layout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoPlayer extends RelativeLayout implements
OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {

    private VideoPlayer me = null; 
    //Components that make up the view
    private VideoView videoView = null;
    private ImageSwitcher imageSwitcher = null;
    private ImageButton btnBack = null;
    private ImageButton btnPlay = null;
    private ImageButton btnStop = null;
    private ImageButton btnForward = null;
    private TextView txtBufferStatus = null;

    //Video playback variables
    private static final String TAG = "VideoPlayer";
    private int mVideoWidth;
    private int mVideoHeight;
    private MediaPlayer mMediaPlayer;
    private SurfaceView mPreview;
    private SurfaceHolder holder;
    public String path = "http://mydomain.com/android/test8.mp4";
    private boolean mIsVideoSizeKnown = false;
    private boolean mIsVideoReadyToBePlayed = false;

    public VideoPlayer(Context context) {
        this(context, null);        
    }

    public VideoPlayer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);        
    }

    public VideoPlayer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        ((Activity)getContext())
                .getLayoutInflater()
                .inflate(R.layout.videoplayer, this, true);
        me = this;

        //Get all the components in the layout
        //Layout videoLayout = VideoPlayer.
        videoView = (VideoView)findViewById(R.id.videoView);
        imageSwitcher = (ImageSwitcher)findViewById(R.id.ImageSwitcher);
        btnBack = (ImageButton)findViewById(R.id.btnBack);
        btnPlay = (ImageButton)findViewById(R.id.btnPlay);
        btnStop = (ImageButton)findViewById(R.id.btnStop);
        btnForward = (ImageButton)findViewById(R.id.btnForward);
        txtBufferStatus = (TextView)findViewById(R.id.txtBufferStatus);

        btnBack.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {
            }
        });

        btnPlay.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {
                txtBufferStatus.setText("Initilaising...");
                imageSwitcher.setVisibility(INVISIBLE);             
                videoView.setVisibility(VISIBLE);               
                mPreview = (SurfaceView) videoView;             
                holder = mPreview.getHolder();              
                holder.addCallback(me);             
                holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
                txtBufferStatus.setText("Initilaised");
            }
        });

        btnStop.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {
            }
        });

        btnForward.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
            }
        });
    }

    public void playVideo() {

        doCleanUp();

        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    // Create a new media player and set the listeners
                    txtBufferStatus.setText("Setting up Media Player...");
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDataSource(path);
                    mMediaPlayer.setDisplay(holder);
                    txtBufferStatus.setText("Preparing...");
                    mMediaPlayer.prepareAsync();
                    mMediaPlayer.setOnBufferingUpdateListener(me);
                    mMediaPlayer.setOnCompletionListener(me);
                    mMediaPlayer.setOnPreparedListener(me);
                    mMediaPlayer.setOnVideoSizeChangedListener(me);
                    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                } catch (Exception e) {
                    Log.e(TAG, "error: " + e.getMessage(), e);
                }                   
            }
        };
        Thread t = new Thread(r);
        t.start();
    }

    public void onBufferingUpdate(MediaPlayer arg0, int percent) {
        Log.d(TAG, "onBufferingUpdate percent:" + percent);       
        txtBufferStatus.setText(Integer.toString(percent));
    }

    public void onCompletion(MediaPlayer arg0) {
        Log.d(TAG, "onCompletion called");
    }

    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
        Log.v(TAG, "onVideoSizeChanged called");
        if (width == 0 || height == 0) {
            Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
            return;
        }
        mIsVideoSizeKnown = true;
        mVideoWidth = width;
        mVideoHeight = height;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    public void onPrepared(MediaPlayer mediaplayer) {
        Log.d(TAG, "onPrepared called");
        txtBufferStatus.setText("Prepared");
        mIsVideoReadyToBePlayed = true;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
        Log.d(TAG, "surfaceChanged called");
    }

    public void surfaceDestroyed(SurfaceHolder surfaceholder) {
        Log.d(TAG, "surfaceDestroyed called");
    }


    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated called");
        playVideo();
    }

    public void releaseMediaPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    public void doCleanUp() {
        mVideoWidth = 0;
        mVideoHeight = 0;
        mIsVideoReadyToBePlayed = false;
        mIsVideoSizeKnown = false;
    }

    private void startVideoPlayback() {
        Log.v(TAG, "startVideoPlayback");
        holder.setFixedSize(mVideoWidth, mVideoHeight);
        mMediaPlayer.start();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2024-11-21 17:00:25

缓冲可能已完成。
您的缓冲可能是这样的:10% - 23% - 30% - 47% - 100%

It is likely that the buffering is finished.
Your buffering could go this way: 10% - 23% - 30% - 47% - 100%

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文