3

I need to play an 3gp audio file stored in my sd card location, to play it with my default media player in my HTC device.

Code :

 public class AudioRecorder extends Activity {

     private static final String CAMERA_STATUS = "camera_upload";
        private static final String GALLERY_STATUS = "gallery_upload";
        MediaRecorder recorder = new MediaRecorder();
        static String path = "audio-android.3gp";
        Button startRecording;
        Button stopRecording;
        Button save;
        Button palyAudio;
        private Context context;


     private String sanitizePath(String path) {
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (!path.contains(".")) {
            path += ".3gp";
        }
        return Environment.getExternalStorageDirectory().getAbsolutePath()
            + path;
        }



     @Override
        protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.audio_recording);
        context = this;
        /**
         * Creates a new audio recording at the given path (relative to root of
         * SD card).
         */

        this.path = sanitizePath(path);

        startRecording = (Button) findViewById(R.id.startRecording);
        stopRecording = (Button) findViewById(R.id.stopRecording);
        save = (Button) findViewById(R.id.resetRecording);
        palyAudio = (Button) findViewById(R.id.playRecorded);

startRecording.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
            try {
                stopRecording.setVisibility(View.VISIBLE);
                startRecording.setVisibility(View.GONE);
                start();

            } catch (IOException e) {
                e.printStackTrace();
            }

            }
        });

        stopRecording.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
            stopRecording.setVisibility(View.GONE);
            startRecording.setVisibility(View.VISIBLE);
            try {
                            stop();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            }
        });


        palyAudio.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
             playAudio();
             //startMediaPlayer();

            }

        });

        }

        /**
         * 
         * play the recorded audio
         * 
         */
        public void playAudio() {
        try {

            Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
            Uri data = Uri.parse(path);
           // Intent intent = new Intent(Intent.ACTION_VIEW, data);  
            intent.setDataAndType(data, "audio/mp3");
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            // TODO: handle exception
        }
        }


      /**
         * Starts a new recording.
         */
        public void start() throws IOException {
        recorder = new MediaRecorder();

        String state = android.os.Environment.getExternalStorageState();

        if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
            throw new IOException("SD Card is not mounted.  It is " + state
                + ".");
        }

        // make sure the directory we plan to store the recording in exists
        File directory = new File(path).getParentFile();
        System.out.println("start() directory >  " + directory);
        if (!directory.exists() && !directory.mkdirs()) {
            throw new IOException("Path to file could not be created.");
        }

        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);                 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);    recorder.setOutputFile(path); 
        recorder.prepare();               
        recorder.start(); 

        }

        /**
         * Stops a recording that has been previously started.
         */
        public void stop() throws IOException {
        recorder.stop(); // Stops recording.
        recorder.release(); // Now the object cannot be reused

        }


    }
halfer
  • 19,824
  • 17
  • 99
  • 186
jennifer
  • 8,133
  • 22
  • 69
  • 96

5 Answers5

6
public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(t);
        path = "/sdcard/sample.mp3";
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDataSource(path);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
    }
Androider
  • 803
  • 6
  • 18
5

So this is actually quite a headbanger, but here's how to do it:

Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath() + yourfilepath);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
it.setDataAndType(uri,"video/3gpp");
startActivity(it);

The key is to set the video MIME, even for the audio file. Don't ask me why this works, it just does.

Rrr Jjj
  • 51
  • 1
  • 3
1
Uri playUri = Uri.parse("file:///sdcard/music/an.mp3");
Intent intent = new Intent(Intent.ACTION_VIEW, playUri); 
startActivity(intent);
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • I have given this bt below expection is thrown : 05-04 17:26:05.161: WARN/System.err(4819): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/mnt/sdcard/audio-android.3gp } – jennifer May 04 '11 at 11:57
1

Use VideoView (see example) to get over the use of surface and states, it uses the native player.

String path = getExternalStorageDirectory() + "/path/to/file/in/sdcard";
VideoView vv = (VideoView)findViewById(R.id.myVideoViewId);
vv.setVideoPath(path);
vv.start();
MByD
  • 135,866
  • 28
  • 264
  • 277
1
protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
        Uri data = Uri.parse("file:///sdcard/sample.mp3");
        intent.setDataAndType(data,"audio/mp3");

        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();

        } 
Androider
  • 803
  • 6
  • 18
  • @Androider : I have given this bt below expection is thrown : 05-04 17:26:05.161: WARN/System.err(4819): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/mnt/sdcard/audio-android.3gp } – jennifer May 04 '11 at 11:57
  • The above code is inside an Activity and dont forget to include that activity in Androidmanifest.xml. – Androider May 04 '11 at 12:07
  • @Androider:http://stackoverflow.com/questions/5882656/no-activity-found-to-handle-intent-android-intent-action-view – jennifer May 05 '11 at 03:56
  • Change intent.setDataAndType as intent.setDataAndType(data,"audio/3gpp"). if the type is video then change to intent.setDataAndType(data,"video/3gpp") or simply use intent.setData("video/*"). – Androider May 05 '11 at 09:36