/** * Decodes an audio file (mp3, flac, wav, etc. everything which can be decoded by ffmpeg) to a * downsampled wav file. * * @param input an audio file which will be decoded to wav * @param wavoutput the output wav file * @param samplerate the samplerate of the output wav. * @throws IllegalArgumentException * @throws InputFormatException * @throws EncoderException */ public static void decodeAudioFile(File input, File wavoutput, int samplerate) throws IllegalArgumentException, InputFormatException, EncoderException { assert wavoutput.getName().endsWith(".wav"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("pcm_s16le"); audio.setChannels(Integer.valueOf(1)); audio.setSamplingRate(new Integer(samplerate)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("wav"); attrs.setAudioAttributes(audio); Encoder encoder = new Encoder(); encoder.encode(input, wavoutput, attrs); }
public void convertWavToMP3(String filepathToConvert, String convertedFilePath) { Encoder wavToMp3Converter = new Encoder(); EncodingAttributes att = new EncodingAttributes(); AudioAttributes audioAtt = new AudioAttributes(); String codecs[] = null; String formats[] = null; try { formats = wavToMp3Converter.getSupportedDecodingFormats(); } catch (EncoderException e) { e.printStackTrace(); } try { codecs = wavToMp3Converter.getAudioEncoders(); } catch (EncoderException e) { e.printStackTrace(); } int mp3CodecIndex = -1; /*for(int i=codecs.length; i>0; i--) { if(codecs[i].compareTo("") == 0) { mp3CodecIndex = i; break; } }*/ audioAtt.setVolume(255); audioAtt.setSamplingRate(new Integer(this.getWavTagReader().sampleRate)); audioAtt.setChannels(new Integer(wavTagReader.numOfChannels)); audioAtt.setBitRate( new Integer( this.wavTagReader.sampleRate * this.wavTagReader.bitsPerSample * this.wavTagReader.numOfChannels)); audioAtt.setCodec("libmp3lame"); att.setAudioAttributes(audioAtt); att.setFormat("mp3"); att.setOffset(new Float((float) 0)); att.setVideoAttributes(null); // att.setDuration(new Float((float)100)); try { wavToMp3Converter.encode( new File(filepathToConvert), new File(convertedFilePath), att, new EncoderProgressListener() { @Override public void sourceInfo(MultimediaInfo multimediaInfo) {} @Override public void progress(int i) {} @Override public void message(String s) {} }); } catch (EncoderException e) { e.getMessage(); e.getCause(); } }
/** Plays the ringtone. */ public void play() { if (mLocalPlayer != null) { // do not play ringtones if stream volume is 0 // (typically because ringer mode is silent). if (mAudioManager.getStreamVolume(AudioAttributes.toLegacyStreamType(mAudioAttributes)) != 0) { mLocalPlayer.start(); } } else if (mAllowRemote && (mRemotePlayer != null)) { final Uri canonicalUri = mUri.getCanonicalUri(); try { mRemotePlayer.play(mRemoteToken, canonicalUri, mAudioAttributes); } catch (RemoteException e) { if (!playFallbackRingtone()) { Log.w(TAG, "Problem playing ringtone: " + e); } } } else { if (!playFallbackRingtone()) { Log.w(TAG, "Neither local nor remote playback available"); } } }
private boolean playFallbackRingtone() { if (mAudioManager.getStreamVolume(AudioAttributes.toLegacyStreamType(mAudioAttributes)) != 0) { int subId = RingtoneManager.getDefaultRingtoneSubIdByUri(mUri); if (subId != -1 && RingtoneManager.getActualRingtoneUriBySubId(mContext, subId) != null) { // Default ringtone, try fallback ringtone. try { AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(com.android.internal.R.raw.fallbackring); if (afd != null) { mLocalPlayer = new MediaPlayer(); if (afd.getDeclaredLength() < 0) { mLocalPlayer.setDataSource(afd.getFileDescriptor()); } else { mLocalPlayer.setDataSource( afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength()); } mLocalPlayer.setAudioAttributes(mAudioAttributes); mLocalPlayer.prepare(); mLocalPlayer.start(); afd.close(); return true; } else { Log.e(TAG, "Could not load fallback ringtone"); } } catch (IOException ioe) { destroyLocalPlayer(); Log.e(TAG, "Failed to open fallback ringtone"); } catch (NotFoundException nfe) { Log.e(TAG, "Fallback ringtone does not exist"); } } else { Log.w(TAG, "not playing fallback for " + mUri); } } return false; }
/** * Converts WAV file to MP3 * * @param filePath path on which MP3 file will be saved */ public void convertWavToMP3(String filePath) { String codecs[]; String formats[]; Encoder encoder = new Encoder(); EncodingAttributes att = new EncodingAttributes(); try { codecs = encoder.getAudioEncoders(); formats = encoder.getSupportedEncodingFormats(); AudioAttributes audioAtt = new AudioAttributes(); audioAtt.setBitRate( this.wavTagReader.getByteRate() * this.wavTagReader.getBitsPerSample() * this.wavTagReader.getNumOfChannels()); audioAtt.setChannels(Integer.valueOf(this.wavTagReader.getNumOfChannels())); audioAtt.setSamplingRate(this.wavTagReader.getSampleRate()); audioAtt.setVolume(255); audioAtt.setCodec(AudioAttributes.DIRECT_STREAM_COPY); att.setAudioAttributes(audioAtt); att.setFormat("mp3"); encoder.encode(this.file, new File(filePath), att); } catch (EncoderException e) { e.printStackTrace(); } }
/** * Gets the stream type where this ringtone will be played. * * @return The stream type, see {@link AudioManager}. * @deprecated use of stream types is deprecated, see {@link #setAudioAttributes(AudioAttributes)} */ @Deprecated public int getStreamType() { return AudioAttributes.toLegacyStreamType(mAudioAttributes); }