0

i am new to Xamarin.i am trying to play sound using AVPlayerViewController in Xamrin.iOS from list view.Here is my button Action.

var playBtn = new Button () {
WidthRequest = 50,
HeightRequest = 50,
BackgroundColor = Color.Transparent,
Command = new Command(() =>{
DependencyService.Get().PlayAudioFile("Audio.mp3");
})

playBtn is custom cell button.Cell data is Populated using XAML The "PlayAudioFile" is an interface and is Platform specific.i am currently using it for iOS. Here is my iOS UIViewController Code

public void PlayAudioFile(string fileName)
{
string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));
var url = NSUrl.FromString (sFilePath);
var player = new AVPlayer (url);
player.Rate = 0.25f;
var playerVC = new AVPlayerViewController();
playerVC.Player = player;
playerVC.AllowsPictureInPicturePlayback = true;
playerVC.View.Frame = this.View.Frame;
player.Play ();
this.PresentViewController (playerVC, true, null);
}

But the AVPlayerViewController is not playing audio and nor its Presenting in view controller.What am i doing wrong. Thanks in Advance.

1 Answers1

0

I see one problem here is the way you are creating url. You are using file from bundle, which means its local file. use NSUrl.FromFilename(sFilePath) instead of NSUrl.FromString (sFilePath);

NSUrl.FromString will create url like http://yourfolder/subfolder/fielname.extension which is invalid in your case . whereas NSUrl.FromFilename will create url like file://yourfolder/subfolder/filesname.extension. which is valid in your case.

PlusInfosys
  • 3,416
  • 1
  • 19
  • 33