Android MediaPlayer无法在客户端打开文件

时间:2016-11-22 23:16:52

标签: java android video-streaming android-mediaplayer android-videoview

最近我用ButterKnife更新了我的代码,重构后我遇到了视频流的问题。每次报告VideoView.setVisibility()后都会报告错误,图像不可见,但可以听到音频。我确信它与布局无关,因为没有任何更改,只有下面提供的课程。

有没有人遇到类似的问题?谢谢你的任何建议。

默认情况下,我想隐藏用户的VideoView,直到按下播放按钮。

W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content provider: http://img.jbzd.pl/2016/06/7c5ed467a0e8ae7d04c80d009851d391.webm
D/MediaPlayer: setDataSource IOException | SecurityException happend : 
    java.io.FileNotFoundException: No content provider: http://img.jbzd.pl/2016/06/7c5ed467a0e8ae7d04c80d009851d391.webm
    at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1141)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:991)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:914)
    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1121)
    at android.widget.VideoView.openVideo(VideoView.java:371)
    at android.widget.VideoView.access$2100(VideoView.java:71)
    at android.widget.VideoView$7.surfaceCreated(VideoView.java:652)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:712)
    at android.view.SurfaceView.setVisibility(SurfaceView.java:334)
    at xxxooo.jbzd.fragments.SinglePostFragment.startPlayback(SinglePostFragment.java:236)
    at xxxooo.jbzd.fragments.SinglePostFragment_ViewBinding$3.doClick(SinglePostFragment_ViewBinding.java:56)
    at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
    at android.view.View.performClick(View.java:5697)
    at android.view.View$PerformClick.run(View.java:22526)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

此代码在重构之前正在运行,但我找不到问题:

public class SinglePostFragment extends CustomFragment implements MediaPlayer.OnCompletionListener {

    @BindView(R.id.postImage) ImageView m_postImage;
    @BindView(R.id.playbackIcon) ImageView m_playIcon;
    @BindView(R.id.singlepost_postVideo) VideoView m_postVideo;
    @BindView(R.id.single_postTitle) TextView m_postTitle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_singlepost, container, false);
        ButterKnife.setDebug(true);
        ButterKnife.bind(this, view);

        m_prevImgBtn.setVisibility(View.INVISIBLE);

        MediaController mc = new MediaController(context);
        mc.setAnchorView(v);
        mc.setMediaPlayer(v);
        m_postVideo.setMediaController(mc);
        m_postVideo.setKeepScreenOn(true);
        m_postVideo.setVideoURI(Uri.EMPTY);
        m_postVideo.setOnCompletionListener(this);

        m_progressDialog = new ProgressDialog(getContext());
        m_progressDialog.setIndeterminate(true);
        m_progressDialog.setCancelable(true);
        m_progressDialog.setMessage(getResources().getString(R.string.progress_dialog_message));
        m_progressDialog.setTitle(getResources().getString(R.string.progress_dialog_loading_image));

        m_currentPost = m_postsHandler.getPostsList(MainActivity.Category.RandomPosts).get(0);

        updateView();

        return view;
    }

    private void updateView() {
        m_postTitle.setText(m_currentPost.getPostTitle());

        m_postVideo.setVisibility(View.INVISIBLE);
        m_postImage.setVisibility(View.VISIBLE);

        switch (m_currentPost.getPostKind()) {
            case IMAGE:
                m_playIcon.setVisibility(View.INVISIBLE);
                m_postVideo.setVideoURI(Uri.EMPTY);
                m_postImage.setAlpha(1.0F);
                loadImage(((IImagePost) m_currentPost).getImageLink());
                break;

            case VIDEO:
                m_playIcon.setVisibility(View.VISIBLE);
                m_postVideo.setVideoURI(Uri.parse(((IVideoPost) m_currentPost).getVideoLink()));
                m_postImage.setAlpha(0.5F);
                loadImage(((IVideoPost) m_currentPost).getPosterLink());
                break;
        }

        Log.i("PostKind", m_currentPost.getPostKind().toString());
    }

    private void loadImage(String imageLink) {
        Picasso.with(getContext()).load(imageLink).into(m_postImage, new com.squareup.picasso.Callback() {
            @Override
            public void onSuccess() {
                m_progressDialog.dismiss();
            }

            @Override
            public void onError() {
                //TODO: Show either toast or message dialog to user + rollback to previous post
            }
        });
    }

    @SuppressWarnings("unused") // Injected by ButterKnife
    @OnClick(R.id.playbackIcon)
    public void startPlayback(View v) {
        v.setVisibility(View.INVISIBLE);
        m_postImage.setVisibility(View.INVISIBLE);
        m_postVideo.setVisibility(View.VISIBLE);

        m_progressDialog.setTitle(getResources().getString(R.string.progress_dialog_buffering_video));
        m_progressDialog.show();

        m_postVideo.requestFocus();
        m_postVideo.start();
        m_postVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
                mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                    @Override
                    public boolean onInfo(MediaPlayer mp, int what, int extra) {
                        if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START)
                            m_progressDialog.show();

                        if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END)
                            m_progressDialog.dismiss();

                        return false;
                    }
                });
            }
        });
        m_postVideo.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                m_progressDialog.dismiss();
                return false;
            }
        });
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        m_postVideo.setVisibility(View.INVISIBLE);
        m_playIcon.setVisibility(View.VISIBLE);
        m_postImage.setVisibility(View.VISIBLE);
    }
}

0 个答案:

没有答案