mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-20 23:56:20 +08:00
android Audio recording/playback and progress bar info
This commit is contained in:
parent
a7f45f18d6
commit
c676865cfb
@ -13,6 +13,7 @@
|
||||
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_SMS" />
|
||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name"
|
||||
android:debuggable="true">
|
||||
|
@ -306,7 +306,29 @@ var Device = {
|
||||
get: function(url, file) {
|
||||
window.DroidGap.httpGet(url, file);
|
||||
}
|
||||
},
|
||||
|
||||
audio: {
|
||||
startRecording: function(file) {
|
||||
window.DroidGap.startRecordingAudio(file);
|
||||
},
|
||||
stopRecording: function() {
|
||||
window.DroidGap.stopRecordingAudio();
|
||||
},
|
||||
startPlaying: function(file) {
|
||||
window.DroidGap.startPlayingAudio(file);
|
||||
},
|
||||
stopPlaying: function() {
|
||||
window.DroidGap.stopPlayingAudio();
|
||||
},
|
||||
getCurrentPosition: function() {
|
||||
return window.DroidGap.getCurrentPositionAudio();
|
||||
},
|
||||
getDuration: function(file) {
|
||||
return window.DroidGap.getDurationAudio(file);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function gotLocation(lat, lon) {
|
||||
|
@ -105,10 +105,32 @@ function onReceiveSms(number, message)
|
||||
http = function(func)
|
||||
{
|
||||
if (func == 'get') {
|
||||
Device.http.get($('httpGetUrl').value, $('httpGetFile').value)
|
||||
Device.http.get($('httpGetUrl').value, $('httpGetFile').value);
|
||||
}
|
||||
}
|
||||
|
||||
audio = function(func)
|
||||
{
|
||||
if (func == 'startPlaying') {
|
||||
Device.audio.startPlaying($('audioFile').value);
|
||||
} else
|
||||
if (func == 'stopPlaying') {
|
||||
Device.audio.stopPlaying();
|
||||
} else
|
||||
if (func == 'startRecording') {
|
||||
Device.audio.startRecording($('audioFile').value);
|
||||
} else
|
||||
if (func == 'stopRecording') {
|
||||
Device.audio.stopRecording();
|
||||
} else
|
||||
if (func == 'getCurrentPosition') {
|
||||
$('posdur').value = Device.audio.getCurrentPosition();
|
||||
} else
|
||||
if (func == 'getDuration') {
|
||||
$('posdur').value = Device.audio.getDuration($('audioFile').value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
addLoadEvent(initGap);
|
||||
@ -130,6 +152,7 @@ addLoadEvent(initGap);
|
||||
<li><a href="#vibration" onclick="Device.vibrate();">Vibration</a></li>
|
||||
<li><a href="#vibration" onclick="Device.playSound('on.mp3")">Play Sound</a></li>
|
||||
<li><a href="#photo" onclick="Device.Image.getFromPhotoLibrary();">Photo...</a></li>
|
||||
<li><a href="#audio" onclick="audio();">Audio...</a></li>
|
||||
<li><a href="#notification" onclick="notification();">Notification...</a></li>
|
||||
<li><a href="#http" onclick="http();">HTTP...</a></li>
|
||||
<li><a href="http://phonegap.com/" target="_self">About</a></li>
|
||||
@ -289,4 +312,39 @@ addLoadEvent(initGap);
|
||||
</fieldset>
|
||||
</div>
|
||||
|
||||
<div id="audio" title="audio" class="panel">
|
||||
<h2>Audio</h2>
|
||||
<fieldset>
|
||||
<div class="row">
|
||||
<label>AudioFile:</label>
|
||||
<input type=text name="audioFile" id="audioFile" size=60 maxlength=2048 value="/hullophonegap.mp3" style="width: 30ex"></input>
|
||||
</div>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<div class="row">
|
||||
<a class="button leftButton" type="submit" onclick="audio('startRecording');">startRecording</a>
|
||||
<a class="button blueButton" type="submit" onclick="audio('stopRecording');">stopRecording</a>
|
||||
</div>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<div class="row">
|
||||
<a class="button leftButton" type="submit" onclick="audio('startPlaying');">startPlaying</a>
|
||||
<a class="button blueButton" type="submit" onclick="audio('stopPlaying');">stopPlaying</a>
|
||||
</div>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<div class="row">
|
||||
<label>info:</label>
|
||||
<input disabled="enabled" id="posdur" type="text" />
|
||||
</div>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<div class="row">
|
||||
<a class="button leftButton" type="submit" onclick="audio('getDuration');">getDuration</a>
|
||||
<a class="button blueButton" type="submit" onclick="audio('getCurrentPosition');">getPosition</a>
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
|
||||
|
||||
<div id="preloader"></div></body></html>
|
||||
|
BIN
bin/DroidGap.apk
BIN
bin/DroidGap.apk
Binary file not shown.
BIN
bin/classes.dex
BIN
bin/classes.dex
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
102
src/com/nitobi/phonegap/AudioHandler.java
Normal file
102
src/com/nitobi/phonegap/AudioHandler.java
Normal file
@ -0,0 +1,102 @@
|
||||
package com.nitobi.phonegap;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import android.media.MediaPlayer;
|
||||
import android.media.MediaRecorder;
|
||||
import android.media.MediaPlayer.OnCompletionListener;
|
||||
|
||||
public class AudioHandler implements OnCompletionListener {
|
||||
private MediaRecorder recorder;
|
||||
private boolean isRecording = false;
|
||||
MediaPlayer mPlayer;
|
||||
private boolean isPlaying = false;
|
||||
private String recording;
|
||||
private String saveFile;
|
||||
|
||||
public AudioHandler(String file) {
|
||||
this.recording = file;
|
||||
}
|
||||
|
||||
public void startRecording(String file){
|
||||
if (!isRecording){
|
||||
saveFile=file;
|
||||
recorder = new MediaRecorder();
|
||||
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
|
||||
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
|
||||
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
|
||||
recorder.setOutputFile(this.recording);
|
||||
recorder.prepare();
|
||||
isRecording = true;
|
||||
recorder.start();
|
||||
}
|
||||
}
|
||||
|
||||
private void moveFile(String file) {
|
||||
/* this is just a hck that I will remove later */
|
||||
File f = new File (this.recording);
|
||||
f.renameTo(new File("/sdcard" + file));
|
||||
System.out.println(this.recording);
|
||||
System.out.println(file);
|
||||
}
|
||||
|
||||
public void stopRecording(){
|
||||
try{
|
||||
if((recorder != null)&&(isRecording))
|
||||
{
|
||||
isRecording = false;
|
||||
recorder.stop();
|
||||
recorder.release();
|
||||
}
|
||||
moveFile(saveFile);
|
||||
}catch (Exception e){e.printStackTrace();}
|
||||
}
|
||||
|
||||
public void startPlaying(String file) {
|
||||
if (isPlaying==false) {
|
||||
try {
|
||||
mPlayer = new MediaPlayer();
|
||||
isPlaying=true;
|
||||
mPlayer.setDataSource("/sdcard/" + file);
|
||||
mPlayer.prepare();
|
||||
mPlayer.setOnCompletionListener(this);
|
||||
mPlayer.start();
|
||||
} catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
}
|
||||
|
||||
public void stopPlaying() {
|
||||
if (isPlaying) {
|
||||
mPlayer.stop();
|
||||
mPlayer.release();
|
||||
isPlaying=false;
|
||||
}
|
||||
}
|
||||
|
||||
public void onCompletion(MediaPlayer mPlayer) {
|
||||
mPlayer.stop();
|
||||
mPlayer.release();
|
||||
isPlaying=false;
|
||||
}
|
||||
|
||||
public long getCurrentPosition() {
|
||||
if (isPlaying)
|
||||
{
|
||||
return(mPlayer.getCurrentPosition());
|
||||
} else { return(-1); }
|
||||
}
|
||||
|
||||
public long getDuration(String file) {
|
||||
long duration = -2;
|
||||
if (isPlaying==false) {
|
||||
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; }
|
||||
}
|
||||
}
|
@ -244,5 +244,46 @@ public class PhoneGap{
|
||||
HttpHandler http = new HttpHandler();
|
||||
http.get(url, file);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* AUDIO
|
||||
* TODO: Basic functions done but needs more work on error handling and call backs, remove record hack
|
||||
*/
|
||||
AudioHandler audio = new AudioHandler("/sdcard/tmprecording.mp3");
|
||||
public void startRecordingAudio(String file)
|
||||
{
|
||||
/* for this to work the recording needs to be specified in the constructor,
|
||||
* a hack to get around this, I'm moving the recording after it's complete
|
||||
*/
|
||||
audio.startRecording(file);
|
||||
}
|
||||
|
||||
public void stopRecordingAudio()
|
||||
{
|
||||
audio.stopRecording();
|
||||
}
|
||||
|
||||
public void startPlayingAudio(String file)
|
||||
{
|
||||
audio.startPlaying(file);
|
||||
}
|
||||
|
||||
public void stopPlayingAudio()
|
||||
{
|
||||
audio.stopPlaying();
|
||||
}
|
||||
|
||||
public long getCurrentPositionAudio()
|
||||
{
|
||||
System.out.println(audio.getCurrentPosition());
|
||||
return(audio.getCurrentPosition());
|
||||
}
|
||||
|
||||
public long getDurationAudio(String file)
|
||||
{
|
||||
System.out.println(audio.getDuration(file));
|
||||
return(audio.getDuration(file));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user