如何将音频文件存储在内部存储中并读取并在Android中播放?

时间:2016-01-22 19:46:44

标签: android audio android-mediaplayer record internal-storage

我正在制作一个电话录音应用程序,在其中我将录制的mp3音频文件存储在内部存储data/data/mypckagename/myfilename中,然后我读了它并尝试播放它,但它无效。

我的录音代码:

                date = new SimpleDateFormat("yy-MM-dd").format(new Date(
                        System.currentTimeMillis()));
                String filename = new SimpleDateFormat("yy-MM-dd_HH-mm-ss")
                        .format(new Date(System.currentTimeMillis()));
                String fileName = filename + ".mp3";// ʵ¼ÊÊÇ3gp
                File mydir = getApplicationContext().getDir("mydir",
                        MODE_WORLD_READABLE);

                File recordName = new File(mydir, fileName);

                try {
                    recordName.createNewFile();
                    Log.d("recorder ", "´´½¨Îļþ " + recordName.getName());
                } catch (IOException e) {
                    e.printStackTrace();
                }

                recorder.setOutputFile(recordName.getAbsolutePath());
                recorder.prepare(); // Ô¤ÆÚ×¼±¸
                recorder.start(); // ¿ªÊ¼¿Ì¼
                record = true;
                Log.d(TAG, "start record");

                // add a new record to list
                String contactName = getContactName(
                        getApplicationContext(), phoneNumber);
                if (contactName == null || contactName.isEmpty())
                    contactName = phoneNumber;
                date = new SimpleDateFormat("dd-MM-yyyy").format(new Date(
                        System.currentTimeMillis()));
                time = getTime();

                CallInfo c = new CallInfo();
                c.setId(UUID.randomUUID());
                c.setName(contactName);
                c.setPhoneNumber(phoneNumber);
                c.setDate(date + " " + time);
                c.setTime("60s");
                c.setRecordFileName(mydir.getAbsolutePath() + "/"
                        + fileName);
                c.setIsIncoming(isIncoming);

                Log.d(TAG, "µç»°ºÅÂë: " + phoneNumber);

                CallLab.get(getApplicationContext()).addCall(c);

播放:

    File audioFile = new File(c.getRecordFileName());
    System.out.println("path=" + audioFile.toString());
    if (audioFile.exists() && audioFile.isFile()) {
        // È»ºóÐèÒª»ñÈ¡¸ÃÎļþµÄUri
        Uri audioUri = Uri.fromFile(audioFile);
        Log.d(TAG, audioUri.toString());
        // È»ºóÖ¸¶¨UriºÍMIME
        final MediaPlayer mediaPlayer=new MediaPlayer();
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(audioFile.toString());
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            mediaPlayer.setDataSource(audioFile.toString());

            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                public void onPrepared(MediaPlayer player) {
                    mediaPlayer.start();
                  }
                });
        } catch (IllegalArgumentException | IllegalStateException
                | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

但是没有播放录制的文件。

0 个答案:

没有答案