qq_22268439 2015-05-17 14:21 采纳率: 0%
浏览 2145

Android 使用intent录音,为什么无法重新指定录制文件存放位置?

1.使用intent存放录制的音频,录音文件只存放在默认文件夹下
2.重新制定位置存放音频文件

  • 写回答

1条回答 默认 最新

  • zzkjliu 2015-05-18 01:02
    关注

    Android 使用intent录音,为什么无法重新指定录制文件存放位

    EX07_11.java
    Java代码 收藏代码

    package irdc.ex07_11;  
    
    import java.io.File;  
    import java.io.IOException;  
    import java.util.ArrayList;  
    
    import android.app.Activity;  
    import android.content.Intent;  
    import android.media.MediaRecorder;  
    import android.net.Uri;  
    import android.os.Bundle;  
    import android.os.Environment;  
    import android.view.View;  
    import android.widget.AdapterView;  
    import android.widget.ArrayAdapter;  
    import android.widget.CheckedTextView;  
    import android.widget.ImageButton;  
    import android.widget.ListView;  
    import android.widget.TextView;  
    import android.widget.Toast;  
    
    public class EX07_11 extends Activity  
    {  
      private ImageButton myButton1;  
      private ImageButton myButton2;  
      private ImageButton myButton3;  
      private ImageButton myButton4;  
    
      private ListView myListView1;  
      private String strTempFile = "ex07_11_";  
      private File myRecAudioFile;  
      private File myRecAudioDir;// 得到Sd卡path  
      private File myPlayFile;  
      private MediaRecorder mMediaRecorder01;  
    
      private ArrayList<String> recordFiles;  
      private ArrayAdapter<String> adapter;// 用于ListView的适配器  
      private TextView myTextView1;  
      private boolean sdCardExit;  
      private boolean isStopRecord;  
    
      /** Called when the activity is first created. */  
      @Override  
      public void onCreate(Bundle savedInstanceState)  
      {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        //主要是4个控制按钮(录制,停止,播放,删除)  
        myButton1 = (ImageButton) findViewById(R.id.ImageButton01);  
        myButton2 = (ImageButton) findViewById(R.id.ImageButton02);  
        myButton3 = (ImageButton) findViewById(R.id.ImageButton03);  
        myButton4 = (ImageButton) findViewById(R.id.ImageButton04);  
        //列表出指定文件夹中所有amr格式音频文件  
        myListView1 = (ListView) findViewById(R.id.ListView01);  
        myTextView1 = (TextView) findViewById(R.id.TextView01);  
    
        myButton2.setEnabled(false);  
        myButton3.setEnabled(false);  
        myButton4.setEnabled(false);  
    
        /* 判断SD Card是否插入 */  
        sdCardExit = Environment.getExternalStorageState().equals(  
            android.os.Environment.MEDIA_MOUNTED);  
    
        /* 取得SD Card路径作为录音的文件位置 */  
        if (sdCardExit)  
        {  
          myRecAudioDir = Environment.getExternalStorageDirectory();  
        }  
    
        /* 取得SD Card目录里的所有.amr文件 */  
        getRecordFiles();  
    
        adapter = new ArrayAdapter<String>(this,  
            R.layout.my_simple_list_item, recordFiles);  
    
        /* 将ArrayAdapter添加ListView对象中 */  
        myListView1.setAdapter(adapter);  
    
        /* 录音 */  
        myButton1.setOnClickListener(new ImageButton.OnClickListener()  
        {  
          @Override  
          public void onClick(View arg0)  
          {  
            try  
            {  
              if (!sdCardExit)  
              {  
                Toast.makeText(EX07_11.this, "请插入SD Card",  
                    Toast.LENGTH_LONG).show();  
                return;  
              }  
              // 创建录音频文件  
              //这种创建方式生成的文件名是随机的,所以我本人不是很习惯  
    //          myRecAudioFile = File.createTempFile(strTempFile, ".amr",  
    //              myRecAudioDir);  
               File sdcardDir = Environment.getExternalStorageDirectory();  
               String path = sdcardDir.getParent() + sdcardDir.getName();  
               String filePath = path + java.io.File.separator + "Demo";  
              //创建文件,使用自己指定文件名(这里我手动创建好了,我们也可以利用mkdirs的方法来创建)  
               myRecAudioFile = new File(filePath,"new.amr");  
    
              mMediaRecorder01 = new MediaRecorder();  
    
              /* 设置录音来源为麦克风 */  
              mMediaRecorder01  
                  .setAudioSource(MediaRecorder.AudioSource.MIC);  
    
              mMediaRecorder01  
                  .setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);  
    
              mMediaRecorder01  
                  .setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);  
              //文件保存位置  
              mMediaRecorder01.setOutputFile(myRecAudioFile  
                  .getAbsolutePath());  
    
              mMediaRecorder01.prepare();  
              mMediaRecorder01.start();  
    
              myTextView1.setText("录音中");  
    
              myButton2.setEnabled(true);  
              myButton3.setEnabled(false);  
              myButton4.setEnabled(false);  
    
              isStopRecord = false;  
            } catch (IOException e)  
            {  
              // TODO Auto-generated catch block  
              e.printStackTrace();  
            }  
          }  
        });  
        /* 停止 */  
        myButton2.setOnClickListener(new ImageButton.OnClickListener()  
        {  
          @Override  
          public void onClick(View arg0)  
          {  
            // TODO Auto-generated method stub  
            if (myRecAudioFile != null)  
            {  
              /* 停止录音 */  
              mMediaRecorder01.stop();  
              mMediaRecorder01.release();  
    
              mMediaRecorder01 = null;  
    
              /* 将录音频文件名给Adapter */  
              adapter.add(myRecAudioFile.getName());  
              myTextView1.setText("停止:" + myRecAudioFile.getName());  
              myButton2.setEnabled(false);  
    
              isStopRecord = true;  
            }  
          }  
        });  
        /* 播放 */  
        myButton3.setOnClickListener(new ImageButton.OnClickListener()  
        {  
          @Override  
          public void onClick(View arg0)  
          {  
            // TODO Auto-generated method stub  
            //if (myPlayFile != null && myPlayFile.exists())  
            //{  
              /* 打开播放的程序 */  
             // openFile(myPlayFile);  
              System.out.println("_________________"+myRecAudioFile.getAbsolutePath());  
                //这里我们也可以加个判断:  
                //if(是否存在音频文件) myRecAudioFile.exists()  
              openFile(myRecAudioFile);  
    
            //}  
          }  
        });  
        /* 删除 */  
        myButton4.setOnClickListener(new ImageButton.OnClickListener()  
        {  
          @Override  
          public void onClick(View arg0)  
          {  
            // TODO Auto-generated method stub  
            if (myPlayFile != null)  
            {  
              /* 先将Adapter删除文件名 */  
              adapter.remove(myPlayFile.getName());  
              /* 删除文件 */  
              if (myPlayFile.exists())  
                myPlayFile.delete();  
              myTextView1.setText("完成删除");  
            }  
          }  
        });  
    
        myListView1  
            .setOnItemClickListener(new AdapterView.OnItemClickListener()  
            {  
              @Override  
              public void onItemClick(AdapterView<?> arg0, View arg1,  
                  int arg2, long arg3)  
              {  
                /* 当有点击档名时将删除及播放按钮Enable */  
                myButton3.setEnabled(true);  
                myButton4.setEnabled(true);  
    
                System.out.println("_______________"  
                    + myRecAudioDir.getAbsolutePath());  
    //            myPlayFile = new File(myRecAudioDir.getAbsolutePath()  
    //                + File.separator  
    //                + ((CheckedTextView) arg1).getText());  
                myTextView1.setText("你选的是:"  
                    + ((CheckedTextView) arg1).getText());  
              }  
            });  
      }  
    
      @Override  
      protected void onStop()  
      {  
        if (mMediaRecorder01 != null && !isStopRecord)  
        {  
          /* 停止录音 */  
          mMediaRecorder01.stop();  
          mMediaRecorder01.release();  
          mMediaRecorder01 = null;  
        }  
        super.onStop();  
      }  
    
      // 存储一个音频文件数组到list当中  
      private void getRecordFiles()  
      {  
        recordFiles = new ArrayList<String>();  
        if (sdCardExit)  
        {  
          File files[] = myRecAudioDir.listFiles();  
          if (files != null)  
          {  
            for (int i = 0; i < files.length; i++)  
            {  
              if (files[i].getName().indexOf(".") >= 0)  
              {  
                /* 只取.amr文件 */  
                String fileS = files[i].getName().substring(  
                    files[i].getName().indexOf("."));  
                if (fileS.toLowerCase().equals(".amr"))  
                  recordFiles.add(files[i].getName());  
              }  
            }  
          }  
        }  
      }  
    
      /* 打开播放录音文件的程序 */  
      private void openFile(File f)  
      {  
        Intent intent = new Intent();  
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        intent.setAction(android.content.Intent.ACTION_VIEW);  
        String type = getMIMEType(f);  
        intent.setDataAndType(Uri.fromFile(f), type);  
        startActivity(intent);  
      }  
    
      private String getMIMEType(File f)  
      {  
        String end = f  
            .getName()  
            .substring(f.getName().lastIndexOf(".") + 1,  
                f.getName().length()).toLowerCase();  
        String type = "";  
        if (end.equals("mp3") || end.equals("aac") || end.equals("aac")  
            || end.equals("amr") || end.equals("mpeg")  
            || end.equals("mp4"))  
        {  
          type = "audio";  
        } else if (end.equals("jpg") || end.equals("gif")  
            || end.equals("png") || end.equals("jpeg"))  
        {  
          type = "image";  
        } else  
        {  
          type = "*";  
        }  
        type += "/*";  
        return type;  
      }  
    }  
    

    另外是界面文件
    main.xml
    Java代码 收藏代码

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical" android:layout_width="fill_parent"  
        android:layout_height="fill_parent" android:background="@drawable/white">  
        <LinearLayout android:id="@+id/LinearLayout01"  
            android:layout_width="wrap_content" android:layout_height="wrap_content">  
            <ImageButton android:id="@+id/ImageButton01"  
                android:layout_width="wrap_content" android:layout_height="wrap_content"  
                android:src="@drawable/record">  
            </ImageButton>  
            <ImageButton android:id="@+id/ImageButton02"  
                android:layout_width="wrap_content" android:layout_height="wrap_content"  
                android:src="@drawable/stop">  
            </ImageButton>  
            <ImageButton android:id="@+id/ImageButton03"  
                android:layout_width="wrap_content" android:layout_height="wrap_content"  
                android:src="@drawable/play">  
            </ImageButton>  
            <ImageButton android:id="@+id/ImageButton04"  
                android:layout_width="wrap_content" android:layout_height="wrap_content"  
                android:src="@drawable/delete">  
            </ImageButton>  
        </LinearLayout>  
        <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:textColor="@drawable/black">  
        </TextView>  
        <ListView android:id="@+id/ListView01" android:layout_width="wrap_content"  
            android:layout_height="wrap_content" android:background="@drawable/black">  
        </ListView>  
    </LinearLayout>  
    

    和my_simple_list_item.xml文件
    Java代码 收藏代码

    <?xml version="1.0" encoding="utf-8"?>  
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@+id/myCheckedTextView1" android:layout_width="fill_parent"  
        android:layout_height="fill_parent" android:textColor="@drawable/white" />   
    

    这个CheckedTextView我还没有使用过,哪位知道的可以留言告诉我一些资料来源啥的
    最后权限:
    Java代码 收藏代码

    <uses-permission android:name="android.permission.RECORD_AUDIO" />  
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛