Skip to content
This repository has been archived by the owner on Jan 20, 2023. It is now read-only.

Use replaceCurrentItem to allow playing different audio files #7

Merged
merged 3 commits into from
Jul 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A Flutter audio plugin.
- [x] play (remote and local file)
- [x] stop
- [x] pause
- [x] seek
- [x] onComplete
- [x] onDuration / onCurrentPosition

Expand Down Expand Up @@ -40,7 +41,7 @@ AudioPlayer audioPlayer = new AudioPlayer();
//...
```

### play, pause , stop
### play, pause , stop, seek

```dart
play() async {
Expand All @@ -65,6 +66,9 @@ stop() async {
if (result == 1) setState(() => playerState = PlayerState.stopped);
}

// seek 5 seconds from the beginning
audioPlayer.seek(5.0);

```

### duration, position, complete, error (temporary api)
Expand Down
16 changes: 13 additions & 3 deletions android/src/main/java/bz/rxla/audioplayer/AudioplayerPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,26 @@ public void onMethodCall(MethodCall call, MethodChannel.Result response) {
} else if (call.method.equals("stop")) {
stop();
response.success(1);
} else if (call.method.equals("seek")) {
double position = call.arguments();
seek(position);
response.success(1);
} else {
response.notImplemented();
}
}

private void seek(double position) {
mediaPlayer.seekTo((int) (position * 1000));
}

private void stop() {
handler.removeCallbacks(sendData);
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}

private void pause() {
Expand Down
46 changes: 31 additions & 15 deletions ios/Classes/SwiftAudioplayerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public class SwiftAudioplayerPlugin: NSObject, FlutterPlugin {
pause()
case "stop":
stop()
case "seek":
guard let sec:Double = call.arguments as? Double else {
result(0)
return
}
seek(sec)
default:
result(FlutterMethodNotImplemented)
}
Expand All @@ -56,25 +62,30 @@ public class SwiftAudioplayerPlugin: NSObject, FlutterPlugin {

fileprivate func togglePlay(_ url: String, isLocal:Bool) {
print( "togglePlay \(url)" )
if player == nil {
if url != lastUrl {
playerItem = AVPlayerItem(url: isLocal ? URL(fileURLWithPath:url): URL(string: url)!)
lastUrl = url

// soundComplete handler
NotificationCenter.default.addObserver(
forName: Notification.Name.AVPlayerItemDidPlayToEndTime,
object: playerItem,
queue: nil, using: onSoundComplete)

if url != lastUrl {
playerItem?.removeObserver(self, forKeyPath: #keyPath(player.currentItem.status))
NotificationCenter.default.removeObserver(onSoundComplete)

playerItem = AVPlayerItem(url: isLocal ? URL(fileURLWithPath:url): URL(string: url)!)
lastUrl = url

// soundComplete handler
NotificationCenter.default.addObserver(
forName: Notification.Name.AVPlayerItemDidPlayToEndTime,
object: playerItem,
queue: nil, using: onSoundComplete)

if let p = player{
p.replaceCurrentItem(with: playerItem)
} else {
player = AVPlayer(playerItem: playerItem)

// is sound ready
player!.currentItem?.addObserver(self, forKeyPath: #keyPath(player.currentItem.status), context: nil)


// stream player position
player!.addPeriodicTimeObserver(forInterval: CMTime(seconds: 0.2, preferredTimescale: CMTimeScale(NSEC_PER_SEC)), queue: nil, using: onTimeInterval)
}

// is sound ready
player!.currentItem?.addObserver(self, forKeyPath: #keyPath(player.currentItem.status), context: nil)
}

if isPlaying == true {
Expand Down Expand Up @@ -119,6 +130,11 @@ public class SwiftAudioplayerPlugin: NSObject, FlutterPlugin {
print("stop")
}
}

func seek(_ seconds: Double) {
let time = CMTime.init(seconds: seconds, preferredTimescale: 1);
playerItem?.seek(to: time);
}

func onSoundComplete(note: Notification) {
print("ios -> onSoundComplete...")
Expand Down
2 changes: 2 additions & 0 deletions lib/audioplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class AudioPlayer {

Future<int> stop() => _channel.invokeMethod('stop');

Future<int> seek(double seconds) => _channel.invokeMethod('seek', seconds);

void setDurationHandler(TimeChangeHandler handler) {
durationHandler = handler;
}
Expand Down