React-native-track-player: useTrackPlayerProgress() -> duration get 0 only Android

Created on 28 Oct 2019  路  6Comments  路  Source: react-native-kit/react-native-track-player

Configuration

System:
OS: macOS 10.14.5
CPU: (4) x64 Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
Memory: 30.26 MB / 12.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 11.10.1 - /usr/local/bin/node
Yarn: 1.12.3 - /usr/local/bin/yarn
npm: 6.7.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2
Android SDK:
API Levels: 27, 28, 29
Build Tools: 28.0.3, 29.0.1
System Images: android-28 | Intel x86 Atom, android-28 | Google Play Intel x86 Atom, android-29 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom
Android NDK: 20.0.5594570
IDEs:
Android Studio: 3.4 AI-183.6156.11.34.5522156
Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
npmPackages:
react: 16.8.6 => 16.8.6
react-native: 0.60.0 => 0.60.0

"react-native-track-player": "^1.1.8"

Code

import { useTrackPlayerProgress } from 'react-native-track-player'
const progress = useTrackPlayerProgress()

Android Question

Most helpful comment

I have some problems in version ^2.0.0-rc13. To solve this i create a hook.

`import {useState, useEffect} from 'react';

import moment from 'moment';
import TrackPlayer from 'react-native-track-player';
export default function hooks() {
const [data, setData] = useState({
position: 0,
duration: 0,
positionString: '00:00:00',
durationString: '00:00:00',
});
useEffect(() => {
function transformTimes(t) {
return moment()
.startOf('day')
.seconds(t)
.format('H:mm:ss');
}
async function getInformations() {
const position = await TrackPlayer.getPosition();
const duration = await TrackPlayer.getDuration();
const positionString = transformTimes(position);
const durationString = transformTimes(duration);
setData({position, duration, positionString, durationString});
}
const time = setInterval(() => getInformations(), 1000);
return () => {
clearInterval(time);
setData({
position: 0,
duration: 0,
positionString: '00:00:00',
durationString: '00:00:00',
});
};
}, []);
return data;
}
`

All 6 comments

same here

Does TrackPlayer.getDuration() also returns 0?

@Guichaguri useTrackPlayerProgress() use TrackPlayer.getDuration() https://prnt.sc/pq8b1i and return 0 too

Are you setting the duration parameter in the track object? getDuration() is not guaranteed to always work (read the note here)

I'm having the same issue, despite setting the duration when adding a track.

I have some problems in version ^2.0.0-rc13. To solve this i create a hook.

`import {useState, useEffect} from 'react';

import moment from 'moment';
import TrackPlayer from 'react-native-track-player';
export default function hooks() {
const [data, setData] = useState({
position: 0,
duration: 0,
positionString: '00:00:00',
durationString: '00:00:00',
});
useEffect(() => {
function transformTimes(t) {
return moment()
.startOf('day')
.seconds(t)
.format('H:mm:ss');
}
async function getInformations() {
const position = await TrackPlayer.getPosition();
const duration = await TrackPlayer.getDuration();
const positionString = transformTimes(position);
const durationString = transformTimes(duration);
setData({position, duration, positionString, durationString});
}
const time = setInterval(() => getInformations(), 1000);
return () => {
clearInterval(time);
setData({
position: 0,
duration: 0,
positionString: '00:00:00',
durationString: '00:00:00',
});
};
}, []);
return data;
}
`

Was this page helpful?
0 / 5 - 0 ratings

Related issues

moduval picture moduval  路  3Comments

mckmarc picture mckmarc  路  4Comments

JakeMotta picture JakeMotta  路  3Comments

b3rkaydem1r picture b3rkaydem1r  路  3Comments

ManrickCapotolan picture ManrickCapotolan  路  4Comments