Android-无法播放视频

时间:2018-10-29 12:36:23

标签: android android-videoview

public class Tutorial extends AppCompatActivity {

    VideoView mVideoView;
    ListView mListView;
    TextView mTitle, mContent;
    ArrayList<String> videoList;
    ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tutorial);

        mVideoView = (VideoView) findViewById(R.id.videoTutorial);
        mListView = (ListView)findViewById(R.id.listView);
        mTitle = (TextView)findViewById(R.id.tutorialTitle);
        mContent = (TextView)findViewById(R.id.tutorialContent);

        videoList = new ArrayList<>();
        videoList.add("Video 1");
        videoList.add("Video 2");
        videoList.add("Video 3");
        videoList.add("Video 4");
        videoList.add("Video 5");


        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,videoList);
        mListView.setAdapter(adapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.scm));
                        break;
                    case 1:
                        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.videopart5));
                        break;
                    case 2:
                        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" +  R.raw.videopart1));
                        break;
                    case 3:
                        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" +  R.raw.videopart2));
                        break;
                    case 4:
                        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" +  R.raw.videopart3));
                        break;
                    case 5:
                        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" +  R.raw.videopart4));
                        break;
                    default:
                        break;
                }
                mVideoView.setMediaController(new MediaController(Tutorial.this));
                mVideoView.requestFocus();
                mVideoView.start();
            }
        });

    }
}

我是Android新手,现在我正尝试使用ListView制作可滚动的VideoView。我从许多其他资源和网站上学习过。编译时没有错误。

结构看起来不错,但是当我单击“视频1”时,它显示为“无法播放此视频”。 代码中是否有错误?

1 个答案:

答案 0 :(得分:0)

您应该尝试以下操作:

// To create the media controller (play, pause, etc.)
MediaController myController = new MediaController(this);

// To link the media controller to the video view.
myController.setAnchorView(mVideoView);

// To link the video view to the media controller.
mVideoView.setMediaController(myController);

//To get the 
mVideoView.requestFocus();

// To autoplay on opening.
mVideoView.start();