Skip to content

Commit

Permalink
Updated audio API
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-stepien committed Apr 10, 2016
1 parent ca3af9d commit d823ed5
Show file tree
Hide file tree
Showing 11 changed files with 559 additions and 2,075 deletions.
7 changes: 7 additions & 0 deletions app/app.iml
Expand Up @@ -66,14 +66,21 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/retrolambda" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down
1 change: 1 addition & 0 deletions library/library.iml
Expand Up @@ -65,6 +65,7 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
Expand Down
Expand Up @@ -2,8 +2,6 @@

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
Expand All @@ -20,12 +18,12 @@
import android.widget.ImageButton;
import android.widget.TextView;
import com.semantive.waveformandroid.R;
import com.semantive.waveformandroid.waveform.soundfile.CheapSoundFile;
import com.semantive.waveformandroid.waveform.soundfile.SamplePlayer;
import com.semantive.waveformandroid.waveform.soundfile.SoundFile;
import com.semantive.waveformandroid.waveform.view.MarkerView;
import com.semantive.waveformandroid.waveform.view.WaveformView;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/*
Expand Down Expand Up @@ -57,7 +55,7 @@ public abstract class WaveformFragment extends Fragment implements MarkerView.Ma
protected long mLoadingLastUpdateTime;
protected boolean mLoadingKeepGoing;
protected ProgressDialog mProgressDialog;
protected CheapSoundFile mSoundFile;
protected SoundFile mSoundFile;
protected File mFile;
protected String mFilename;
protected WaveformView mWaveformView;
Expand Down Expand Up @@ -87,7 +85,7 @@ public abstract class WaveformFragment extends Fragment implements MarkerView.Ma
protected int mPlayEndMsec;
protected Handler mHandler;
protected boolean mIsPlaying;
protected MediaPlayer mPlayer;
protected SamplePlayer mPlayer;
protected boolean mTouchDragging;
protected float mTouchStart;
protected int mTouchInitialOffset;
Expand Down Expand Up @@ -411,7 +409,7 @@ protected void loadFromFile() {
mProgressDialog.setOnCancelListener((DialogInterface dialog) -> mLoadingKeepGoing = false);
mProgressDialog.show();

final CheapSoundFile.ProgressListener listener = (double fractionComplete) -> {
final SoundFile.ProgressListener listener = (double fractionComplete) -> {
long now = System.currentTimeMillis();
if (now - mLoadingLastUpdateTime > 100) {
mProgressDialog.setProgress(
Expand All @@ -421,26 +419,12 @@ protected void loadFromFile() {
return mLoadingKeepGoing;
};

// Create the MediaPlayer in a background thread
new Thread() {
public void run() {
try {
MediaPlayer player = new MediaPlayer();
player.setDataSource(mFile.getAbsolutePath());
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepare();
mPlayer = player;
} catch (final java.io.IOException e) {
Log.e(TAG, "Error while creating media player", e);
}
}
}.start();

// Load the sound file in a background thread
new Thread() {
public void run() {
try {
mSoundFile = CheapSoundFile.create(mFile.getAbsolutePath(), listener);
mSoundFile = SoundFile.create(mFile.getAbsolutePath(), listener);
mPlayer = new SamplePlayer(mSoundFile);
} catch (final Exception e) {
Log.e(TAG, "Error while loading sound file", e);
mProgressDialog.dismiss();
Expand Down Expand Up @@ -715,42 +699,16 @@ protected synchronized void onPlay(int startPosition) {
} else {
mPlayEndMsec = mWaveformView.pixelsToMillisecs(mEndPos);
}

mPlayStartOffset = 0;

int startFrame = mWaveformView.secondsToFrames(mPlayStartMsec * 0.001);
int endFrame = mWaveformView.secondsToFrames(mPlayEndMsec * 0.001);
int startByte = mSoundFile.getSeekableFrameOffset(startFrame);
int endByte = mSoundFile.getSeekableFrameOffset(endFrame);
if (startByte >= 0 && endByte >= 0) {
try {
mPlayer.reset();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
FileInputStream subsetInputStream = new FileInputStream(mFile.getAbsolutePath());
mPlayer.setDataSource(subsetInputStream.getFD(),startByte, endByte - startByte);
mPlayer.prepare();
mPlayStartOffset = mPlayStartMsec;
} catch (Exception e) {
Log.e(TAG, "Exception trying to play file subset", e);
mPlayer.reset();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(mFile.getAbsolutePath());
mPlayer.prepare();
mPlayStartOffset = 0;
}
}

mPlayer.setOnCompletionListener((MediaPlayer mediaPlayer) -> handlePause());
mPlayer.setOnCompletionListener(() -> handlePause());
mIsPlaying = true;

if (mPlayStartOffset == 0) {
mPlayer.seekTo(mPlayStartMsec);
}
mPlayer.seekTo(mPlayStartMsec);
mPlayer.start();
updateDisplay();
enableDisableButtons();
} catch (Exception e) {
Log.e(TAG, "Exception while playing file", e);
Log.e(TAG, "Error while playing sound file", e);
mInfo.setText(e.toString());
}
}

Expand Down

0 comments on commit d823ed5

Please sign in to comment.