Skip to content

Commit

Permalink
Merge pull request #75 from SimformSolutionsPvtLtd/fix/UNT-T28557_Aud…
Browse files Browse the repository at this point in the history
…io_play_from_seek_position_on_stop_to_play_mode

fix(UNT-T28557): Audio play from seek position on stop to play mode
  • Loading branch information
mukesh-simform authored Sep 10, 2024
2 parents 4447aee + c6e2b7e commit 3f31e64
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 6 deletions.
9 changes: 8 additions & 1 deletion android/src/main/java/com/audiowaveform/AudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ class AudioPlayer(
private var updateFrequency = UpdateFrequency.Low
private lateinit var audioPlaybackListener: CountDownTimer

fun preparePlayer(path: String?, volume: Int?, frequency: UpdateFrequency, promise: Promise) {
fun preparePlayer(
path: String?,
volume: Int?,
frequency: UpdateFrequency,
progress: Long,
promise: Promise
) {
if (path != null) {
isPlayerPrepared = false
updateFrequency = frequency
Expand All @@ -42,6 +48,7 @@ class AudioPlayer(
if (!isPlayerPrepared) {
if (state == Player.STATE_READY) {
player.volume = (volume ?: 1).toFloat()
player.seekTo(progress)
isPlayerPrepared = true
val duration = player.duration
promise.resolve(duration.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,19 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
val key = obj.getString(Constants.playerKey)
val frequency = obj.getInt(Constants.updateFrequency)
val volume = obj.getInt(Constants.volume)
val progress = if (!obj.hasKey(Constants.progress) || obj.isNull(Constants.progress)) {
0 // Set default progress to zero if null, undefined, or missing
} else {
obj.getInt(Constants.progress).toLong()
}

if (key != null) {
initPlayer(key)
audioPlayers[key]?.preparePlayer(
path,
volume,
getUpdateFrequency(frequency),
progress,
promise
)
} else {
Expand Down
4 changes: 2 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { generateAudioList, type ListItem } from './constants';
import stylesheet from './styles';
import { Colors } from './theme';

const ListItem = React.memo(
const RenderListItem = React.memo(
({
item,
currentPlaying,
Expand Down Expand Up @@ -244,7 +244,7 @@ const AppContainer = () => {
</View>
<ScrollView scrollEnabled={shouldScroll}>
{list.map(item => (
<ListItem
<RenderListItem
key={item.path}
currentPlaying={currentPlaying}
setCurrentPlaying={setCurrentPlaying}
Expand Down
3 changes: 2 additions & 1 deletion ios/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
super.init()
}

func preparePlayer(_ path: String?, volume: Double?, updateFrequency: UpdateFrequency, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
func preparePlayer(_ path: String?, volume: Double?, updateFrequency: UpdateFrequency, time: Double, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
if(!(path ?? "").isEmpty) {
self.updateFrequency = updateFrequency
let audioUrl = URL.init(string: path!)
Expand All @@ -40,6 +40,7 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate {
player = try AVAudioPlayer(contentsOf: audioUrl!)
player?.prepareToPlay()
player?.volume = Float(volume ?? 100.0)
player?.currentTime = Double(time / 1000)
resolve(true)
} catch let error as NSError {
reject(Constants.audioWaveforms, error.localizedDescription, error)
Expand Down
1 change: 1 addition & 0 deletions ios/AudioWaveform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class AudioWaveform: RCTEventEmitter {
audioPlayers[key!]?.preparePlayer(args?[Constants.path] as? String,
volume: args?[Constants.volume] as? Double,
updateFrequency: UpdateFrequency(rawValue: (args?[Constants.updateFrequency]) as? Double ?? 0) ?? UpdateFrequency.medium,
time: args?[Constants.progress] as? Double ?? 0,
resolver: resolve,
rejecter: reject)
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/components/Waveform/Waveform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {

const { checkHasAudioRecorderPermission } = useAudioPermission();

const preparePlayerForPath = async () => {
const preparePlayerForPath = async (progress?: number) => {
if (!isNil(path) && !isEmpty(path)) {
try {
const prepare = await preparePlayer({
path,
playerKey: `PlayerFor${path}`,
updateFrequency: UpdateFrequency.medium,
volume: volume,
progress,
});
return Promise.resolve(prepare);
} catch (err) {
Expand Down Expand Up @@ -205,7 +206,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
if (mode === 'static') {
try {
if (playerState === PlayerState.stopped) {
await preparePlayerForPath();
await preparePlayerForPath(currentProgress);
}

const play = await playPlayer({
Expand Down
1 change: 1 addition & 0 deletions src/types/AudioWaveformTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface IExtractWaveform extends IPlayerKey, IPlayerPath {
export interface IPreparePlayer extends IPlayerKey, IPlayerPath {
updateFrequency?: UpdateFrequency;
volume?: number;
progress?: number;
}

export interface IStartPlayer extends IPlayerKey {
Expand Down

0 comments on commit 3f31e64

Please sign in to comment.