【问题标题】:How to play 3gp audio file in Android using default player in the device?如何使用设备中的默认播放器在 Android 中播放 3gp 音频文件?
【发布时间】:2011-05-04 11:21:25
【问题描述】:

我需要播放存储在我的 sd 卡位置的 3gp 音频文件,以便在我的 HTC 设备中使用我的默认媒体播放器播放它。

代码:

 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

        }


    }

【问题讨论】:

  • 你能告诉我如何找到你玩 3gp 的答案
  • Dr.nik 实际上.. 我录制了 auido 并将其保存在 sdcard 中的 3gp 中.. 之后我播放了音频...
  • 我知道但你能告诉我如何从 sdcard 播放 3gp 文件
  • 因为我玩起来很麻烦
  • mediaPlayerStopped = false;媒体播放器 = 新媒体播放器(); Uri 数据 = Uri.parse(path); mediaPlayer.setDataSource(上下文,数据); mediaPlayer.prepare(); mediaPlayer.start();

标签: android


【解决方案1】:
public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(t);
        path = "/sdcard/sample.mp3";
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDataSource(path);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
    }

【讨论】:

  • 我们可以调用任何播放音频的意图
【解决方案2】:

所以这实际上是一个相当大的问题,但这里是如何做到的:

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

关键是要设置视频 MIME,即使是音频文件。不要问我为什么会这样,它就是这样。

【讨论】:

    【解决方案3】:
    Uri playUri = Uri.parse("file:///sdcard/music/an.mp3");
    Intent intent = new Intent(Intent.ACTION_VIEW, playUri); 
    startActivity(intent);
    

    【讨论】:

    • 我已经给出了低于预期的这个 bt: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 }
    【解决方案4】:

    使用 VideoView(参见 example)来克服表面和状态的使用,它使用本机播放器。

    String path = getExternalStorageDirectory() + "/path/to/file/in/sdcard";
    VideoView vv = (VideoView)findViewById(R.id.myVideoViewId);
    vv.setVideoPath(path);
    vv.start();
    

    【讨论】:

      【解决方案5】:
      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:我已经给出了低于预期的这个 bt:05-04 17:26:05.161: WARN/System.err(4819): android.content.ActivityNotFoundException: No Activity found to handle意图 { act=android.intent.action.VIEW dat=/mnt/sdcard/audio-android.3gp }
      • 上面的代码在一个Activity里面,别忘了在Androidmanifest.xml中包含那个Activity。
      • 将 intent.setDataAndType 更改为 intent.setDataAndType(data,"audio/3gpp")。如果类型是视频,则更改为 intent.setDataAndType(data,"video/3gpp") 或简单地使用 intent.setData("video/*")。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多