Audio:
MediaPlayer is the easier way, if you just want to play an audio file in the background, somewhere in an appliaction. There are no ui controls here, but of course you can use MediaPlayer.stop(), play(), seekTo() ,etc. Just bind the needed functions to a button, gesture, or event. As you can see, it also throws a lot of exceptions, which you need to catch.
MediaPlayer is the easier way, if you just want to play an audio file in the background, somewhere in an appliaction. There are no ui controls here, but of course you can use MediaPlayer.stop(), play(), seekTo() ,etc. Just bind the needed functions to a button, gesture, or event. As you can see, it also throws a lot of exceptions, which you need to catch.
- //set up MediaPlayer
- MediaPlayer mp = new MediaPlayer();
- try {
- mp.setDataSource(path+"/"+fileName);
- // TODO Auto-generated catch block
- e.printStackTrace();
- // TODO Auto-generated catch block
- e.printStackTrace();
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- mp.prepare();
- // TODO Auto-generated catch block
- e.printStackTrace();
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- mp.start();
- }
Video:
In this example, I open a video file, and decide if I want it to autoplay after it is loaded.
You propably want to store your video files, on the sd card. You can get the path to the sd card via: Environment.getExternalStorageDirectory().
- //get current window information, and set format, set it up differently, if you need some special effects
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
- //the VideoView will hold the video
- VideoView videoHolder = new VideoView(this);
- //MediaController is the ui control howering above the video (just like in the default youtube player).
- videoHolder.setMediaController(new MediaController(this));
- //assing a video file to the video holder
- videoHolder.setVideoURI(Uri.parse(path+"/"+fileName));
- //get focus, before playing the video.
- videoHolder.requestFocus();
- if(autoplay){
- videoHolder.start();
- }
- }
0 comments :
Post a Comment