我是android应用程序开发的新手。我想开发一个视频播放器。在这个视频播放器中,我需要实现文件选择器/文件资源管理器来打开视频文件以选择要播放的视频。这就是我工作正常。
我知道当尝试播放非视频文件时,它会显示错误消息,例如,
Sorry,this video cannot be play
这也很好用。问题是:
- 为什么此错误消息(抱歉,此视频无法播放)显示两次?
- 为什么显示错误消息后,我尝试从文件选择器打开一个新文件,它再次显示该错误消息。(即)在我选择新的正确视频文件后,此错误消息会在我播放所选视频文件之前突然显示(再次回调)。
这是我的代码:
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();
}
}
我该如何解决这个错误?任何人都可以帮助我找到我所做的错误