片段中的VideoView出现“无法创建媒体播放器”错误

时间:2018-12-13 16:31:39

标签: android android-videoview

对于一个简单的应用,我遵循此代码,该应用使用videoview复制视频https://www.journaldev.com/20671/android-videoview

一切都完美!

问题是我想将此代码放入主应用程序的片段中,然后将代码从活动“转换”为片段。当我在设备上启动应用程序时,这使我进入logcat错误

12-13 17:24:16.371 17097-17097/*** E/MediaPlayer: Unable to create media player

并且视频没有开始,但是我看到一条警告,提示“无法播放视频”。

我进行了很多搜索,但没有发现对我有用的东西。

这是我的代码:

fragment_video.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".VideoFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</FrameLayout>

Videofragment.java

package ***;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * A simple {@link Fragment} subclass.
 */
public class VideoFragment extends Fragment {


VideoView vd;

    public VideoFragment() {
        // Required empty public constructor
    }


    VideoView videoView;
    ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"));
    int index = 0;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v= inflater.inflate(R.layout.fragment_video, container, false);

        videoView = v.findViewById(R.id.videoView);
        final MediaController mediacontroller = new MediaController(getActivity());
        mediacontroller.setAnchorView(videoView);

        videoView.setMediaController(mediacontroller);
        videoView.setVideoURI(Uri.parse(arrayList.get(index)));
        videoView.requestFocus();

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                        videoView.setMediaController(mediacontroller);
                        mediacontroller.setAnchorView(videoView);

                    }
                });
            }
        });

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Toast.makeText(getActivity().getApplicationContext(), "Video over", Toast.LENGTH_SHORT).show();
                if (index++ == arrayList.size()) {
                    index = 0;
                    mp.release();
                    Toast.makeText(getActivity().getApplicationContext(), "Video over", Toast.LENGTH_SHORT).show();
                } else {
                    videoView.setVideoURI(Uri.parse(arrayList.get(index)));
                    videoView.start();
                }


            }
        });

        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d("API123", "What " + what + " extra " + extra);
                return false;
            }
        });
        return v;

    }

}

这是我想将视频投射到活动的代码(using this code like a template,因为我想将视频显示在滑块中)。

    package ***;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.stephentuso.welcome.TitlePage;
import com.stephentuso.welcome.WelcomeActivity;
import com.stephentuso.welcome.WelcomeConfiguration;
import com.stephentuso.welcome.FragmentWelcomePage;

public class MainActivity extends WelcomeActivity  {

    @Override
    protected WelcomeConfiguration configuration() {
        return new WelcomeConfiguration.Builder(this)
                .defaultBackgroundColor(R.color.colorPrimary)
                .page(new FragmentWelcomePage() {
                    @Override
                    protected android.support.v4.app.Fragment fragment() {
                        return new VideoFragment();
                    }
                }.background(R.color.colorPrimary))
                .page(new TitlePage(R.drawable.lorem1, getString(R.string.label_lorem_1))
                )
                .page(new TitlePage(R.drawable.lorem2, getString(R.string.label_lorem_2))
                )
                .page(new TitlePage(R.drawable.lorem3, getString(R.string.label_lorem_3))
                )
                .swipeToDismiss(true)
                .build();
    }
}

我不知道问题可能在哪里。 预先谢谢你。

0 个答案:

没有答案