我想要从扬声器或输出声音中捕获声音

时间:2017-03-06 04:29:58

标签: java android audio recording

我想创建一个录音应用程序,它将记录手机扬声器的输出声音,例如,如果我在手机上播放收音机,通过使用此应用程序,我可以捕获并记录输出,后来我可以通过播放录制的文件来播放声音或歌曲

现在我可以录制来自麦克风的声音,所以我想修改代码,有人能告诉我该怎么做吗?

这是我的主要活动

public class MainActivity extends AppCompatActivity {

private Button play,stop,record;
private MediaRecorder myAudioRecorder;
private String outputfile;


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

    play=(Button)findViewById(R.id.play);
    stop=(Button)findViewById(R.id.stop);
    record=(Button)findViewById(R.id.record);
    stop.setEnabled(false);
    play.setEnabled(false);


    //outputfile= Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording_"+System.currentTimeMillis()+".mp3";
    File directorysample=new File(Environment.getExternalStorageDirectory()+separator+"RadioRecords");
    directorysample.mkdirs();
    String filename = "/recording_" + System.currentTimeMillis() + ".mp3";
    final  String directory = Environment.getExternalStorageDirectory()+ "/RadioRecords";
    outputfile = directory + "/" + filename;




    //record function


    record.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {

         try {


             myAudioRecorder=new MediaRecorder();
             myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
             myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
             myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.DEFAULT);
             myAudioRecorder.setOutputFile(outputfile);
             myAudioRecorder.prepare();
             myAudioRecorder.start();
     }
     catch (IllegalStateException ise){



     }catch (IOException ioe){



         }
            record.setEnabled(false);
            stop.setEnabled(true);
            Toast.makeText(getApplicationContext(),"record startded",Toast.LENGTH_LONG).show();

     }

 });


   //stop function


    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {



            myAudioRecorder.stop();
            record.setEnabled(true);
            myAudioRecorder.release();
            myAudioRecorder=null;
            stop.setEnabled(false);
            play.setEnabled(true);
            Toast.makeText(getApplicationContext(),"recorded audio",Toast.LENGTH_LONG).show();

        }

    });
            play.setOnClickListener(new View.OnClickListener() {
                 @Override
                public void onClick(View view) {
                     MediaPlayer mediaPlayer=new MediaPlayer();

                     try {
                             mediaPlayer.setDataSource(outputfile);
                             mediaPlayer.prepare();
                             mediaPlayer.start();
                             Toast.makeText(getApplicationContext(),"playing  audio",Toast.LENGTH_LONG).show();

    }
    catch (Exception e)
    {

    }

}
   });








    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;











}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

0 个答案:

没有答案