Last active
April 19, 2016 10:32
-
-
Save tony1223/5731812 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mcall.service.aidl; | |
interface IServiceWorkRecorder { | |
boolean start(int workID); | |
void pause(); | |
String stop(); | |
int getDuration(); | |
int getCurrentPosition(); | |
int getCurrentWorkID(); | |
boolean isRecording(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyActivity extends Activity { | |
private IServiceWorkRecorder recorder = null; | |
private ServiceConnection conn = new ServiceConnection() { | |
public void onServiceDisconnected(ComponentName name) { | |
recorder = null; | |
} | |
public void onServiceConnected(ComponentName name, IBinder service) { | |
recorder = IServiceWorkRecorder.Stub.asInterface(service); | |
recorderReady(); | |
} | |
}; | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
Intent intent = new Intent(MyActivity.this, RecordService.class); | |
bindService(intent, conn, Context.BIND_AUTO_CREATE); | |
startService(intent); | |
//do something else | |
} | |
private void recorderReady() { | |
//recorder ready | |
} | |
public void statRecord(){ | |
recorder.start(requestWorkID); | |
} | |
public void stopRecord(){ | |
try { | |
final String filepath = recorder.stop(); | |
if (filepath == null) { | |
//show error | |
return; | |
} | |
//do the file upload job ...etc | |
} catch (RemoteException e) { | |
// FIXME : handle exception | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.Date; | |
import android.app.Notification; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.media.MediaRecorder; | |
import android.os.IBinder; | |
import android.os.RemoteException; | |
import android.util.Log; | |
import com.mcall.R; | |
import com.mcall.activity.SheetActivity; | |
import com.mcall.service.aidl.IServiceWorkRecorder; | |
import com.mcall.util.FileUtil; | |
public class RecordService extends Service { | |
public static final int NOTIFICATION_ID = 100000; | |
private MediaRecorder mRecorder; | |
private IServiceWorkRecorder.Stub stub = new IServiceWorkRecorder.Stub() { | |
private String currentFilePath = null; | |
private int currentWorkID; | |
private boolean recording = false; | |
private String genRecordFilePath(int workID) { | |
return FileUtil.getFilePath(workID + "_recording_" + (new Date().getTime()) + ".3gp"); | |
} | |
public boolean isRecording() throws RemoteException { | |
return recording; | |
}; | |
public boolean start(int workID) throws RemoteException { | |
recording = true; | |
currentFilePath = genRecordFilePath(workID); | |
currentWorkID = workID; | |
mRecorder = new MediaRecorder(); | |
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); | |
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); | |
mRecorder.setOutputFile(currentFilePath); | |
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); | |
postNotification(); | |
try { | |
mRecorder.prepare(); | |
} catch (IOException e) { | |
Log.e("error", "prepare() failed"); | |
return false; | |
} | |
try { | |
mRecorder.start(); | |
} catch (IllegalStateException ex) { | |
Log.e("error", "start() failed,錄音時發生錯誤,可能是 SD 卡無法寫入"); | |
return false; | |
} | |
// TODO: handle exception | |
return true; | |
} | |
@SuppressWarnings("deprecation") | |
private void postNotification(){ | |
// 初始化Notification Manager | |
NotificationManager gNotMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
Notification tBNot = new Notification(R.drawable.ic_launcher, "錄音中", System.currentTimeMillis()); | |
Intent notificationIntent = new Intent(RecordService.this, MyActivity.class); | |
notificationIntent.putExtra("RequestWorkID", currentWorkID); | |
PendingIntent contentIntent = PendingIntent.getActivity(RecordService.this, NOTIFICATION_ID, | |
notificationIntent, 0); | |
tBNot.setLatestEventInfo(RecordService.this, | |
// TODO use hashID instead. | |
"錄音中", "", contentIntent); | |
gNotMgr.notify(NOTIFICATION_ID, tBNot); | |
} | |
@Override | |
public void pause() throws RemoteException { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public String stop() throws RemoteException { | |
NotificationManager gNotMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
gNotMgr.cancel(NOTIFICATION_ID); | |
recording = false; | |
if (mRecorder != null) { | |
mRecorder.stop(); | |
mRecorder.release(); | |
mRecorder = null; | |
return currentFilePath; | |
} | |
return null; | |
} | |
@Override | |
public int getDuration() throws RemoteException { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public int getCurrentPosition() throws RemoteException { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public int getCurrentWorkID() throws RemoteException { | |
return currentWorkID; | |
} | |
}; | |
public IBinder onBind(Intent intent) { | |
return stub; | |
} | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
return super.onStartCommand(intent, flags, startId); | |
} | |
public void onDestroy() { | |
super.onDestroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment