当我点击播放按钮时,它没有播放视频。我是否需要对清单文件进行任何更改?当用户将手机从纵向模式旋转到横向模式时,如何更改视图?
public class MainActivity extends Activity implements OnClickListener{
private static final String TAG="VideoPlayer";
EditText videoPath;
VideoView video;
ImageButton play;
ImageButton pause;
ImageButton reset;
ImageButton stop;
String current;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoPath=(EditText)findViewById(R.id.path);
videoPath.setText("G:\\Amar\\Messsssss\\Mobile Data\\a.mp4");
video=(VideoView)findViewById(R.id.videoView);
play=(ImageButton)findViewById(R.id.playButton);
pause=(ImageButton)findViewById(R.id.pauseButton);
reset=(ImageButton)findViewById(R.id.resetButton);
stop=(ImageButton)findViewById(R.id.stopButton);
play.setOnClickListener(this);
pause.setOnClickListener(this);
reset.setOnClickListener(this);
stop.setOnClickListener(this);
runOnUiThread(new Runnable(){
public void run()
{
playVideo();
}
});
}
@Override
public void onClick(View v) {
int id=v.getId();
switch(id)
{
case R.id.playButton:
playVideo();
break;
case R.id.pauseButton:
if(video!=null)
video.pause();
break;
case R.id.resetButton:
if(video!=null)
video.seekTo(0);
break;
case R.id.stopButton:
if(video!=null)
current=null;
video.stopPlayback();
break;
}
}
private void playVideo()
{
try
{
final String path=videoPath.getText().toString();
Log.v(TAG,"Path: "+path);
if(path==null ||path.length()==0)
{
Toast.makeText(MainActivity.this,"url/path is empty",Toast.LENGTH_LONG).show();
}
current=path;
video.setVideoPath(pathSource(current));
video.start();
video.requestFocus();
}
catch(Exception e)
{
Log.e(TAG,"error:+ "+e.getMessage(),e);
if(video!=null)
{
video.stopPlayback();
}
}
}
private String pathSource(String path)throws IOException
{
if(!URLUtil.isNetworkUrl(path)){
return path;
}
else
{
URL url=new URL(path);
URLConnection connection=url.openConnection();
connection.connect();
InputStream stream=connection.getInputStream();
if(stream==null)
throw new RuntimeException("Stream is empty");
File temp=File.createTempFile("mediaPlayerTemp","data");
temp.deleteOnExit();
String tempPath=temp.getAbsolutePath();
FileOutputStream fos=new FileOutputStream(temp);
byte []buf=new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
fos.write(buf, 0, numread);
} while (true);
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
fos.close();
return tempPath;
}
}