2012-03-07 47 views
3

我正在使用android內置媒體播放器通過網址播放音樂的項目。我想實現這一目標的是能夠將流數據保存在SD卡上的文件中。我試圖用媒體錄像機中的android build來做到這一點,但它記錄了手機的所有內容,不僅是來自媒體播放器的聲音。Android如何從媒體播放器錄製音樂

所以我的問題是哪個是最好的方法來實現這個?

這裏是我已經測試了一個例子,但我不能播放MP3文件之後,看看是否一切正常:

Log.e("URL AGAIN","url : "+url); 
      try { 
       if(!isrecording){ 

        URL urlStream = new URL(url); 
        InputStream inputStream = urlStream.openStream(); 
        Log.d("", "urlStream.openStream()"); 

        String filename = Environment.getExternalStorageDirectory().getAbsolutePath(); 
        filename += "/deliciousradio.mp3"; 

        File outputSource= new File(filename); 
        fileOutputStream = new FileOutputStream(outputSource); 
        Log.d("", "FileOutputStream: " + outputSource); 

        int bytesRead = -1; 
        isrecording = true; 

        byte[] buffer = new byte[30 * 1024]; 
        while ((bytesRead = inputStream.read(buffer)) > 0) { 

         byte[] buffer2 = new byte[bytesRead]; 
         fileOutputStream.write(buffer2); 

         Log.d("","bytes size :"+buffer2.length); 
         Log.d("","bytesRead : "+bytesRead); 

        } 


       } else if(isrecording){ 
        fileOutputStream.close(); 
       } 
      } catch(Exception e){} 

這裏的問題是,我收到30作爲buffer2的長度,我不能理解爲什麼。

感謝您的幫助!

+0

什麼是輸入和輸出格式? – Raffaele 2012-03-07 16:42:54

+0

我總是得到'buffer2.length'和'bytesRead'的相同數字,並且它從不是'30'。順便說一句,你不應該分配'buffer2'。有['OutputStream.write(buffer,start,length)'](http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write%28byte%5B%5D ,%20int,%20int%29)。 用於複製流的BTW,您可以方便地使用[Apache IO utils](http://commons.apache.org/io/api-1.2/org/apache/commons/io/IOUtils.html#copy%28java.io.InputStream ,%20java.io.OutputStream%29) – Raffaele 2012-03-07 17:55:05

+0

嗨@ android-droid,我也在尋找相同的東西(如何記錄音樂從媒體播放器)。如果你有任何關於這個plz幫助mee ..先謝了。 – sandeep 2013-02-28 09:52:36

回答

0
Button recstart, recstop,replay; 
//onCreate 
File f; 
    File externalStorage; 
    String path = ""; 
    MediaRecorder recorder; 
    Timer time; 
    String filename1 = ""; 
    boolean s = false; 
externalStorage = Environment.getExternalStorageDirectory(); 
     String sdCardPath = externalStorage.getAbsolutePath(); 
     recorder = new MediaRecorder(); 
     path = sdCardPath + "/"; 

recstart.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       String state = android.os.Environment.getExternalStorageState(); 
       if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) { 
        try { 
         throw new IOException("SD Card is not mounted. It is " 
           + state + "."); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

       Date dt = new Date(); 
       int hours = dt.getHours(); 
       int minutes = dt.getMinutes(); 
       int seconds = dt.getSeconds(); 
       String curTime = hours + "_" + minutes + "_" + seconds; 

       filename1 = "phonecall_at" + curTime + ".mp4"; 
       if (recorder == null) { 
        recorder = new MediaRecorder(); 
       } 

       f = new File(path, filename1); 
       try { 
        s = f.createNewFile(); 

       } catch (IOException e1) { 
        // TODO Auto-generated catch block 
        // Toast.makeText(AudioRecording.this,"hiiiii...."+e1.getMessage(),2000).show(); 
        e1.printStackTrace(); 
       } 
       // MediaRecorder.AudioSource.VOICE_CALL + 
       // MediaRecorder.AudioSource.MIC 
       // recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_UPLINK 
       // + MediaRecorder.AudioSource.VOICE_DOWNLINK); 
       if (s == true) { 
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 

        recorder.setOutputFile(f.getAbsolutePath()); 
        // record.setText("stop"); 
        // record.setBackgroundColor(Color.BLUE); 
        // Toast.makeText(AudioRecording.this, String.valueOf(s), 
        // 3000).show(); 
        try { 
         recorder.prepare(); 
         Toast.makeText(AudioRecording.this, "Recording starts", 
           5000).show(); 
        } catch (IllegalStateException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
         Log.e(".................................", 
           "" + e.toString()); 
        } 
        recorder.start(); 
       } else { 
        Toast.makeText(getApplicationContext(), 
          "No space Left on device", 2000).show(); 
       } 
       /* 
       * try { recorder.prepare(); Toast.makeText(Recordingvoice 
       * .this,"Recording starts",5000).show(); recorder.start(); 
       * 
       * 
       * } catch (IllegalStateException e) { // TODO Auto-generated 
       * catch block e.printStackTrace(); } catch (IOException e) { // 
       * TODO Auto-generated catch block // e.printStackTrace(); } 
       */ 
      } 
     }); 
recstop.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       if (s == true) { 
        Toast.makeText(AudioRecording.this, "Recording stopped", 
          2000).show(); 
        if (recorder == null) { 
         recorder = new MediaRecorder(); 
        } 
        if (recorder != null) { 
         recorder.stop(); 
         recorder.release(); 
        } 
        recorder = null; 
        // time.cancel(); 
        // uploadCode(); 
       } 

      } 
     }); 
replay.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       if (f.exists()) { 
        Toast showMsg = Toast.makeText(getApplicationContext(), "Playing", Toast.LENGTH_SHORT); 
        showMsg.show(); 
        String path = f.getAbsolutePath(); 
        Uri myUri = Uri.parse(path); 
        MediaPlayer mp = new MediaPlayer(); 
        mp.setLooping(false); 
        mp = MediaPlayer.create(AudioRecording.this, myUri); 
        mp.start(); 
       } 
      } 
     }); 
+2

正如我所看到的,您正在使用MediaRecorder類,它實際上是從麥克風錄製的,並且所有其他聲音也將被錄製,例如,如果用戶在錄音過程中講話等。 – 2012-03-07 12:40:28

+0

MediaRecorder類記錄用戶在講話過程中說的媒體文件錄音等。 – 2012-03-07 12:46:55

+0

我不需要記錄用戶的說話。我只想記錄媒體播放器的輸出。 – 2012-03-07 13:13:27

相關問題