Recording and playing audio. App Make app AudioFun with 4 buttons – Start recording (id=StartRecording) – Stop recording (id=StopRecording) – Start playback.

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 4 Overview Overview of Sensors Programming Tutorial 1: Tracking location with GPS and Google.
Advertisements

Services. Application component No user interface Two main uses Performing background processing Supporting remote method execution.
SQLite. Command line sqlite3 The command line sqlite3 is not installed on all devices To install, get sqlite3 from web page, and $ adb push sqlite3 /sdcard/
10.2. Audio with SD Card Prof. Oum Saokosal Master of Engineering in Information Systems, South Korea
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Cosc 5/4730 Multimedia Part 1: Audio media. PLAYING AUDIO Android android.media.
Group 8: Dylan Lentini (AE), Mandy Minuti (WSE), Jean Paul Galea (TL)
Recording and playing audio. Approaches For playing audio – Play a stream with media player Easiest Works with compressed files Works with urls and files.
Chapter 6 Jam! Implementing Audio in Android Apps.
Chapter 6: Jam! Implementing Audio in Android Apps.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface.
Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface.
Get android development environment running. Install – Get and install JDK 5 or 6 (see link in the.
Exception examples. import java.io.*; import java.util.*; class IO { private String line; private StringTokenizer tokenizer; public void newline(DataInputStream.
Debugging Android Applications
Video upload and streaming
CS371m - Mobile Computing Audio.
Getting Started with Android Programming Note: if you have already installed android development tools, please check that you have the same version as.
Android Programming. Outline Preparation Create new project Build and Run a project Debug a project Deploy on devices.
Basic Audio and Video. Audio Primary components – Audio file stored in ‘raw’ folder within ‘res’ folder ‘raw’ directory must be created while many formats.
Multimedia.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
CS378 - Mobile Computing Anatomy of an Android App and the App Lifecycle.
Android development the first app. Andoid vs iOS which is better? Short answer: neither Proponents on both sides For an iOS side, see this article on.
CS5103 Software Engineering Lecture 08 Android Development II.
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
UFCFX5-15-3Mobile Device Development Android Development SDKs and Publishing.
Testing with Android Part I of II. Android Testing Framework Based on JUnit The Android JUnit extensions provide component-specific test case classes.
Java Android-8 Imran Shafi. Lecture Contents  Debugging Android Projects  Java/XML Errors  Debugger  Logcat Utility  Android Debug Bridge (adb) 
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal.
Creating Multimedia Interaction with Windows Media Technologies 7.
20-Oct-15 Java 7. The diamond operator Instead of HashMap > map = new HashMap >(); you can now say HashMap > map = new HashMap; In fact, if you don’t.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
Android Security Auditing Slides and projects at samsclass.info.
Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. An application.
Introduction to Programming G50PRO University of Nottingham Unit 11 : Files Input/Ouput Paul Tennent
Camera. Make new project – CameraFun – Give permission to use camera write_external _storage Make two buttons – id takePictureButton – id showLastPicButton.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Persistence Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
Mobile Programming Lecture 11 Animation and TraceView.
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
New Activity On Button Click via Intent. App->res->Layout->Blank Activity A new xml file is created and a new class is added to java folder. In manifest.xml.
Activities and Intents Richard S. Stansbury 2015.
CS378 - Mobile Computing Audio.
Multimedia. Audio,vedio and Images End user typically refer to vedio/audio using the respective file format MP4 or AVI(audio vedio interleave) Developer.
Files and Serialization. Files Used to transfer data to and from secondary storage.
Cosc 5/4735 YouTube API. YouTube The YouTube Android Player API enables you to incorporate video playback functionality into your Android applications.
David Sutton SMS TELEPHONY IN ANDROID. OUTLINE  This week’s exercise, an SMS Pub Quiz  Simulating telephony on an emulator  Broadcast Intents and broadcast.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
CS 61B Data Structures and Programming Methodology July 7, 2008 David Sun.
CS371m - Mobile Computing Audio. Audio on Device Devices have multiple audio streams: – music, alarms, notifications, incoming call ringer, in call volume,
Audio and Haptic Feedback
SOP for CSD Log Capture Tool V0.1_
Android Application Audio 1.
Multimedia.
Data Storage: Part 2 (File System)
GUI Programming Fundamentals
Lecture 9: ADB Topics: Basic ADB Commands.
Android – Read/Write to External Storage
Recap: Android Components
CIS 470 Mobile App Development
CS5103 Software Engineering
Exceptions Complicate Code
Exceptions Complicate Code
Exceptions Complicate Code
Lecture 11: ADB Topics: Basic ADB Commands.
CA16R405 - Mobile Application Development (Theory)
Presentation transcript:

Recording and playing audio

App Make app AudioFun with 4 buttons – Start recording (id=StartRecording) – Stop recording (id=StopRecording) – Start playback (id=StartPlayback) – Stop playback (id=StopPlayback) Include permission to Record Audio There is no permission to play on audio device. – So a malicious app could play a scary noise in the middle of the night! Include three class attributes for this activity – final private static String RECORDED_FILE = "/audio.mp4“; – MediaRecorder audioListener; – MediaPlayer player; – String pathForAppFiles;

Recording (1) Make onClickListener for StartRecording Get MediaRecorder – if (audioListener!=null) audioListener.release(); – if (audioListener == null) { audioListener = new MediaRecorder(); } Get path for file – Note: each app runs in its own VM, with its own private directory and files. The SDK provides several tools for accessing the apps directory and files – The apps directory is at /data/data/ – Files are at /data/data/ /files FileOutputStream fos; // in java.io.FileOutputStream fos = Context.openFileOutput(“filename.txt”,MODE_PRIVATE); // opens file /data/data/ /files\filename.txt for writing – similarly FileInputStream fis; // in java.io.FileOutputStream fis = Context.openFileInput(“filename.txt”); // opens file /data/data/ /files\filename.txt for reading MediaRecorder and MediaPlayer need the full path – String pathForAppFiles = getFilesDir().getAbsolutePath(); // returns /data/data/ /files – pathForAppFiles += RECORDED_FILE; // file name with full path

logging The SDK provides logging – Log.e(tag, string) – E.g., Log.e(“Debug Info”,”Set file name”); – Or Log.e(“file name”, pathForAppFiles); – The log can be seen from the DDMS – Or from the command line C:\android\android-sdk-windows\platform-tools> adb –d logcat C:\android\android-sdk-windows\platform-tools> adb –e logcat

Set up media recorder audioListener.setAudioSource(MediaRecorder.AudioSource.MIC); – Options instead of MIC : CAMCORDER Microphone audio source with same orientation as camera if available, the main device microphone otherwise DEFAULT MIC Microphone audio source VOICE_CALL Voice call uplink + downlink audio source VOICE_DOWNLINK Voice call downlink (Rx) audio source VOICE_RECOGNITION Microphone audio source tuned for voice recognition if available, behaves like DEFAULT otherwise. VOICE_UPLINK Voice call uplink (Tx) audio source audioListener.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); – options DEFAULT MPEG_4: MPEG4 media file format RAW_AMR: Good for speech. Not sure if this is supported. Documentation says “ToDo: change link when AMR_NB is exposed. “ THREE_GPP :3GPP media file format audioListener.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); – options AMR_NB : AMR (Narrowband) audio codec, for speech DEFAULT audioListener.setOutputFile(pathForAppFiles);

Record try { audioListener.prepare(); audioListener.start(); } catch (Exception e) { Log.e("Audio", "Failed to prepare and start audio recording", e); } stopButton.setVisibility(View.VISIBLE); recordButton.setVisibility(View.GONE); playButton.setVisibility(View.GONE);

Stop recording Make onClickListener for stopRecordingButton if (audioListener == null) return; audioListener.stop(); audioListener.release(); audioListener = null; Log entry Log.e("Debug Info","Stopped rcording file"+pathForAppFiles); Make nice buttons stopButton.setVisibility(View.GONE); recordButton.setVisibility(View.VISIBLE); playButton.setVisibility(View.VISIBLE); Try it Run on device or emulator emulator is slow, so the quality if bad Get file from emulator using the DDMS and play in quickTime Get file from device via adb adb -d pull /data/data/edu.udel.eleg454.AudioFun/files/audio.mp4 c:\audio.mp4

playback Get clean MediaPlayer if (player!=null) player.release(); if (player == null) { player = new MediaPlayer (); } Get file Problem: the file does not have the correct permissions. See adb shell … ls –l There are several ways to fix this. Use the file descriptor from when the file was created. But what if we want to play a file that was not created when we run the app this time Change permissions with chmod (easiest option, but Android might not support exec() in the future!) String audioFilePath = getFilesDir().getAbsolutePath(); audioFilePath += RECORDED_FILE; Log.d("Audio filename:",audioFilePath ); String command = "chmod 666 " + audioFilePath.toString(); try { Runtime.getRuntime().exec(command); } catch (IOException e1) { Log.e("SetPermissions", "Couldn't set permissions", e1); } Change permissions with java Before recording, FileOutputStream fos; try { fos = openFileOutput(RECORDED_FILE, Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE); fos.close(); } catch (FileNotFoundException e1) { Log.e("audio file error","could not open file"); return; } catch (IOException e) { Log.e("audio file error","could not close the file"); return; } Get file name

playback try { player.setDataSource(audioFilePath); player.prepare(); player.start(); } catch (Exception e) { Log.e("Audio", "Playback failed.", e); } stopPlaybackButton.setVisibility(View.VISIBLE); recordButton.setVisibility(View.GONE); playButton.setVisibility(View.GONE); Nice buttons

Stop playback stopPlayback.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (player == null)return; player.stop(); player.release(); player = null; stopPlayback.setVisibility(View.GONE); record.setVisibility(View.VISIBLE); play.setVisibility(View.VISIBLE); } protected void onDestroy() { super.onDestroy(); if (audioListener!=null) audioListener.release(); if (player!=null) player.release(); } Also,