bluefireteam / audioplayers

A Flutter package to play multiple audio files simultaneously (Android/iOS/web/Linux/Windows/macOS)
https://pub.dartlang.org/packages/audioplayers
MIT License
1.97k stars 838 forks source link

AudioPlayer.getDuration gives a incorrect duration when playing .m4a files on iOS. #376

Closed itsMatoosh closed 3 years ago

itsMatoosh commented 4 years ago

Using the AudioPlayer.getDuration method when playing a local (.m4a) file on iOS gives a duration in miliseconds which is 2 times longer than the actual duration of the track. Using the same code and the same music file on Android gives the correct duration.

Because the incorrect duration is given, the onPlayerCompletion event of the player doesn't fire at the end of the track but much later.

This could be caused by the library incorrectly reading the .m4a file metadata on iOS.

volgin commented 4 years ago

I play a mix of mp3 and m4a files using this plugin every day, and I never had a problem with the wrong duration. And none of my test users, who have different phones and iOS versions, have reported any issues related to wrong duration.

itsMatoosh commented 4 years ago

Hmm, that's weird. I'm using iPhone 6 to test the app and the problem always occurs. I ended up using a workaround where I listen to the onAudioPositionChanged stream and skip the current song when half of the given duration passes. This is not an ideal solution though :/

volgin commented 4 years ago

I have tested my app on iPhone 6, and the duration of .m4a files is correct - no workarounds necessary.

freddydrodev commented 4 years ago

in my case it gives me negative duration. my files are mp3

volgin commented 4 years ago

@frediustcDev This means there is a problem in your code. The duration is correct on all files. I am using this plugin right now to listen to music :)

monnguyen2512 commented 4 years ago

i same issue :(

luanpotter commented 3 years ago

@itsMatoosh please follow the issue template. We need a code example + sample file + platform so I can reproduce it. Otherwise I will close this issue.

erickzanardo commented 3 years ago

Closing due inactivity, if there is more info that you can add to the issue, comment here and we can re open the issue.

xshiwei commented 8 months ago

I also encountered this problem, the URL is https://rr5---sn-i3belnll.googlevideo.com/videoplayback?expire=1702644364&ei=LPZ7Zd33EvuM1d8PtrCy-Ao&ip=199.182.234.20&id=o-AACxtQ6WCufMClTnw5shWgf9KLqJJW9ejkg0NOz ncM9R&itag=139&source=youtube&requiressl=yes&xpc= EgVo2aDSNQ%3D%3D&gcr=hk&vprv=1&mime=audio%2Fmp4&gir=yes&clen=1004767&dur=164.467&lmt=1633610990078612&keepalive=yes&fexp=24007246&c=ANDROID_TESTSUITE&txp=23112 22&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cxpc%2Cgcr%2Cvprv %2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AJfQdSswRQIgUQQ_Uv3eJ9X05ka8CzOuu2VmxlzwqAFHjYn3pRVzMp0CIQCoqE7BivyHJYEUtfSCoY1yNd0P5U98GPXIot1vq8SbRA%3 D%3D&redirect_counter=1&rm=sn-i3bds76&req_id=53801a152221a3ee&cms_redirect=yes&cmsv=e&ipbypass=yes&mh=jB&mip=58.84.54.80&mm=31&mn=sn-i3belnll&ms=au&mt=1702623697&mv= m&mvi= 5&pl=24&lsparams=ipbypass,mh,mip,mm,mn,ms,mv,mvi,pl&lsig=AAO5W4owRQIgUMFgsiJGzB7PjMf84eui6aHsV15dmC5a-gIbM82RYMsCIQCfUKxc_MITE7t2mVn5K8VBCT_s18nlBgOMonP_b IgxiA%3D%3D

xshiwei commented 8 months ago
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';

class DebugMediaTestPage extends StatefulWidget {
  const DebugMediaTestPage({super.key});

  @override
  State<DebugMediaTestPage> createState() => _DebugMediaTestPageState();
}

class _DebugMediaTestPageState extends State<DebugMediaTestPage> {
  late AudioPlayer _youtubeAudio;

  @override
  void initState() {
    super.initState();
    _youtubeAudio = AudioPlayer();
    _youtubeAudio.eventStream.listen((event) {
      debugPrint('event: ${event.toString()}');
    });
  }

  Future playYoutubeStream() async {
    await _youtubeAudio.play(UrlSource('https://rr5---sn-i3belnll.googlevideo.com/videoplayback?expire=1702644364&ei=LPZ7Zd33EvuM1d8PtrCy-Ao&ip=199.182.234.20&id=o-AACxtQ6WCufMClTnw5shWgf9KLqJJW9ejkg0NOzncM9R&itag=139&source=youtube&requiressl=yes&xpc=EgVo2aDSNQ%3D%3D&gcr=hk&vprv=1&mime=audio%2Fmp4&gir=yes&clen=1004767&dur=164.467&lmt=1633610990078612&keepalive=yes&fexp=24007246&c=ANDROID_TESTSUITE&txp=2311222&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cxpc%2Cgcr%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AJfQdSswRQIgUQQ_Uv3eJ9X05ka8CzOuu2VmxlzwqAFHjYn3pRVzMp0CIQCoqE7BivyHJYEUtfSCoY1yNd0P5U98GPXIot1vq8SbRA%3D%3D&redirect_counter=1&rm=sn-i3bds76&req_id=53801a152221a3ee&cms_redirect=yes&cmsv=e&ipbypass=yes&mh=jB&mip=58.84.54.80&mm=31&mn=sn-i3belnll&ms=au&mt=1702623697&mv=m&mvi=5&pl=24&lsparams=ipbypass,mh,mip,mm,mn,ms,mv,mvi,pl&lsig=AAO5W4owRQIgUMFgsiJGzB7PjMf84eui6aHsV15dmC5a-gIbM82RYMsCIQCfUKxc_MITE7t2mVn5K8VBCT_s18nlBgOMonP_bIgxiA%3D%3D'));
  }

  @override
  void dispose() {
    _youtubeAudio.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          ElevatedButton(
            onPressed: () {
              playYoutubeStream();
            },
            child: const Text('播放YoutubeStream'),
          ),
        ],
      ),
    );
  }
}
xshiwei commented 8 months ago
image
Gustl22 commented 8 months ago

I also encountered this problem, the URL is https://rr5---sn-i3belnll.googlevideo.com...

The url does not work. It includes spaces.

xshiwei commented 8 months ago

我也遇到了这个问题,网址是https://rr5---sn-i3belnll.googlevideo.com ...

该网址不起作用。它包括空格。

You can use the URL in my code

atvbasha commented 5 months ago

@xshiwei the code you provided is not working for me!