Download presentation
Presentation is loading. Please wait.
Published byDaniela Malone Modified over 9 years ago
1
Basic Audio and Video
2
Audio
3
Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats supported,.mp3 is most stable across devices – Instance of MediaPlayer android.media.MediaPlayer key methods – create » static convenience method for instantiation and preparation – start
4
Sample code - MediaPlayer MediaPlayer audioPlayer; audioPlayer = MediaPlayer.create(this, R.raw.audio_file_name); //no extension on file name audioPlayer.setAudioStream(AudioManager.STREAM_MUSIC); audioPlayer.start();
5
MediaPlayer – coding issues Other information – audioPlayer.stop(), pause(), and resume() audio does not automatically stop if activity pauses only if application stops – audioPlayer.release() should be called when done MediaPlayers can be expensive frees resources – Can use a BroadcastReceiver to allow hardware control (e.g. play, pause, skip, etc. on headset) – Can use Service to continue playback when App stops
6
MediaPlayer – coding issues One of the prepare() methods must be called – prepare() method called automatically by create() method OK if using a file – prepareAsync () method cannot use create() convenience method starts MediaPlayer in a new thread better for streaming audio app does not ‘hang’ if waiting for buffered data
7
Video
8
Primary components – Video file.3gp file – new standard supported by 3G phones can be stored in res/raw folder – don’t supply extension in call can be stored within device’s file system (i.e. SD card) – VideoView widget typical widget in xml layout file key methods – setVideoPath (full path and file name with extension) – start
9
Sample code - VideoView //Video stored on SD Card VideoView vv = (VideoView)findViewById(R.id.vvVideo); vv.setVideoPath("/sdcard/filename.3gp"); vv.start(); or: //Video stored in raw directory VideoView vv = (VideoView)findViewById(R.id.vvVideo); vv.setVideoPath("android.resource://edu.csci153/raw/filename"); vv.start();
10
Internal file system Emulator or attached device
11
Internal file system Display files within eclipse – Window…Show View…Other…File Explorer avoid ‘data’ directory – emulator allows access – system directory – restricted access on an actual device Copy files to/from device within eclipse – icons in File Explorer copy to: ‘Push a file onto the device’ icon copy from: ‘Pull a file from the device’ icon delete from device: ‘Delete the selection’ icon
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.