App Development for Smart Devices CS 495/595 - Fall 2013 Lec #7: Audio, Video & Telephony Try It Out Tamer Nadeem Dept. of Computer Science
Trt It Out Example - SoundPool Example - VideoView Page 2 Fall 2013 CS 495/595 - App Development for Smart Devices
SoundPool Android provides two API's for playing sounds: SoundPool and MediaPlayer. SoundPool can be used for small audio clips. It can repeat sounds and play several sounds simultaneously. The sound files played with SoundPool should not exceed 1 MB. SoundPool does load the file asynchronously. In recent Android SDK, it is possible to check if the loading is complete via OnLoadCompleteListener. Mediaplayer is better suited for longer music and movies. Page 3 Fall 2013 CS 495/595 - App Development for Smart Devices
Example of SoundPool Create an application that will start playing a sound once the finger touches the display. Create an Android project cs.edu.odu.cs495.soundpool" with the Activity "PlaySound Get a free sound effect from http://hamsterrepublic.com/ohrrpgce/free_sound_effects.html, put it into your "res/raw" folder under the name "sound1.ogg". main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="click on the screen to start playing" android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="fill_parent"> </TextView> </LinearLayout> Page 4 Fall 2013 CS 495/595 - App Development for Smart Devices
Example of SoundPool package edu.odu.cs.cs495.soundpool; import android.app.activity; import android.media.audiomanager; import android.media.soundpool; import android.media.soundpool.onloadcompletelistener; import android.os.bundle; import android.util.log; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; public class PlaySound extends Activity implements OnTouchListener { private SoundPool soundpool; private int soundid; boolean loaded = false; Page 5 Fall 2013 CS 495/595 - App Development for Smart Devices
Example of SoundPool /** Called when the activity is first created. */ @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); View view = findviewbyid(r.id.textview1); view.setontouchlistener(this); // Set the hardware buttons to control the music this.setvolumecontrolstream(audiomanager.stream_music); // Load the sound soundpool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); soundpool.setonloadcompletelistener(new OnLoadCompleteListener() { @Override public void onloadcomplete(soundpool soundpool, int sampleid, int status) { loaded = true; ); soundid = soundpool.load(this, R.raw.sound1, 1); Page 6 Fall 2013 CS 495/595 - App Development for Smart Devices
Example of SoundPool @Override public boolean ontouch(view v, MotionEvent event) { if (event.getaction() == MotionEvent.ACTION_DOWN) { // Getting the user sound settings AudioManager audiomanager = (AudioManager) getsystemservice(audio_service); float actualvolume = (float) audiomanager. getstreamvolume(audiomanager.stream_music); float maxvolume = (float) audiomanager. getstreammaxvolume(audiomanager.stream_music); float volume = actualvolume / maxvolume; // Is the sound loaded already? if (loaded) { soundpool.play(soundid, volume, volume, 1, 0, 1f); Log.e("Test", "Played sound"); return false; Page 7 Fall 2013 CS 495/595 - App Development for Smart Devices
Example of VideoView A simple example using VideoView to play 3gp from YouTube main.xml <?xml version="1.0" encoding="utf- 8"?> <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <VideoView android:id="@+id/myvideoview" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> Page 8 Fall 2013 CS 495/595 - App Development for Smart Devices
Example of VideoView package edu.odu.cs.cs495.myvideoview; import android.app.activity; import android.net.uri; import android.os.bundle; import android.widget.mediacontroller; import android.widget.videoview; public class MyVideoView extends Activity { String SrcPath = "rtsp://<replace WITH PATH TO YouTube video (3gp)>"; /** Called when the activity is first created. */ @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); VideoView myvideoview = (VideoView)findViewById(R.id.myvideoview); myvideoview.setvideouri(uri.parse(srcpath)); myvideoview.setmediacontroller(new MediaController(this)); myvideoview.requestfocus(); myvideoview.start(); Page 9 Fall 2013 CS 495/595 - App Development for Smart Devices