调用音频记录和获取生成的文件 [英] Invoking audio recorder and getting the resulting file

查看:102
本文介绍了调用音频记录和获取生成的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用在Android 2.2.1的音频记录(设备三星Galaxy POP),使用下面的code:

I am trying to invoke the audio recorder on Android 2.2.1 (device Samsung Galaxy POP) using the following code:

private static final int ACTIVITY_RECORD_SOUND = 1;
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, ACTIVITY_RECORD_SOUND);

这成功地调用记录。在我的活动结果我做到以下几点:

This invokes the recorder successfully. In my activity result i do the following:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
                         case ACTIVITY_RECORD_SOUND:
                             data.getDataString();
                         break;
                            }
                }
       }

在我完成录制我preSS回到了录音机,返回按预期的方式控制到onActivityResult方法,但我的结果code始终为0(这是Activity.RESULT_CANCELED)和我的数据是空值。难道我错过了一些东西呢?请帮助我。这适用于仿真器而不是在该设备。先谢谢了。

After i complete the recording i press back on the audio recorder which returns the control to the onActivityResult method as expected, but my resultCode is always 0 (which is Activity.RESULT_CANCELED) and my data is null. Am i missing out on something here? Kindly help me with this. This works on the emulator but not on the device. Thanks in advance.

推荐答案

我终于利用FileObserver找到一个解决办法我的问题。我用以下方法来实现它:

I finally found a workaround for my problem by using the FileObserver. I achieved it by doing the following:

package com.pravaa.audiointent;

 import java.io.DataOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.Enumeration;
 import java.util.Vector;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.FileObserver;
 import android.provider.MediaStore;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.LinearLayout;
 import android.widget.Toast;

public class AudioActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button sampleButton;
private FileObserver mFileObserver;
private Vector<String> audioFileNames;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    audioFileNames = new Vector<String>();
    LinearLayout finalContainer = new LinearLayout(this);
    sampleButton = new Button(this);
    sampleButton.setOnClickListener(this);
    sampleButton.setText("Start Audio Intent");
    finalContainer.addView(sampleButton);
    setContentView(finalContainer);
    addObserver();

}

private void addObserver() {
    this.mFileObserver = new FileObserver("/sdcard/Sounds/") {
        @Override
        public void onEvent(int event, String path) {
            if (event == FileObserver.CREATE) {
                if (path != null) {
                    int index = path.indexOf("tmp");
                    String tempFileName = (String) path.subSequence(0,
                            index - 1);
                    audioFileNames.add(tempFileName);

                }
            } else if (event == FileObserver.DELETE) {
                if (path != null) {
                    int index = path.indexOf("tmp");
                    String tempFileName = (String) path.subSequence(0,
                            index - 1);
                    if (audioFileNames.contains(tempFileName)) {
                        audioFileNames.remove(tempFileName);
                    }
                }

            }
        }
    };
}

private void readFile(String fileName) {

    File attachment = new File("/sdcard/Sounds/" + fileName);
    if (attachment.exists()) {
        FileInputStream fis;
        try {
            fis = new FileInputStream(attachment);
            byte[] bytes = new byte[(int) attachment.length()];
            try {
                fis.read(bytes);
                fis.close();

                attachment.delete();

                saveMedia("Test" + fileName, bytes);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    mFileObserver.startWatching();
}

public void saveMedia(String fileName, byte[] data) {

    String imagePath = "/sdcard/sam/";
    System.out.println("Inside Folder");


    File file = new File(imagePath, fileName);
    System.out.println("File Created");

    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(file);
        DataOutputStream dataOutputStream = new DataOutputStream(
                fileOutputStream);
        System.out.println("Writting File");
        dataOutputStream.write(data, 0, data.length);
        System.out.println("Finished writting File");
        dataOutputStream.flush();
        dataOutputStream.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
    startActivityForResult(intent, 2); 
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    // TODO Auto-generated method stub
    if (requestCode == 2) {
        if (mFileObserver != null) {
            mFileObserver.stopWatching();
        }
        Enumeration<String> audioFileEnum = audioFileNames.elements();
        while (audioFileEnum.hasMoreElements()) {
            readFile((String) audioFileEnum.nextElement());
        }
    }
}}

这篇关于调用音频记录和获取生成的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆