Can not get duration property in Xamarin.IOS in C#

Engineer Zhang 0 Reputation points
2023-05-27T07:22:15.7266667+00:00

I'm trying to get duration of an url audio.

NSUrl nsurl = NSUrl.FromFilename(url);
AVUrlAssetOptions option = new AVUrlAssetOptions();
option.PreferPreciseDurationAndTiming = true;
avassetTem = new AVUrlAsset(url, option);
string duration = avassetTem.Duration.Seconds.ToString();

Var url is a '.wav' file saved on server. I want to load duration of it. But with these code i can only get '0'.

Trying so many ways, i can get duration only when i play the audio with AVPlayer instance and then get duration from CurrentItem.Duration after audio played over.

AudioSession.Category = AudioSessionCategory.MediaPlayback;
player = new AVPlayer(NSUrl.FromFilename(path));
player.ActionAtItemEnd = AVPlayerActionAtItemEnd.Pause;
player.Play();
await Task.Delay(1000);
var duration = player.CurrentItem.Duration.Seconds;

I'm using ios simulator.

Developer technologies | .NET | Xamarin
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Engineer Zhang 0 Reputation points
    2023-05-29T08:09:31.01+00:00

    I have solved the problem.

    The media file i tested is not an real .wav file even with an extension of wav. When playing the file, player may turn the file to correct wav file, then i can get the info of file.


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.