Added streaming support for audio

This commit is contained in:
rnvahey@gmail.com 2009-03-01 02:23:16 +07:00
parent c96e369351
commit cb90eb73d7
5 changed files with 91 additions and 37 deletions

View File

@ -315,7 +315,7 @@ var Device = {
return Device.storage.result; return Device.storage.result;
}, },
testExistence: function(file){ testExistence: function(file){
Device.storage.result = window.DroidGap.testDirOrFileExists(file); Device.storage.result = window.DroidGap.testFileExists(file);
return Device.storage.result; return Device.storage.result;
}, },
delFile: function(file){ delFile: function(file){
@ -329,7 +329,7 @@ var Device = {
createDir: function(file){ createDir: function(file){
Device.storage.result = window.DroidGap.createDirectory(file); Device.storage.result = window.DroidGap.createDirectory(file);
return Device.storage.result; return Device.storage.result;
} }
}, },

View File

@ -222,7 +222,6 @@ addLoadEvent(initGap);
</div> </div>
<div id="geo" title="Geo Location" class="panel" > <div id="geo" title="Geo Location" class="panel" >
<h2>Change Location</h2> <h2>Change Location</h2>
<fieldset> <fieldset>
<div class="row"> <div class="row">
@ -236,13 +235,10 @@ addLoadEvent(initGap);
<div class="row"> <div class="row">
<a class="button blueButton" type="submit" onclick="updateLocation();">Update Location</a> <a class="button blueButton" type="submit" onclick="updateLocation();">Update Location</a>
</div> </div>
</fieldset> </fieldset>
</div> </div>
<div id="readgeo" title="Read Location" class="panel" > <div id="readgeo" title="Read Location" class="panel" >
<h2>Read Location</h2> <h2>Read Location</h2>
<fieldset> <fieldset>
<div class="row"> <div class="row">
@ -254,11 +250,9 @@ addLoadEvent(initGap);
<input disabled="enable" name="lon" id="userlon" value="" type="text"/> <input disabled="enable" name="lon" id="userlon" value="" type="text"/>
</div> </div>
</fieldset> </fieldset>
</div> </div>
<div id="setintervals" title="Set Intervals" class="panel" > <div id="setintervals" title="Set Intervals" class="panel" >
<h2>Change Intervals</h2> <h2>Change Intervals</h2>
<fieldset> <fieldset>
<div class="row"> <div class="row">
@ -272,13 +266,10 @@ addLoadEvent(initGap);
<div class="row"> <div class="row">
<a class="button blueButton" type="submit" onclick="userSetIntervals();">Set Intervals</a> <a class="button blueButton" type="submit" onclick="userSetIntervals();">Set Intervals</a>
</div> </div>
</fieldset> </fieldset>
</div> </div>
<div id="accel" title="Accelerometer" class="panel"> <div id="accel" title="Accelerometer" class="panel">
<h2>Accelerometer</h2> <h2>Accelerometer</h2>
<fieldset> <fieldset>
<div class="row"> <div class="row">
@ -339,6 +330,7 @@ addLoadEvent(initGap);
</div> </div>
</fieldset> </fieldset>
</div> </div>
<div id="esm" title="Storage" class="panel" > <div id="esm" title="Storage" class="panel" >
<h2>Storage Management</h2> <h2>Storage Management</h2>
<fieldset> <fieldset>
@ -401,8 +393,8 @@ addLoadEvent(initGap);
</div> </div>
</fieldset> </fieldset>
</div> </div>
<div id="pi" title="Phone Information" class="panel"> <div id="pi" title="Phone Information" class="panel">
<fieldset> <fieldset>
<div class="row"> <div class="row">
<label>Phone NO.</label> <label>Phone NO.</label>
@ -423,8 +415,7 @@ addLoadEvent(initGap);
<div class="row"> <div class="row">
<label>Time Zone</label> <label>Time Zone</label>
<input disabled="enabled" name="tzid" id="tzid" value="" type="text" style="width: 30ex"></input> <input disabled="enabled" name="tzid" id="tzid" value="" type="text" style="width: 30ex"></input>
</div> </div>
</fieldset> </fieldset>
</div> </div>

View File

@ -2,11 +2,16 @@ package com.nitobi.phonegap;
import java.io.File; import java.io.File;
import android.media.AudioManager;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaRecorder; import android.media.MediaRecorder;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
public class AudioHandler implements OnCompletionListener { public class AudioHandler implements OnCompletionListener, OnPreparedListener, OnErrorListener {
private MediaRecorder recorder; private MediaRecorder recorder;
private boolean isRecording = false; private boolean isRecording = false;
MediaPlayer mPlayer; MediaPlayer mPlayer;
@ -33,11 +38,9 @@ public class AudioHandler implements OnCompletionListener {
} }
private void moveFile(String file) { private void moveFile(String file) {
/* this is just a hck that I will remove later */ /* this is a hack to save the file as the specified name */
File f = new File (this.recording); File f = new File (this.recording);
f.renameTo(new File("/sdcard" + file)); f.renameTo(new File("/sdcard" + file));
System.out.println(this.recording);
System.out.println(file);
} }
public void stopRecording(){ public void stopRecording(){
@ -57,14 +60,25 @@ public class AudioHandler implements OnCompletionListener {
try { try {
mPlayer = new MediaPlayer(); mPlayer = new MediaPlayer();
isPlaying=true; isPlaying=true;
mPlayer.setDataSource("/sdcard/" + file); Log.d("Audio startPlaying", "audio: " + file);
mPlayer.prepare(); if (isStreaming(file))
mPlayer.setOnCompletionListener(this); {
mPlayer.start(); Log.d("AudioStartPlaying", "Streaming");
// Streaming prepare async
mPlayer.setDataSource(file);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.prepareAsync();
} else {
Log.d("AudioStartPlaying", "File");
// Not streaming prepare synchronous, abstract base directory
mPlayer.setDataSource("/sdcard/" + file);
mPlayer.prepare();
}
mPlayer.setOnPreparedListener(this);
} catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); }
} }
} }
public void stopPlaying() { public void stopPlaying() {
if (isPlaying) { if (isPlaying) {
mPlayer.stop(); mPlayer.stop();
@ -73,6 +87,7 @@ public class AudioHandler implements OnCompletionListener {
} }
} }
@Override
public void onCompletion(MediaPlayer mPlayer) { public void onCompletion(MediaPlayer mPlayer) {
mPlayer.stop(); mPlayer.stop();
mPlayer.release(); mPlayer.release();
@ -86,17 +101,57 @@ public class AudioHandler implements OnCompletionListener {
} else { return(-1); } } else { return(-1); }
} }
private boolean isStreaming(String file)
{
if (file.contains("http://")) {
return true;
} else {
return false;
}
}
public long getDuration(String file) { public long getDuration(String file) {
long duration = -2; long duration = -2;
if (isPlaying==false) { if (!isPlaying & !isStreaming(file)) {
try { try {
mPlayer = new MediaPlayer(); mPlayer = new MediaPlayer();
mPlayer.setDataSource("/sdcard/" + file); mPlayer.setDataSource("/sdcard/" + file);
mPlayer.prepare(); mPlayer.prepare();
duration = mPlayer.getDuration(); duration = mPlayer.getDuration();
mPlayer.release(); mPlayer.release();
} catch (Exception e) { e.printStackTrace(); return(-1); } } catch (Exception e) { e.printStackTrace(); return(-3); }
return duration; } else
} else { return -1; } if (isPlaying & !isStreaming(file)) {
duration = mPlayer.getDuration();
} else
if (isPlaying & isStreaming(file)) {
try {
duration = mPlayer.getDuration();
} catch (Exception e) { e.printStackTrace(); return(-4); }
}else { return -1; }
return duration;
}
@Override
public void onPrepared(MediaPlayer mPlayer) {
if (isPlaying) {
mPlayer.setOnCompletionListener(this);
mPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener()
{
@Override
public void onBufferingUpdate(MediaPlayer mPlayer, int percent)
{
/* TODO: call back, e.g. update outer progress bar */
Log.d("AudioOnBufferingUpdate", "percent: " + percent);
}
});
mPlayer.start();
}
}
@Override
public boolean onError(MediaPlayer mPlayer, int arg1, int arg2) {
Log.e("AUDIO onError", "error " + arg1 + " " + arg2);
return false;
} }
} }

View File

@ -6,7 +6,7 @@ import android.os.Environment;
public class DirectoryManager { public class DirectoryManager {
protected boolean isDirtoryOrFileExists (String name){ protected boolean testFileExists (String name){
boolean status; boolean status;
if ((testSaveLocationExists())&&(!name.equals(""))){ if ((testSaveLocationExists())&&(!name.equals(""))){
File path = Environment.getExternalStorageDirectory(); File path = Environment.getExternalStorageDirectory();
@ -41,7 +41,7 @@ public class DirectoryManager {
return status; return status;
} }
protected boolean deleteDir (String fileName){ protected boolean deleteDirectory(String fileName){
boolean status; boolean status;
SecurityManager checker = new SecurityManager(); SecurityManager checker = new SecurityManager();
@ -75,7 +75,7 @@ public class DirectoryManager {
return status; return status;
} }
protected boolean deleteFile (String fileName){ protected boolean deleteFile(String fileName){
boolean status; boolean status;
SecurityManager checker = new SecurityManager(); SecurityManager checker = new SecurityManager();

View File

@ -248,38 +248,42 @@ public class PhoneGap{
http.get(url, file); http.get(url, file);
} }
/*
* TODO: FIX! These should not be returning arbitrary Strings!
*/
public String testSaveLocationExists(){ public String testSaveLocationExists(){
if (fileManager.testSaveLocationExists()) if (fileManager.testSaveLocationExists())
return "SD Card available"; return "SD Card available";
else else
return "SD Card unavailable"; return "SD Card unavailable";
} }
public String testDirOrFileExists(String file){ public String testFileExists(String file){
if (fileManager.isDirtoryOrFileExists(file)) if (fileManager.testFileExists(file))
return "File exists"; return "File exists";
else else
return "No this file"; return "No this file";
} }
public String deleteDirectory (String dir){ public String deleteDirectory (String dir){
if (fileManager.deleteDir(dir)) if (fileManager.deleteDirectory(dir))
return "Completed"; return "Completed";
else else
return "Not completed"; return "Not completed";
} }
public String deleteFile (String file){ public String deleteFile (String file){
if (fileManager.deleteFile(file)) if (fileManager.deleteFile(file))
return "Completed"; return "Completed";
else else
return "Not completed"; return "Not completed";
} }
public String createDirectory(String dir){
if (fileManager.createDirectory(dir)) public String createDirectory(String dir){
if (fileManager.createDirectory(dir))
return "Completed"; return "Completed";
else else
return "Not completed"; return "Not completed";
} }
/** /**
* AUDIO * AUDIO
@ -326,21 +330,25 @@ public class PhoneGap{
(TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE); (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
return(tm.getLine1Number()); return(tm.getLine1Number());
} }
public String getVoiceMailNumber() { public String getVoiceMailNumber() {
TelephonyManager tm = TelephonyManager tm =
(TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE); (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
return(tm.getVoiceMailNumber()); return(tm.getVoiceMailNumber());
} }
public String getNetworkOperatorName(){ public String getNetworkOperatorName(){
TelephonyManager tm = TelephonyManager tm =
(TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE); (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
return(tm.getNetworkOperatorName()); return(tm.getNetworkOperatorName());
} }
public String getSimCountryIso(){ public String getSimCountryIso(){
TelephonyManager tm = TelephonyManager tm =
(TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE); (TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
return(tm.getSimCountryIso()); return(tm.getSimCountryIso());
} }
public String getTimeZoneID() { public String getTimeZoneID() {
TimeZone tz = TimeZone.getDefault(); TimeZone tz = TimeZone.getDefault();
return(tz.getID()); return(tz.getID());