Monday, November 5, 2012

Android - Video\Audio Sample

Hello


Following is very simple sample for playing Audio and Video using external urls

Sample code - MediaPlayerTry.zip


Basic API is the class MediaPlayer

Audio Sample
  String url = "http://vprbbc.streamguys.net/vprbbc24.mp3";
 
  mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  try {
  mediaPlayer.setDataSource(url);
  mediaPlayer.prepare();
  mediaPlayer.start();
 } catch (IllegalArgumentException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (SecurityException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IllegalStateException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }


remarks :

  • patient is needed for prepare
  • mediaPlayer is a member type MediaPlayer
  • the player is stopped via stop()


Video Sample

Requires VideoView
String SrcPath = "rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQnhycnrJQ8qmRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYPj_hYjnq6uUTQw=/0/0/0/video.3gp";
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();

It is possible to stop the video via the VideoView widget


http://developer.android.com/guide/topics/media/mediaplayer.html

Supported Media Formats

Nathan

No comments:

Post a Comment