mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 07:02:51 +08:00
Added streaming support for audio
This commit is contained in:
parent
c96e369351
commit
cb90eb73d7
@ -315,7 +315,7 @@ var Device = {
|
||||
return Device.storage.result;
|
||||
},
|
||||
testExistence: function(file){
|
||||
Device.storage.result = window.DroidGap.testDirOrFileExists(file);
|
||||
Device.storage.result = window.DroidGap.testFileExists(file);
|
||||
return Device.storage.result;
|
||||
},
|
||||
delFile: function(file){
|
||||
@ -329,7 +329,7 @@ var Device = {
|
||||
createDir: function(file){
|
||||
Device.storage.result = window.DroidGap.createDirectory(file);
|
||||
return Device.storage.result;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
|
@ -222,7 +222,6 @@ addLoadEvent(initGap);
|
||||
</div>
|
||||
|
||||
<div id="geo" title="Geo Location" class="panel" >
|
||||
|
||||
<h2>Change Location</h2>
|
||||
<fieldset>
|
||||
<div class="row">
|
||||
@ -236,13 +235,10 @@ addLoadEvent(initGap);
|
||||
<div class="row">
|
||||
<a class="button blueButton" type="submit" onclick="updateLocation();">Update Location</a>
|
||||
</div>
|
||||
|
||||
</fieldset>
|
||||
|
||||
</div>
|
||||
|
||||
<div id="readgeo" title="Read Location" class="panel" >
|
||||
|
||||
<h2>Read Location</h2>
|
||||
<fieldset>
|
||||
<div class="row">
|
||||
@ -254,11 +250,9 @@ addLoadEvent(initGap);
|
||||
<input disabled="enable" name="lon" id="userlon" value="" type="text"/>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
</div>
|
||||
|
||||
<div id="setintervals" title="Set Intervals" class="panel" >
|
||||
|
||||
<h2>Change Intervals</h2>
|
||||
<fieldset>
|
||||
<div class="row">
|
||||
@ -272,13 +266,10 @@ addLoadEvent(initGap);
|
||||
<div class="row">
|
||||
<a class="button blueButton" type="submit" onclick="userSetIntervals();">Set Intervals</a>
|
||||
</div>
|
||||
|
||||
</fieldset>
|
||||
|
||||
</div>
|
||||
|
||||
<div id="accel" title="Accelerometer" class="panel">
|
||||
|
||||
<h2>Accelerometer</h2>
|
||||
<fieldset>
|
||||
<div class="row">
|
||||
@ -339,6 +330,7 @@ addLoadEvent(initGap);
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
|
||||
<div id="esm" title="Storage" class="panel" >
|
||||
<h2>Storage Management</h2>
|
||||
<fieldset>
|
||||
@ -401,8 +393,8 @@ addLoadEvent(initGap);
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
|
||||
<div id="pi" title="Phone Information" class="panel">
|
||||
|
||||
<fieldset>
|
||||
<div class="row">
|
||||
<label>Phone NO.</label>
|
||||
@ -423,8 +415,7 @@ addLoadEvent(initGap);
|
||||
<div class="row">
|
||||
<label>Time Zone</label>
|
||||
<input disabled="enabled" name="tzid" id="tzid" value="" type="text" style="width: 30ex"></input>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
|
||||
|
@ -2,11 +2,16 @@ package com.nitobi.phonegap;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.media.MediaPlayer.OnErrorListener;
|
||||
import android.media.MediaRecorder;
|
||||
import android.media.MediaPlayer.OnBufferingUpdateListener;
|
||||
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 boolean isRecording = false;
|
||||
MediaPlayer mPlayer;
|
||||
@ -33,11 +38,9 @@ public class AudioHandler implements OnCompletionListener {
|
||||
}
|
||||
|
||||
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);
|
||||
f.renameTo(new File("/sdcard" + file));
|
||||
System.out.println(this.recording);
|
||||
System.out.println(file);
|
||||
}
|
||||
|
||||
public void stopRecording(){
|
||||
@ -57,14 +60,25 @@ public class AudioHandler implements OnCompletionListener {
|
||||
try {
|
||||
mPlayer = new MediaPlayer();
|
||||
isPlaying=true;
|
||||
mPlayer.setDataSource("/sdcard/" + file);
|
||||
mPlayer.prepare();
|
||||
mPlayer.setOnCompletionListener(this);
|
||||
mPlayer.start();
|
||||
Log.d("Audio startPlaying", "audio: " + file);
|
||||
if (isStreaming(file))
|
||||
{
|
||||
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(); }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void stopPlaying() {
|
||||
if (isPlaying) {
|
||||
mPlayer.stop();
|
||||
@ -73,6 +87,7 @@ public class AudioHandler implements OnCompletionListener {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCompletion(MediaPlayer mPlayer) {
|
||||
mPlayer.stop();
|
||||
mPlayer.release();
|
||||
@ -86,17 +101,57 @@ public class AudioHandler implements OnCompletionListener {
|
||||
} else { return(-1); }
|
||||
}
|
||||
|
||||
private boolean isStreaming(String file)
|
||||
{
|
||||
if (file.contains("http://")) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public long getDuration(String file) {
|
||||
long duration = -2;
|
||||
if (isPlaying==false) {
|
||||
if (!isPlaying & !isStreaming(file)) {
|
||||
try {
|
||||
mPlayer = new MediaPlayer();
|
||||
mPlayer.setDataSource("/sdcard/" + file);
|
||||
mPlayer.prepare();
|
||||
duration = mPlayer.getDuration();
|
||||
mPlayer.release();
|
||||
} catch (Exception e) { e.printStackTrace(); return(-1); }
|
||||
return duration;
|
||||
} else { return -1; }
|
||||
} catch (Exception e) { e.printStackTrace(); return(-3); }
|
||||
} else
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import android.os.Environment;
|
||||
|
||||
public class DirectoryManager {
|
||||
|
||||
protected boolean isDirtoryOrFileExists (String name){
|
||||
protected boolean testFileExists (String name){
|
||||
boolean status;
|
||||
if ((testSaveLocationExists())&&(!name.equals(""))){
|
||||
File path = Environment.getExternalStorageDirectory();
|
||||
@ -41,7 +41,7 @@ public class DirectoryManager {
|
||||
return status;
|
||||
}
|
||||
|
||||
protected boolean deleteDir (String fileName){
|
||||
protected boolean deleteDirectory(String fileName){
|
||||
boolean status;
|
||||
SecurityManager checker = new SecurityManager();
|
||||
|
||||
@ -75,7 +75,7 @@ public class DirectoryManager {
|
||||
return status;
|
||||
|
||||
}
|
||||
protected boolean deleteFile (String fileName){
|
||||
protected boolean deleteFile(String fileName){
|
||||
boolean status;
|
||||
SecurityManager checker = new SecurityManager();
|
||||
|
||||
|
@ -248,38 +248,42 @@ public class PhoneGap{
|
||||
http.get(url, file);
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO: FIX! These should not be returning arbitrary Strings!
|
||||
*/
|
||||
public String testSaveLocationExists(){
|
||||
if (fileManager.testSaveLocationExists())
|
||||
return "SD Card available";
|
||||
else
|
||||
return "SD Card unavailable";
|
||||
}
|
||||
public String testDirOrFileExists(String file){
|
||||
if (fileManager.isDirtoryOrFileExists(file))
|
||||
public String testFileExists(String file){
|
||||
if (fileManager.testFileExists(file))
|
||||
return "File exists";
|
||||
else
|
||||
return "No this file";
|
||||
}
|
||||
|
||||
public String deleteDirectory (String dir){
|
||||
if (fileManager.deleteDir(dir))
|
||||
if (fileManager.deleteDirectory(dir))
|
||||
return "Completed";
|
||||
else
|
||||
return "Not completed";
|
||||
}
|
||||
|
||||
public String deleteFile (String file){
|
||||
if (fileManager.deleteFile(file))
|
||||
return "Completed";
|
||||
else
|
||||
return "Not completed";
|
||||
}
|
||||
public String createDirectory(String dir){
|
||||
if (fileManager.createDirectory(dir))
|
||||
|
||||
public String createDirectory(String dir){
|
||||
if (fileManager.createDirectory(dir))
|
||||
return "Completed";
|
||||
else
|
||||
return "Not completed";
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* AUDIO
|
||||
@ -326,21 +330,25 @@ public class PhoneGap{
|
||||
(TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
return(tm.getLine1Number());
|
||||
}
|
||||
|
||||
public String getVoiceMailNumber() {
|
||||
TelephonyManager tm =
|
||||
(TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
return(tm.getVoiceMailNumber());
|
||||
}
|
||||
|
||||
public String getNetworkOperatorName(){
|
||||
TelephonyManager tm =
|
||||
(TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
return(tm.getNetworkOperatorName());
|
||||
}
|
||||
|
||||
public String getSimCountryIso(){
|
||||
TelephonyManager tm =
|
||||
(TelephonyManager)mCtx.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
return(tm.getSimCountryIso());
|
||||
}
|
||||
|
||||
public String getTimeZoneID() {
|
||||
TimeZone tz = TimeZone.getDefault();
|
||||
return(tz.getID());
|
||||
|
Loading…
Reference in New Issue
Block a user