2014-04-05 236 views
-1

我正試圖開發需要用戶語音錄製的android應用程序。我看到很多代碼,但沒有任何與我一起工作,打開它,但它沒有記錄任何東西。Android音頻錄製

這裏是代碼:

package com.example.record8; 

import java.io.File; 
import java.io.IOException; 

import android.app.Activity; 
import android.content.ContentResolver; 
import android.content.ContentValues; 
import android.content.Intent; 
import android.media.MediaRecorder; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.util.Log; 
import android.view.View; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

MediaRecorder recorder; 
File audiofile = null; 
private static final String TAG = "SoundRecordingActivity"; 
private View startButton; 
private View stopButton; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    startButton = findViewById(R.id.btnStart); 
    stopButton = findViewById(R.id.btnStop); 
} 

public void startRecording(View view) throws IOException { 

    startButton.setEnabled(false); 
    stopButton.setEnabled(true); 

    File sampleDir = Environment.getExternalStorageDirectory(); 
    try { 
     audiofile = File.createTempFile("sound", ".3gp", sampleDir); 
    } catch (IOException e) { 
     Log.e(TAG, "sdcard access error"); 
     return; 
    } 
    recorder = new MediaRecorder(); 
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    recorder.setOutputFile(audiofile.getAbsolutePath()); 
    recorder.prepare(); 
    recorder.start(); 
} 

public void stopRecording(View view) { 
    startButton.setEnabled(true); 
    stopButton.setEnabled(false); 
    recorder.stop(); 
    recorder.release(); 
    addRecordingToMediaLibrary(); 
} 

protected void addRecordingToMediaLibrary() { 
    ContentValues values = new ContentValues(4); 
    long current = System.currentTimeMillis(); 
    values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName()); 
    values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current/1000)); 
    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp"); 
    values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath()); 
    ContentResolver contentResolver = getContentResolver(); 

    Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    Uri newUri = contentResolver.insert(base, values); 

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri)); 
    Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show(); 
} 
} 

這裏是XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

<Button 
    android:id="@+id/btnStart" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginLeft="45dp" 
    android:layout_marginTop="48dp" 
    android:text="@string/startrecording" /> 

<Button 
    android:id="@+id/btnStop" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/btnStart" 
    android:layout_below="@+id/btnStart" 
    android:layout_marginTop="52dp" 
    android:text="@string/stoprecording" /> 

<Button 
    android:id="@+id/btnFormat" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/btnStop" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="75dp" 
    android:text="@string/audio_format" /> 

我不知道爲什麼忽略了最低紀錄。

回答

0

您可以檢查此鏈接,

http://androidcodeexamples.blogspot.in/2012/06/voice-recording-in-android.html

我也已經實現了錄音從這個博客,它的工作完全罰款也。

並確保您在執行此操作時在Android清單文件中擁有此權限。

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

也可以通過訪問該鏈接如下

https://www.dropbox.com/s/rhcswyht20xhud2/Android%20Audio%20Recording.zip

希望這將幫助你下載的源代碼。