如何在设备中使用默认的播放器在Android中播放3GP音频文件? [英] How to play 3gp audio file in android using default player in the device?

查看:166
本文介绍了如何在设备中使用默认的播放器在Android中播放3GP音频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要播放存储在我的SD卡位置的3GP音频文件,在我的HTC设备符合我的默认媒体播放器播放它。

请帮助

code:

 公共类AudioRecorder延伸活动{     私有静态最后弦乐CAMERA_STATUS =camera_upload;
        私有静态最后弦乐GALLERY_STATUS =gallery_upload;
        MediaRecorder记录=新MediaRecorder();
        静态字符串路径=音频android.3gp
        按钮的startRecording;
        按钮STO precording;
        按钮来保存;
        按钮palyAudio;
        私人上下文的背景下;
     私人字符串sanitizePath(字符串路径){
        如果(!path.startsWith(/)){
            路径=/+路径;
        }
        如果(!path.contains(。)){
            路径+ =名为.3gp;
        }
        返回Environment.getExternalStorageDirectory()。getAbsolutePath()
            +路径;
        }     @覆盖
        保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);        的setContentView(R.layout.audio_recording);
        上下文=这一点;
        / **
         *创建在给定的路径中的新录音(相对于根
         * SD卡)。
         * /        this.path = sanitizePath(路径);        的startRecording =(按钮)findViewById(R.id.startRecording);
        STO precording =(按钮)findViewById(R.id.sto precording);
        节省=(按钮)findViewById(R.id.resetRecording);
        palyAudio =(按钮)findViewById(R.id.playRecorded);startRecording.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(查看为arg0){
            尝试{
                STO precording.setVisibility(View.VISIBLE);
                startRecording.setVisibility(View.GONE);
                开始();            }赶上(IOException异常五){
                e.printStackTrace();
            }            }
        });        STO precording.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(查看为arg0){
            STO precording.setVisibility(View.GONE);
            startRecording.setVisibility(View.VISIBLE);
            尝试{
                            停止();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }            }
        });
        palyAudio.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(查看为arg0){
             playAudio();
             // startMediaPlayer();            }        });        }        / **
         *
         *播放录制的音频
         *
         * /
        公共无效playAudio(){
        尝试{            意向意图=新意图(android.content.Intent.ACTION_VIEW);
            乌里数据= Uri.parse(路径);
           //意向意图=新意图(Intent.ACTION_VIEW,数据);
            intent.setDataAndType(数据,音频/ MP3);
            startActivity(意向);
        }赶上(ActivityNotFoundException E){
            e.printStackTrace();
        }赶上(例外五){
            // TODO:处理异常
        }
        }
      / **
         *开始一个新的记录。
         * /
        公共无效的start()抛出IOException
        记录=新MediaRecorder();        字符串状态= android.os.Environment.getExternalStorageState();        如果(!state.equals(android.os.Environment.MEDIA_MOUNTED)){
            抛出新IOException异常(SD卡未安装,这是+状态
                +。);
        }        //确保目录,我们计划存储在记录存在
        文件目录=新的文件(路径).getParentFile();
        的System.out.println(开始()目录>中+目录);
        如果(directory.exists()及!&放大器;!directory.mkdirs()){
            抛出新IOException异常(路径文件无法被创建。);
        }        recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB); recorder.setOutputFile(路径);
        。录音机prepare();
        recorder.start();        }        / **
         *停止了已经previously开始录音。
         * /
        公共无效停止()抛出IOException
        recorder.stop(); //停止录制。
        recorder.release(); //现在对象不能重复使用        }
    }


解决方案

 公共无效的onCreate(捆绑冰柱){
        super.onCreate(冰柱);
        的setContentView(T);
        PATH =/sdcard/sample.mp3;
                    mMediaPlayer =新的MediaPlayer();
                    mMediaPlayer.setDataSource(路径);
                    mMediaPlayer prepare()。
                    mMediaPlayer.start();
    }

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..

Pls help

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

        }


    }

解决方案

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

这篇关于如何在设备中使用默认的播放器在Android中播放3GP音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆