ryanheise / just_audio

Audio Player
1.03k stars 638 forks source link

Go back to live audio after pause or stop #96

Open diego-lipinski-de-castro opened 4 years ago

diego-lipinski-de-castro commented 4 years ago

Describe the solution you'd like When streaming, I would like a way to go back to the currently live audio after the user has paused or stopped the player.

Currently, If i stop or pause the player, and play again, it will play the audio as a playback, not what is live at the moment, and I cant find a way to stop that.

Im not sure If Im clear on the problem, Im not sure how I can explain a better way

lkho commented 4 years ago

Feature suggestion: add a way to seek to the end of live streams

Android: Player.seekToDefaultPosition() or Player.seekTo(C.TIME_UNSET) iOS: https://stackoverflow.com/a/13892727 [player seekToTime: CMTimeMakeWithSeconds(MAXFLOAT, NSEC_PER_SEC)];

ryanheise commented 4 years ago

Hi @diego-lipinski-de-castro @lkho 's fix is now available in the latest 0.2.0 release. To use it, call player.seek(null) to seek to the end of the live stream. Let me know how it goes.

diego-lipinski-de-castro commented 4 years ago

Worked on Android, on iOS (13.5) it does nothing tho

ryanheise commented 4 years ago

Hi @diego-lipinski-de-castro

On line 77 of ios/Classes/AudioPlayer.m would you be able to try changing kCMTimePositiveInfinity to CMTimeMakeWithSeconds(MAXFLOAT, NSEC_PER_SEC) and let me know if it works in your case?

Another alternative to try is to replace lines 77-78 with:

if (args[0] == [NSNull null]) {
    NSValue *value = player.currentItem.seekableTimeRanges.lastObject;
    if (value) {
        CMTimeRange seekableRange = [value CMTimeRangeValue];
        CMTime latestTime = CMTimeRangeGetEnd(seekableRange);
        [self seek:latestTime result:result];
    }
} else {
    [self seek:CMTimeMake([args[0] intValue], 1000)];
}
diego-lipinski-de-castro commented 4 years ago

None of these worked, I also tried some more options described on the stackoverflow link above but couldnt get it to work, but I know just about nothing of iOS development, so I was just trying copying paste, some options gave errors, sometimes it throws errors about the @avaiable condition which I dont know why it does that

ryanheise commented 4 years ago

Unfortunately I don't have a particularly ideal setup for testing but if you can copy and paste here which alternatives gave errors, I can help rewrite it to avoid those errors, if you can then run it and verify for me whether it fixes your issue.

As some background, those errors just happen when you use a feature that requires a certain version of iOS or macOS and you have to put if statements in your code to check that the API is available before using that feature. E.g.:

    if (@available(macOS 10.13, iOS 11.0, *)) {
        // This does the best at reducing distortion on voice
        // with speeds below 1.0
        playerItem.audioTimePitchAlgorithm = AVAudioTimePitchAlgorithmTimeDomain;
    }
diego-lipinski-de-castro commented 4 years ago

I did a background check about the @avaiable problem, by the logs i think the error was happening only on the negations, where if(!@avaiable) was used, later today I will try again and come back with what i have tried and the real logs

CpHeat commented 2 years ago

Just to let you know seek(null) is not working on iOS. For now I have to do a stop() then play() to get to the actual live but it has a bad feeling since it puts the notification on offline mode for a sec (on iOS only). I also tried with a fantaisist seek(Duration(hours: 99)) but it didn't do nothing on iOS either, it just played back to where it paused like seek(null) does. I'm experimenting on iOS 12 That could be a very nice feature to implement if you could

ryanheise commented 2 years ago

Perhaps it would be a good idea to add a live streaming example to the repository since that will also help my to test this use case.

If you have suggestions for live streams that I could use, please post links below. Ideally a stream that is supported on both iOS and Android, and which streams metadata of what is currently playing.

ryanheise commented 2 years ago

@CpHeat can you provide a link to the stream you were testing? Add the time @lkho came up with the solution, I'm assuming it worked, but maybe that's to do with the iOS version and also possibly to particular stream.

Providing some test urls will be helpful in experimenting with solutions.

CpHeat commented 2 years ago

@CpHeat can you provide a link to the stream you were testing? Add the time @lkho came up with the solution, I'm assuming it worked, but maybe that's to do with the iOS version and also possibly to particular stream.

Providing some test urls will be helpful in experimenting with solutions.

Sorry I didn't see your first reply !

My stream is a mp3 stream using icecast the url is : https://la-prise.com/stream (it's this one I use in my app) or without redirection : http://51.83.70.42:8000/stream

It works perfectly on Android but seek(null) is not working on iOS The stream should be streaming metadata too

Let me know if you find something out or if I can do something else to help you find the issue

ryanheise commented 2 years ago

I did some experiments with icecast streams last night actually, without any success.

Currently the code seeks to positive infinity (which doesn't work). But there is another approach that seeks to the end of the last seekable range (see here), and I tried that. That also doesn't work because iOS is reporting no seekable ranges.

So for the moment, I don't see a way of doing this. If anyone finds a way, please share below and I will implement it.