将信息存储在音频文件中

时间:2011-05-25 10:02:30

标签: android

我想在录制MP3文件时存储艺术家等信息。 :

public class AudioCap extends Activity {  
    private static final String LOG_TAG = "AudioRecordTest";  
    private static String mFileName = null;  

    private RecordButton mRecordButton = null;  
    private MediaRecorder mRecorder = null;  

    private PlayButton mPlayButton = null;    
    private MediaPlayer mPlayer = null;  

    private void onRecord(boolean start) {  
        if (start) {  
            startRecording();  
    }       else {  
            stopRecording();  
    }  
     }  

    private void onPlay(boolean start) {  
        if (start) {  
            startPlaying();  
        }   else {  
            stopPlaying();  
        }  
    } 

    private void startPlaying() {  
        mPlayer = new MediaPlayer();  
         try {  
            mPlayer.setDataSource(mFileName);  
            mPlayer.prepare();  
            mPlayer.start();  
        } catch (IOException e) {  
             Log.e(LOG_TAG, "prepare() failed");  
        }  
    }  

private void stopPlaying() {
    mPlayer.release();
    mPlayer = null;
}

private void startRecording() {
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }

    mRecorder.start();
}

private void stopRecording() {
    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;
}

class RecordButton extends Button {
    boolean mStartRecording = true;

    OnClickListener clicker = new OnClickListener() {
        public void onClick(View v) {
            onRecord(mStartRecording);
            if (mStartRecording) {
                setText("Stop recording");
            } else {
                setText("Start recording");
            }
            mStartRecording = !mStartRecording;
        }
    };

    public RecordButton(Context ctx) {
        super(ctx);
        setText("Start recording");
        setOnClickListener(clicker);
    }
}

class PlayButton extends Button {
    boolean mStartPlaying = true;

    OnClickListener clicker = new OnClickListener() {
        public void onClick(View v) {
            onPlay(mStartPlaying);
            if (mStartPlaying) {
                setText("Stop playing");
            } else {
                setText("Start playing");
            }
            mStartPlaying = !mStartPlaying;
        }
    };

    public PlayButton(Context ctx) {
        super(ctx);
        setText("Start playing");
        setOnClickListener(clicker);
    }
}

public AudioCap() {
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/audiorecordtest.mp3";
}

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    LinearLayout ll = new LinearLayout(this);
    mRecordButton = new RecordButton(this);
    ll.addView(mRecordButton,
            new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    0));
    mPlayButton = new PlayButton(this);
    ll.addView(mPlayButton,
            new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    0));
    setContentView(ll);
}

@Override
public void onPause() {
    super.onPause();
    if (mRecorder != null) {
        mRecorder.release();
        mRecorder = null;
    }

    if (mPlayer != null) {
        mPlayer.release();
        mPlayer = null;
    }
}

}

我有以下代码以mp3格式录制我的音频文件。但细节不能添加到它。那么有什么方法可以通过我的android activity / java程序将一些元数据存储到我录制的音频文件中。

的问候,
斯纳

2 个答案:

答案 0 :(得分:1)

您需要在MediaStore

中添加音频信息
    ContentResolver mCr = app.getContentResolver();
   ContentValues values = new ContentValues();
   values.put(MediaStore.MediaColumns.DATA, your_sound_file.getAbsolutePath());
   values.put(MediaStore.MediaColumns.TITLE, "Name Of Your File");
   values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
   values.put(MediaStore.MediaColumns.SIZE, your_sound_file.length());
   values.put(MediaStore.Audio.Media.ARTIST, "Artist Name");
   values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
      //Now set some extra features it depend on you
   values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
   values.put(MediaStore.Audio.Media.IS_ALARM, true);
   values.put(MediaStore.Audio.Media.IS_MUSIC, false);

   Uri uri = MediaStore.Audio.Media.getContentUriForPath(your_file.getAbsolutePath());
   Uri uri2= mCr.insert(uri, values);

在调用insert方法之前,请检查声音文件是否已存在。如果文件已存在,请确保您没有调用它。 未捕获异常,因为异常不在您的代码中。它甚至不在你的过程中。它正在MediaStore内容提供商的过程中。

Agin这些链接可以帮助你

Setting Ringtone notification from SD card file

Android: save sound as ringtone / SQLiteConstraintException

答案 1 :(得分:0)

参见here,这个例子描述了如何从mp3文件中提取id3标签。我认为将信息存储到mp3的过程是类似的

链接到MediaStore