Showing posts with label Multimedia. Show all posts
Showing posts with label Multimedia. Show all posts

Saturday, 6 April 2013

Android Multimedia How to play a MP3 in an Android application?


Actually, the format doesn't matter as long as Android can decode it. 

private void playSound(int dResource) 
{
    try{
          if ( m_objMediaPlayer != null ) {
                  m_objMediaPlayer.stop();
                  m_objMediaPlayer = null;
         }
         m_objMediaPlayer = MediaPlayer.create(this, dResource);
         m_objMediaPlayer.setLooping(false);
         m_objMediaPlayer.start();
    } catch( Exception ex ) {
            Log.e( "Oops in playsound", "", ex );
       }
 }

Android Multimedia How to capture audio in an android application?


This snippet captures audio from a given source and saves it to a file with android.media.MediaRecorder. 
/* Create a MediaRecorder */
recorder = new MediaRecorder();

/* ContentValues include title, timestamp, mime type */
ContentValues values = new ContentValues(3);
values.put(MediaStore.MediaColumns.TITLE, SOME_NAME_HERE);
values.put(MediaStore.MediaColumns.TIMESTAMP, System.currentTimeMillis());
values.put(MediaStore.MediaColumns.MIME_TYPE, recorder.getMimeContentType());

/* Create entry in the content database */
ContentResolver contentResolver = new ContentResolver();
Uri base = MediaStore.Audio.INTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);

if (newUri == null) {
    /* Handle exception here - not able to create a new content entry */
}

/* Receive the real path as String */
String path = contentResolver.getDataFilePath(newUri);

/* Set Audio Source, Format, Encode and File */
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);

/* Prepare the Recorder */
recorder.prepare();

/* Start Recording */
recorder.start();

/* ... */

/* Stop Recording Again */
recorder.stop();
recorder.release();



Disqus for Android