简体   繁体   中英

How to play a ringtone from the iPhone in my Xamarin.iOS application?

I can't seem to find anything on internet about it..

I need to trigger a ringtone (which could be chosen by the user later on) on a specific event but for now just playing a ringtone (or music) would be alright.

There is no SDK API to play a ringtone on iOS but you could play a local file. Have you had a look at the Xamarin tutorial Playing Sound with AVAudioPlayer

This will let you play local files like so:

// Initialize background music
songURL = new NSUrl("Sounds/"+filename);
NSError err;
backgroundMusic = new AVAudioPlayer (songURL, "wav", out err);
backgroundMusic.Volume = MusicVolume;
backgroundMusic.FinishedPlaying += delegate { 
    backgroundMusic = null;
};
backgroundMusic.NumberOfLoops=-1;
backgroundMusic.Play();
backgroundSong=filename;

If it is a message sounds (≤30 seconds), not a ringtone, you could try using AudioServicesPlaySystemSound

Here is a list of all the system sound Ids , also at this GitHub too. 1007 is the standard Tri-Tone notification sound.

AudioServicesPlaySystemSound(1003);

There is some talk about your app being rejected when using AudioServicesPlaySystemSound but I can't see any solid proof also here is Apple's sample code to play a system sound

The iOS SDK does not bundle these sound files, but they are on the device in the /System/Library/Audio/UISounds/ folder. Ringtone are located here /Library/Ringtones/ Someone did mention these are copyrighted, so if you are bundling them in your app then beware.

Then play the sound like so:

var s = new SystemSound (soundId);
s.PlayAlertSound ();

Beware

Bundling copyrighted sounds from Apple will probably get your app rejected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM