错误消息来了两次,并在Android VideoView中反复回调

时间:2013-09-14 06:32:28

标签: android android-videoview

我是Android应用程序开发的新手。我想开发一个视频播放器。在这个视频播放器中,我需要实现文件选择器/文件浏览器来打开视频文件以选择要播放的视频。这就是我工作正常。

我知道当尝试播放非视频文件时,它会显示错误信息,如

Sorry,this video cannot be play

这也很好。问题是:

  1. 为什么会出现此错误消息(抱歉,此视频无法播放)显示两次?
  2. 为什么在显示错误消息后,我尝试从文件选择器中打开一个新文件,它再次显示该错误消息。 (即)在我选择新的正确视频文件后,在播放所选视频文件之前,此错误消息会突然显示(再次回叫)。
  3. 这是我的代码:

    package com.example.video_bug;
    
    import java.io.File;
    
    import com.ipaulpro.afilechooser.utils.FileUtils;
    
    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.ActivityNotFoundException;
    import android.content.Intent;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.MediaController;
    import android.widget.Toast;
    import android.widget.VideoView;
    
    public class MainActivity extends Activity {
    
        VideoView player;
        Button b1,b2,b3;
        String videoFilePath;
         private static final int REQUEST_CODE = 6384;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            player = (VideoView) findViewById(R.id.videoView1);
            b1=(Button)findViewById(R.id.button1);
            b2=(Button)findViewById(R.id.button2);
            b3=(Button)findViewById(R.id.button3);
    
            b1.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    showChooser();
                }
            });
    
            b2.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    player.setVideoURI(Uri.parse(videoFilePath));
                      player.setMediaController(new   MediaController(MainActivity.this));
                       player.requestFocus();
                    player.start();
                }
            });
    
          b3.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                player.stopPlayback();
            }
        });
    
    }
    
        private void showChooser() {
            // Use the GET_CONTENT intent from the utility class
            Intent target = FileUtils.createGetContentIntent();
            // Create the chooser Intent
            Intent intent = Intent.createChooser(
                    target, getString(R.string.chooser_title));
            try {
                startActivityForResult(intent, REQUEST_CODE);
            } catch (ActivityNotFoundException e) {
                // The reason for the existence of aFileChooser
            }               
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
            case REQUEST_CODE:  
                // If the file selection was successful
                if (resultCode == RESULT_OK) {      
                    if (data != null) {
                        // Get the URI of the selected file
                        final Uri uri = data.getData();
    
                        try {
                            // Create a file instance from the URI
                            final File file = FileUtils.getFile(uri);
                            videoFilePath=file.getAbsolutePath();
                            Toast.makeText(MainActivity.this, 
                                    "File Selected: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
                        } catch (Exception e) {
                            Log.e("FileSelectorTestActivity", "File select error", e);
                        }
                    }
                } 
                break;
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    
    
        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            Log.e("called","onPause");
            super.onPause();
        }
    
    
        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            Log.e("called","onResume");
            super.onResume();
        }
    
    }
    

    如何解决此错误?任何人帮我找到错误我做了什么

0 个答案:

没有答案