在android中录制后将音频文件发送到服务器

Posted

技术标签:

【中文标题】在android中录制后将音频文件发送到服务器【英文标题】:send audio file to server after recording in android 【发布时间】:2012-08-09 10:58:59 【问题描述】:

提前感谢您花时间在我的应用上...

我有一个应用程序,我需要在单击名为“RECORD”的按钮后创建一个音频文件并将此文件发送到服务器。音频文件必须以任何 android 支持的音频格式发送。 我怎样才能做到这一点......

【问题讨论】:

你已经尝试过什么......代码在哪里??你在找什么或者你想在这里写你的应用程序! 我想创建一个应用程序,我需要在其中创建一个录制文件并将其发送到服务器。有哪些关键点,在开发的时候一定要牢记? 通过这个链接你会得到所有与录音相关的细节..developer.android.com/reference/android/media/… 谢谢你...!这可以在模拟器的帮助下保存录制吗?我的意思是我们可以将录制文件保存在系统硬盘上吗? 您找到解决方案了吗?你能分享一下,你是如何将此文件发送到服务器的? 【参考方案1】:

Android 中的录制代码

ma​​in.java:

package com.example.audiosend;

import android.os.Bundle;
import android.app.Activity;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity 

   private MediaRecorder myRecorder;
   private MediaPlayer myPlayer;
   private String outputFile = null;
   private Button startBtn;
   private Button stopBtn;
   private Button playBtn;
   private Button stopPlayBtn;
   private TextView text;

   @Override
   protected void onCreate(Bundle savedInstanceState) 
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      text = (TextView) findViewById(R.id.text1);

      // store it to sd card

outputFile = Environment.getExternalStorageDirectory().
              getAbsolutePath() + "/MyAppRecording.3gpp"; //this is the folder in which your Audio file willl save

      myRecorder = new MediaRecorder();
      myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
      myRecorder.setOutputFile(outputFile);

      startBtn = (Button)findViewById(R.id.start);
      startBtn.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View v) 
            // TODO Auto-generated method stub
            start(v);
        
      );

      stopBtn = (Button)findViewById(R.id.stop);
      stopBtn.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View v) 
            // TODO Auto-generated method stub
            stop(v);
        
      );

      playBtn = (Button)findViewById(R.id.play);
      playBtn.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View v) 
            // TODO Auto-generated method stub
                play(v);    
        
      );

      stopPlayBtn = (Button)findViewById(R.id.stopPlay);
      stopPlayBtn.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View v) 
            // TODO Auto-generated method stub
            stopPlay(v);
        
      );
   

   public void start(View view)
       try 
          myRecorder.prepare();
          myRecorder.start();
        catch (IllegalStateException e) 
          // start:it is called before prepare()
          // prepare: it is called after start() or before setOutputFormat() 
          e.printStackTrace();
        catch (IOException e) 
           // prepare() fails
           e.printStackTrace();
        

       text.setText("Recording Point: Recording");
       startBtn.setEnabled(false);
       stopBtn.setEnabled(true);

       Toast.makeText(getApplicationContext(), "Start recording...", 
               Toast.LENGTH_SHORT).show();
   

   public void stop(View view)
       try 
          myRecorder.stop();
          myRecorder.release();
          myRecorder  = null;

          stopBtn.setEnabled(false);
          playBtn.setEnabled(true);
          text.setText("Recording Point: Stop recording");

          Toast.makeText(getApplicationContext(), "Stop recording...",
                  Toast.LENGTH_SHORT).show();
        catch (IllegalStateException e) 
            //  it is called before start()
            e.printStackTrace();
        catch (RuntimeException e) 
            // no valid audio/video data has been received
            e.printStackTrace();
       
   

   public void play(View view) 
       try
           myPlayer = new MediaPlayer();
           myPlayer.setDataSource(outputFile);
           myPlayer.prepare();
           myPlayer.start();

           playBtn.setEnabled(false);
           stopPlayBtn.setEnabled(true);
           text.setText("Recording Point: Playing");

           Toast.makeText(getApplicationContext(), "Start play the recording...", 
                   Toast.LENGTH_SHORT).show();
        catch (Exception e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
   

   public void stopPlay(View view) 
       try 
           if (myPlayer != null) 
               myPlayer.stop();
               myPlayer.release();
               myPlayer = null;
               playBtn.setEnabled(true);
               stopPlayBtn.setEnabled(false);
               text.setText("Recording Point: Stop playing");

               Toast.makeText(getApplicationContext(), "Stop playing the recording...", 
                       Toast.LENGTH_SHORT).show();
           
        catch (Exception e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
   

【讨论】:

该脚本的哪一部分将文件保存到服务器???

以上是关于在android中录制后将音频文件发送到服务器的主要内容,如果未能解决你的问题,请参考以下文章

使用 SmartFace.io 录制音频(Android - Ios)

流式传输实时音频

在 iPhone 上录制音频并使用 NSOutputStream 通过网络发送

Android:使用 audiorecord 类录制音频播放快进

尝试将音频文件上传到服务器时出现Android问题

使用音频文件向 Google Home 发送广播消息