如何在android中找到录制文件大小

时间:2014-08-11 05:13:58

标签: android

*您正在开发一个应用程序。在那里录制来电和去电,它将显示在列表视图中。如何找到录制文件大小(例如kb或字节)。 任何人都可以为编程提供解决方案。

public class RecordService extends Service implements MediaRecorder.OnInfoListener,                                                           
MediaRecorder.OnErrorListener  
{
private static final String TAG = "CallRecorder";  
public static final String DEFAULT_STORAGE_LOCATION = "/sdcard/callrecorder";
private static final int RECORDING_NOTIFICATION_ID = 1;
private MediaRecorder recorder = null;
private boolean isRecording = false;
private File recording = null;;
private File makeOutputFile (SharedPreferences prefs)
{
    File dir = new File(DEFAULT_STORAGE_LOCATION);
    // test dir for existence and writeability
    if (!dir.exists()) {
        try {
            dir.mkdirs();
        } catch (Exception e) {
    Log.e("CallRecorder", "RecordService:makeOutputFile unable to create                           directory " + dir + ": " + e);
            Toast t = Toast.makeText(getApplicationContext(), "CallRecorder was unable      to create the directory " + dir + " to store recordings: " + e, Toast.LENGTH_LONG);
            t.show();
            return null;
        }
    } else {
        if (!dir.canWrite()) {
            Log.e(TAG, "RecordService:makeOutputFile does not have write permission for    directory: " + dir);
            Toast t = Toast.makeText(getApplicationContext(), "CallRecorder does not  have write permission for the directory directory " + dir + " to store recordings", Toast.LENGTH_LONG);
            t.show();
            return null;
        }
    }
    String prefix = "call";
    int audiosource = Integer.parseInt(prefs.getString(Preferences.PREF_AUDIO_SOURCE,              "1"));
    prefix += "-" + audiosource ;
    String suffix = "";
    int audioformat = Integer.parseInt(prefs.getString(Preferences.PREF_AUDIO_FORMAT, "1"));
    switch (audioformat) {
    case MediaRecorder.OutputFormat.THREE_GPP:
        suffix = ".3gpp";
        break;
    case MediaRecorder.OutputFormat.MPEG_4:
        suffix = ".mpg";
        break;
    case MediaRecorder.OutputFormat.RAW_AMR:
        suffix = ".amr";
        break;
    }
    try {
        return File.createTempFile(prefix, suffix, dir);
    } catch (IOException e) {
        Log.e("CallRecorder", "RecordService:makeOutputFile unable to create temp file in " + dir + ": " + e);
        Toast t = Toast.makeText(getApplicationContext(), "CallRecorder was unable to create temp file in " + dir + ": " + e, Toast.LENGTH_LONG);
        t.show();
        return null;
    }
}
public void onCreate()
{
    super.onCreate();
    recorder = new MediaRecorder();
    Log.i("CallRecorder", "onCreate created MediaRecorder object");
}
@SuppressLint("ShowToast")
public void onStart(Intent intent, int startId) {
    Log.i("CallRecorder", "RecordService:onStartCommand called while isRecording:" + isRecording);
    if (isRecording) return;
    Context c = getApplicationContext();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
    Boolean shouldRecord = prefs.getBoolean(Preferences.PREF_RECORD_CALLS, false);
    if (!shouldRecord) {
        Log.i("CallRecord", "RecordService:onStartCommand with PREF_RECORD_CALLS false, not recording");
        return;
    }
    int audiosource = Integer.parseInt(prefs.getString(Preferences.PREF_AUDIO_SOURCE, "1"));
    int audioformat = Integer.parseInt(prefs.getString(Preferences.PREF_AUDIO_FORMAT, "1"));
   recording = makeOutputFile(prefs);

    if (recording == null) {

        recorder = null;
        return; //return 0;
    }
    Log.i("CallRecorder", "RecordService will config MediaRecorder with audiosource: " + audiosource + " audioformat: " + audioformat);
    try {
        recorder.reset();
        recorder.setAudioSource(audiosource);
        Log.d("CallRecorder", "set audiosource " + audiosource);
        recorder.setOutputFormat(audioformat);
        Log.d("CallRecorder", "set output " + audioformat);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        Log.d("CallRecorder", "set encoder default");
        recorder.setOutputFile(recording.getAbsolutePath());
        Log.d("CallRecorder", "set file: " + recording);

        recorder.setOnInfoListener(this);
        recorder.setOnErrorListener(this);

        try {
            recorder.prepare();

        } catch (java.io.IOException e) {
            Log.e("CallRecorder", "RecordService:onStart() IOException attempting recorder.prepare()\n");
            Toast t = Toast.makeText(getApplicationContext(), "CallRecorder was unable to start recording: " + e, Toast.LENGTH_LONG);
            t.show();
            recorder = null;
            return; //return 0; //START_STICKY;
        }
        Log.d("CallRecorder", "recorder.prepare() returned");

        recorder.start();
        isRecording = true;
        Log.i("CallRecorder", "recorder.start() returned");

        updateNotification(true);

        if(recording==null)
        {
            Toast.makeText(getApplicationContext(), "settings", 1000).show();
            recording.delete();
            Log.v("recording", "settings" +recording);
        }
    } catch (java.lang.Exception e) {
        Toast t = Toast.makeText(getApplicationContext(), "CallRecorder was unable to start recording: " + e, Toast.LENGTH_LONG);
        t.show();

        Log.e("CallRecorder", "RecordService:onStart caught unexpected exception", e);
        recorder = null;
    }
    return; 

}

public void onDestroy()
{
    super.onDestroy();
    if (null != recorder) {
        Log.i("CallRecorder", "RecordService:onDestroy calling recorder.release()");

        isRecording = false;
        recorder.release();

       Toast t = Toast.makeText(getApplicationContext(), "CallRecorder finished recording call to " + recording, Toast.LENGTH_LONG);
        t.show();

        }
    updateNotification(false);
}
// methods to handle binding the service
public IBinder onBind(Intent intent)
{
    return null;
}
public boolean onUnbind(Intent intent)
{
    return false;
}
public void onRebind(Intent intent)
{
}
private void updateNotification(Boolean status)
{
    Context c = getApplicationContext();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    if (status) {
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "Recording call " + prefs.getString(Preferences.PREF_AUDIO_SOURCE, "1");
        //CharSequence tickerText = "Recording call " + prefs.getString(Preferences.PREF_AUDIO_SOURCE, "1");
        long when = System.currentTimeMillis();

        //Notification notification = new Notification(icon, tickerText, when);
        Notification notification=new Notification(icon,tickerText,when);
        Context context = getApplicationContext();
        CharSequence contentTitle = "CallRecorder Status";
        CharSequence contentText = "Recording call...";
        Intent notificationIntent = new Intent(this, RecordService.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(RECORDING_NOTIFICATION_ID, notification);
    } else {
        mNotificationManager.cancel(RECORDING_NOTIFICATION_ID);
    }
}
// MediaRecorder.OnInfoListener
public void onInfo(MediaRecorder mr, int what, int extra)
{
    Log.i("CallRecorder", "RecordService got MediaRecorder onInfo callback with what: " + what + " extra: " + extra);
    isRecording = false;
}
// MediaRecorder.OnErrorListener
public void onError(MediaRecorder mr, int what, int extra) 
{
    Log.e("CallRecorder", "RecordService got MediaRecorder onError callback with what: " + what + " extra: " + extra);
    Toast.makeText(getApplicationContext(), "recording not poossible plese chenge the settings", 1000).show();
    isRecording = false;
    mr.release();
}

} *

3 个答案:

答案 0 :(得分:0)

AssetFileDescriptor

AssetFileDescriptor sampleFD = getResources().openRawResourceFd(R.raw.audio);
long size = sampleFD.getLength()

InputStream

InputStream ins = context.getResources().openRawResource (R.raw.audio);
int size = ins.available();

答案 1 :(得分:0)

试试这个 -

float file_size = Float.parseFloat(String.valueOf(filenew.length()/1024));
        Double d=(double) (file_size/1000);
        DecimalFormat df = new DecimalFormat("#.##");
        size.setText(df.format(d)+" MB");

答案 2 :(得分:0)

使用这个来获取kb文件的大小

  try{
                 File file = new File("/sdcard/my_recording.amr");
                 long length = file.length();
                 length = length/1024;
                 System.out.println("File Path : " + file.getPath() + ", File size : " + length +" KB");
            }catch(Exception e){
                System.out.println("File not found : " + e.getMessage() + e);
            }