2013-03-13 9 views
11

प्राप्त करें मैंने एक वॉयस रिकॉर्डर ऐप बनाया है, और मैं एक सूचीदृश्य में रिकॉर्डिंग की अवधि दिखाना चाहता हूं। मैं इस तरह के रिकॉर्डिंग को सहेजता हूं:ऑडियो फ़ाइल की अवधि

MediaRecorder recorder = new MediaRecorder(); 
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
folder = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "Audio recordings"); 
String[] files = folder.list(); 
    int number = files.length + 1; 
    String filename = "AudioSample" + number + ".mp3"; 
    File output = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "Audio recordings" + File.separator 
      + filename); 
    FileOutputStream writer = new FileOutputStream(output); 
    FileDescriptor fd = writer.getFD(); 
    recorder.setOutputFile(fd); 
    try { 
     recorder.prepare(); 
     recorder.start(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     Log.e(LOG_TAG, "prepare() failed"); 
     e.printStackTrace(); 
    } 

मैं इस फ़ाइल के सेकंड में अवधि कैसे प्राप्त कर सकता हूं?

अग्रिम धन्यवाद

--- संपादित मैं यह काम कर रहा है, मैं MediaPlayer.getduration() MediaPlayer.setOnPreparedListener अंदर() विधि तो यह वापस आ 0.

उत्तर

0

के बाद आप फ़ाइल में लिखने कहा जाता है , इसे मीडियाप्लेयर में खोलें, और उस पर getDuration कॉल करें।

+0

की कोशिश की है कि का उपयोग कर मानव पठनीय प्रारूप करने के लिए मिलीसेकंड में बदल सकते हैं लेकिन यह हमेशा किसी कारण – Simon

+1

के लिए 0 देता है कोई फर्क नहीं पड़ता कि मैं इसे काम कर रहा हूँ! मैंने MediaPlayer.setOnPreparedListener विधि में MediaPlayer.getDuration() को कॉल किया है और यह 0 – Simon

10

कोशिश उपयोग 1000 पर

long totalDuration = mediaPlayer.getDuration(); // to get total duration in milliseconds 

long currentDuration = mediaPlayer.getCurrentPosition(); // to Gets the current playback position in milliseconds 

डिवीजन सेकंड में बदलने के लिए।

आशा है कि इससे आपकी मदद मिलेगी।

+1

MediaPlayer.getduration() कुछ अजीब कारणों से 0 देता है, क्या आपको पता है क्यों? – Simon

19

या तो यह कोशिश मिलीसेकेंड में अवधि पाने के लिए:

MediaPlayer mp = MediaPlayer.create(yourActivity, Uri.parse(pathofyourrecording)); 
int duration = mp.getDuration(); 

या मापने के समय नैनोसेकंड में recorder.stop() तक recorder.start() से गुजरे:

long startTime = System.nanoTime();  
// ... do recording ...  
long estimatedTime = System.nanoTime() - startTime; 
+0

पहला वाला 0 देता है और यदि मैं दूसरे समाधान का उपयोग करना चाहता हूं तो मुझे फ़ाइल खेलना है और मैं नहीं चाहता कि – Simon

+0

आपको दूसरी फ़ाइल के लिए फ़ाइल चलाने की आवश्यकता नहीं है। जब आप रिकॉर्डिंग बंद करते हैं तो उपयोगकर्ता रिकॉर्डिंग शुरू करता है और अनुमानित समय की गणना करता है जब आप स्टार्टटाइम सेट करते हैं। पहले विकल्प के लिए, सुनिश्चित करें कि आप MediaPlayer को प्रारंभ करने से पहले अपने MediaRecorder को रिलीज़ करें। यदि यह अभी भी 0 लौट रहा है, तो एक बग होने की संभावना है। मैं विभिन्न ऑडियो प्रारूपों का प्रयास करूंगा। – Erol

+0

यह समाधान कुछ उपकरणों पर काम करता है लेकिन दूसरों पर नहीं। कोई विचार क्यों? नेक्सस 5 पर काम करता है लेकिन हडल नहीं। क्या यह मीडिया प्रकारों का समर्थन किया जा सकता है? जिस फ़ाइल को मैं पढ़ने की कोशिश कर रहा हूं वह एक h264 mp4 है। – speedynomads

1

आप Ringdroid को देखा है?। यह बहुत हल्का वजन है और एकीकरण सीधे आगे है। यह वीबीआर मीडिया फ़ाइलों के साथ भी अच्छी तरह से काम करता है।

अवधि प्राप्त करने में आपकी समस्या के लिए, आप Ringdroid का उपयोग करके नीचे कुछ ऐसा करना चाहेंगे।

public class AudioUtils 
{ 
    public static long getDuration(CheapSoundFile cheapSoundFile) 
    { 
     if(cheapSoundFile == null) 
      return -1; 
     int sampleRate = cheapSoundFile.getSampleRate(); 
     int samplesPerFrame = cheapSoundFile.getSamplesPerFrame(); 
     int frames = cheapSoundFile.getNumFrames(); 
     cheapSoundFile = null; 
     return 1000 * (frames * samplesPerFrame)/sampleRate; 
    } 

    public static long getDuration(String mediaPath) 
    { 
     if(mediaPath != null && mediaPath.length() > 0) 
      try 
      { 
       return getDuration(CheapSoundFile.create(mediaPath, null)); 
      }catch (FileNotFoundException e){} 
      catch (IOException e){} 
     return -1; 
    } 
} 

आशा है कि मदद करता है

39

MediaMetadataRetriever यह करने के लिए एक हल्के और प्रभावी तरीका है। MediaPlayer

इसके अलावा, Error (100,0) के बाद से यह एक भारी है और कभी कभी पुनः आरंभ की जरूरत है फिर से और फिर से किया जाना MediaPlayer पर भी हो सकता है बहुत भारी है और स्क्रॉल, पेजिंग, लिस्टिंग की तरह उच्च निष्पादन वातावरण में प्रदर्शन मुद्दा पैदा कर सकता है, आदि।

Uri uri = Uri.parse(pathStr); 
MediaMetadataRetriever mmr = new MediaMetadataRetriever(); 
mmr.setDataSource(AppContext.getAppContext(),uri); 
String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); 
int millSecond = Integer.parseInt(durationStr); 
+0

मीडियाप्लेयर.getDuration() में कम से कम mp4 के लिए एक बग प्रतीत होता है क्योंकि यह खोजना अच्छा था। धन्यवाद! –

+0

मैं MediaMetadataCompat.METADATA_KEY_DURATION कैसे प्राप्त करूं? –

3

करने का सबसे तेज़ तरीका MediaMetadataRetriever के माध्यम से करना है। हालांकि, वहाँ एक पकड़

यदि आप डेटा स्रोत सेट करने के लिए यूआरआई और संदर्भ का उपयोग आप बग का सामना कर सकते https://code.google.com/p/android/issues/detail?id=35794

समाधान फ़ाइल का पूर्ण पथ का उपयोग मीडिया फ़ाइल के मेटाडाटा को पुनः प्राप्त करने के किया जाता है।

नीचे तो

private static String getDuration(File file) { 
       MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); 
       mediaMetadataRetriever.setDataSource(file.getAbsolutePath()); 
       String durationStr = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); 
       return Utils.formateMilliSeccond(Long.parseLong(durationStr)); 
      } 

करने के लिए कोड का टुकड़ा है अब आप नीचे से किसी भी प्रारूप

 /** 
     * Function to convert milliseconds time to 
     * Timer Format 
     * Hours:Minutes:Seconds 
     */ 
     public static String formateMilliSeccond(long milliseconds) { 
      String finalTimerString = ""; 
      String secondsString = ""; 

      // Convert total duration into time 
      int hours = (int) (milliseconds/(1000 * 60 * 60)); 
      int minutes = (int) (milliseconds % (1000 * 60 * 60))/(1000 * 60); 
      int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60)/1000); 

      // Add hours if there 
      if (hours > 0) { 
       finalTimerString = hours + ":"; 
      } 

      // Prepending 0 to seconds if it is one digit 
      if (seconds < 10) { 
       secondsString = "0" + seconds; 
      } else { 
       secondsString = "" + seconds; 
      } 

      finalTimerString = finalTimerString + minutes + ":" + secondsString; 

    //  return String.format("%02d Min, %02d Sec", 
    //    TimeUnit.MILLISECONDS.toMinutes(milliseconds), 
    //    TimeUnit.MILLISECONDS.toSeconds(milliseconds) - 
    //      TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds))); 

      // return timer string 
      return finalTimerString; 
     } 
संबंधित मुद्दे