2010-09-04 16 views
12

मैं AudioTrack कक्षा का उपयोग कर एंड्रॉइड में एक पीसीएम फ़ाइल चलाने की कोशिश कर रहा हूं। मैं फ़ाइल को ठीक से खेलने के लिए प्राप्त कर सकता हूं, लेकिन प्लेबैक समाप्त होने पर मैं विश्वसनीय रूप से बता नहीं सकता। AudioTrack.getPlayState कहता है कि प्लेबैक बंद हो गया है जब यह खेलना समाप्त नहीं हुआ है। मुझे AudioTrack.setNotificationMarkerPosition के साथ एक ही समस्या है, और मुझे पूरा यकीन है कि मेरा मार्कर फ़ाइल के अंत में सेट है (हालांकि मुझे पूरा यकीन नहीं है कि मैं इसे सही कर रहा हूं)। इसी प्रकार, प्लेबैक जारी रहता है जब getPlaybackHeadPosition फ़ाइल के अंत में होता है और वृद्धि को रोक दिया है। क्या कोई मदद कर सकता है?ऑडियोट्रैक ऑब्जेक्ट कब खेलना समाप्त हो गया है, यह कैसे बताना है?

उत्तर

14

मुझे पता चला कि audioTrack.setNotificationMarkerPosition (audioLength) और audioTrack.setPlaybackPositionUpdateListener का उपयोग करके मेरे लिए काम किया।

// Get the length of the audio stored in the file (16 bit so 2 bytes per short) 
    // and create a short array to store the recorded audio. 
    int audioLength = (int) (pcmFile.length()/2); 
    short[] audioData = new short[audioLength]; 
    DataInputStream dis = null; 

    try { 
     // Create a DataInputStream to read the audio data back from the saved file. 
     InputStream is = new FileInputStream(pcmFile); 
     BufferedInputStream bis = new BufferedInputStream(is); 
     dis = new DataInputStream(bis); 

     // Read the file into the music array. 
     int i = 0; 
     while (dis.available() > 0) { 
      audioData[i] = dis.readShort(); 
      i++; 
     } 

     // Create a new AudioTrack using the same parameters as the AudioRecord. 
     audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, RECORDER_SAMPLE_RATE, RECORDER_CHANNEL_OUT, 
            RECORDER_AUDIO_ENCODING, audioLength, AudioTrack.MODE_STREAM); 
     audioTrack.setNotificationMarkerPosition(audioLength); 
     audioTrack.setPlaybackPositionUpdateListener(new OnPlaybackPositionUpdateListener() { 
      @Override 
      public void onPeriodicNotification(AudioTrack track) { 
       // nothing to do 
      } 
      @Override 
      public void onMarkerReached(AudioTrack track) { 
       Log.d(LOG_TAG, "Audio track end of file reached..."); 
       messageHandler.sendMessage(messageHandler.obtainMessage(PLAYBACK_END_REACHED)); 
      } 
     }); 

     // Start playback 
     audioTrack.play(); 

     // Write the music buffer to the AudioTrack object 
     audioTrack.write(audioData, 0, audioLength); 

    } catch (Exception e) { 
     Log.e(LOG_TAG, "Error playing audio.", e); 
    } finally { 
     if (dis != null) { 
      try { 
       dis.close(); 
      } catch (IOException e) { 
       // don't care 
      } 
     } 
    } 
3

यह मेरे लिए काम करता है::

  do{              // Montior playback to find when done 
       x = audioTrack.getPlaybackHeadPosition(); 
     }while (x< pcmFile.length()/2); 
निम्नलिखित कोड देखें
संबंधित मुद्दे