Skip to content

Commit

Permalink
Fixing a bug in looping sound file
Browse files Browse the repository at this point in the history
  • Loading branch information
shlomi_h committed Oct 22, 2015
1 parent ee2d87b commit 7687b1f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 79 deletions.
Binary file modified dist/cassette.zip
Binary file not shown.
1 change: 0 additions & 1 deletion examples/Soundfile/Soundfile.pde
Expand Up @@ -10,7 +10,6 @@ void setup() {

music = new SoundFile(this, "Crickets.mp3");
music.loop();
music.play();
}

void draw() {
Expand Down
Binary file modified library/cassette.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion src/cassette/audiofiles/MediaPlayerHandlerCallback.java
Expand Up @@ -3,7 +3,8 @@
import android.content.res.AssetFileDescriptor;

public interface MediaPlayerHandlerCallback {
public void initPlayer(AssetFileDescriptor fileName);
public void startPlayer();
public void stopPlayer();
public void initPlayer(AssetFileDescriptor fileName);
public void setLoopingONPlayer();
}
162 changes: 85 additions & 77 deletions src/cassette/audiofiles/SoundFile.java
Expand Up @@ -20,106 +20,114 @@ public class SoundFile extends SoundBase implements MediaPlayerHandlerCallback {

private MediaPlayerHandler handler;
private MediaPlayer player;

private boolean looping = false;

public SoundFile(PApplet parent, String fileName) {
super(parent);
AssetFileDescriptor afd = null;
try {

public SoundFile(PApplet parent, String fileName) {
super(parent);
AssetFileDescriptor afd = null;
try {
afd = activity.getAssets().openFd(fileName);
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IOException e) {
e.printStackTrace();
}

HandlerThread backgroundThread = new HandlerThread("MediaPlayer");
backgroundThread.start();
handler = new MediaPlayerHandler(backgroundThread.getLooper());
handler.setCallback(this);
handler.sendMessage(handler.obtainMessage(MediaPlayerHandler.MSG_INIT_PLAYER, afd));
}

public void play() {
handler.sendMessage(handler.obtainMessage(MediaPlayerHandler.MSG_START_PLAYER));
}
HandlerThread backgroundThread = new HandlerThread("MediaPlayer");
backgroundThread.start();
handler = new MediaPlayerHandler(backgroundThread.getLooper());
handler.setCallback(this);
handler.sendMessage(handler.obtainMessage(MediaPlayerHandler.MSG_INIT_PLAYER, afd));
}

public void play() {
handler.sendMessage(handler.obtainMessage(MediaPlayerHandler.MSG_START_PLAYER));
}

public void stop() {
handler.sendMessage(handler.obtainMessage(MediaPlayerHandler.MSG_STOP_PLAYER));
}

public void stop() {
handler.sendMessage(handler.obtainMessage(MediaPlayerHandler.MSG_STOP_PLAYER));
}
public void loop() {
handler.sendMessage(handler.obtainMessage(MediaPlayerHandler.MSG_SETLOOPINGON_PLAYER));
handler.sendMessage(handler.obtainMessage(MediaPlayerHandler.MSG_START_PLAYER));

public void loop() {
looping = true;
}

@Override
public void onPause() {
}

@Override
public void onResume() {
}

@Override
public String getEventName() {
return "soundFileEvent";
}

private class MediaPlayerHandler extends Handler {

public static final int MSG_INIT_PLAYER = 0;
public static final int MSG_START_PLAYER = 1;
public static final int MSG_STOP_PLAYER = 2;

MediaPlayerHandlerCallback callback;

public MediaPlayerHandler(Looper looper) {
}

@Override
public void onPause() {
}

@Override
public void onResume() {
}

@Override
public String getEventName() {
return "soundFileEvent";
}

private class MediaPlayerHandler extends Handler {

public static final int MSG_INIT_PLAYER = 0;
public static final int MSG_START_PLAYER = 1;
public static final int MSG_STOP_PLAYER = 2;
public static final int MSG_SETLOOPINGON_PLAYER = 3;

MediaPlayerHandlerCallback callback;

public MediaPlayerHandler(Looper looper) {
super(looper);
}

public void setCallback (MediaPlayerHandlerCallback cb) {
this.callback = cb;
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_INIT_PLAYER:
AssetFileDescriptor afd = (AssetFileDescriptor) msg.obj;
callback.initPlayer(afd);
break;
this.callback = cb;
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_INIT_PLAYER:
AssetFileDescriptor afd = (AssetFileDescriptor) msg.obj;
callback.initPlayer(afd);
break;
case MSG_START_PLAYER:
callback.startPlayer();
break;
case MSG_STOP_PLAYER:
callback.stopPlayer();
break;
case MSG_STOP_PLAYER:
callback.stopPlayer();
break;
case MSG_SETLOOPINGON_PLAYER:
callback.setLoopingONPlayer();
break;
default:
break;
}
}
}
@Override
public void initPlayer(AssetFileDescriptor afd) {
player = new MediaPlayer();
try {
}
}
@Override
public void initPlayer(AssetFileDescriptor afd) {
player = new MediaPlayer();
try {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
player.setLooping(looping);
player.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void startPlayer() {
player.start();
}
}

@Override
public void startPlayer() {
player.start();
}

@Override
public void setLoopingONPlayer() {
player.setLooping(true);
}

@Override
public void stopPlayer() {
player.stop();
}
@Override
public void stopPlayer() {
player.stop();
}
}

0 comments on commit 7687b1f

Please sign in to comment.