What is the difference between AVAudioPlayer and MPMusicPlayerController? - iphone

It looks like both of them can play audio file.
What is the difference between AVAudioPlayer and MPMusicPlayerController?
Welcome any comment
Thanks
interdev

Read the class reference
MPMusicPlayerController: http://developer.apple.com/iphone/library/documentation/mediaplayer/reference/MPMusicPlayerController_ClassReference/Reference/Reference.html
You use an MPMusicPlayerController object, or music player, to play media items from the iPod library.
AVAudioPlayer: http://developer.apple.com/iphone/library/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html
An instance of the AVAudioPlayer class, called an audio player, provides playback of audio data from a file or memory.
Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency.

Related

Does AVAudioPlayer stream a file from disk, or does it load it into memory all at once?

I want to play very long audio files of up to an hour. AVAudioPlayer seems to be the easiest solution. But is it also efficient or does it load the whole file into memory before playing?
Apple's doc for AVAudioPlayer say:
An instance of the AVAudioPlayer class, called an audio player,
provides playback of audio data from a file or memory.
Applerecommends that you use this class for audio playback unless you are
playing audio captured from a network stream or require very low I/O
latency.

ios - Is it possible to record audio and play video from network stream at the same time?

I'm doing research for a iPhone video chat app project.
I've tried this to capture image,
use AVCaptureVideoPreviewLayer to display camera view
and put a MPMoviePlayerController to play network stream video at the back.
They all work until I add an audio input to the AVCaptureSession.
The MPMoviePlayerController stop the AVCaptureSession if there is audio input.
I think of using AVAudioSession for both playing and recording audio and some other way to play video,
but the documentation of AVAudioPlayer said
"Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency."
I found the Multimedia Programming Guide saying that
"To provide lowest latency audio, especially when doing simultaneous input and output (such as for a VoIP application), use the I/O unit or the Voice Processing I/O unit."
Is it the correct direction to implement video chat in iphone?

AVPlayer vs. AVAudioPlayer

The documentation for AVPlayer states the following:
[The] player works equally well with local and remote media files
However, the documentation for AVAudioPlayer states the following:
Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream
For the work I am doing I need some of the capabilities of AVAudioPlayer, but all my audio is being streamed. The main thing I need from AVAudioPlayer that AVPlayer does not have is the "playing" property. It is difficult to build a player UI without that property, among others.
So what is the difference between AVPlayer and AVAudioPlayer that makes the latter unsuitable for network streaming? Is there a way to get some of the info from AVPlayer that AVAudioPlayer provides such as the "playing" property?
AVPlayer can play from AVPlayerItem using AVURLAsset with an iPod library url. The AVAudioPlayer cannot play from an iPod library url.
AVPlayer has no volume property and requires the use of the system volume setting which can be controlled only by the hardware switch or an MPVolumeView. But you can set the mix volume of AVAudioPlayer.
AVPlayer seems to report an incorrect currentTime after seeking. But AVAudioPlayer reports accurately.
7 years after...
From a point of view with dependence on Swift and CocoaPods, so my answer is comparing for iOS 8+ only.
1. iPod library support
identical support since iOS6
2. volume control
identical support:
You can set the mix volume of AVAudioPlayer directly.
You can set the mix volume of AVPlayer with an AVAudioMix on the AVPlayerItem
3. seeking control
both AVPlayer and AVAudioPlayer seem to report an incorrect currentTime after seeking:
for AVAudioPlayer, it is suggested to stop() the AVAudioPlayer before seeking
for AVPlayer, it is suggested to pass the option AVURLAssetPreferPreciseDurationAndTimingKey when initializing AVURLAssets. And rely on values given by observer block.
4. changing source
you need only one AVPlayer to play multiple files
you need multiple AVAudioPlayer to play multiple files
The AVPlayer actually has a similar property as the playing property of AVAudioPlayer.
Take a look at the rate property.

how can we play audio and video together in ipod/iphone?

HI all
i am playing a video(.mp4 format) without sound (i mean it doesn't have sound it is a mute video ) and in the background i am playing an audio file (.mp3 format) when i play my code through simulator it works fine as i want like when i tap on the video it is just mute but behind i am playing the audio so for user it seems that video has this sound but when i installed my code in device and play video than it doesn't work like so it play video but without sound than how can i play an audio and a video together in the above format ?
actually we are not just playing a single video or audio file it just comes from an array by choosing randomly and same for the audio file so we cann't do this i think so any other idea for it ??
Should we use another format for audio aur video for doing this thing ??
thanks for the help
Balraj verma
The problem is that the default Audio Session does not allow audio mixing.
In the Reference Library (Working with Movie Players) they say you should use a mixable category configuration for your audio session, for example the Ambient category. In the Application Delegate:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryAmbient error: &setCategoryError];
Apple's documentation states that some audio formats are not suited to be played back simultaneously because of hardware restrictions. See Playing Multiple Sounds Simultaneously for more details.
A solution for you would be to use PCM encoded audio which should allow simultaneous playback.
I would like to add that I managed to play two mp3 audio files at the same time on 3G and 3GS iPhones. Which shouldn't be possible according to documentation but worked for me.
You can rather use the an instance of AvAudioPlayer in a new thread. Use the following link to see how does this work
http://www.mobileorchard.com/easy-audio-playback-with-avaudioplayer/
create an instance of MPMoviePlayer to start playback of videos.
Hope this will work.
You should combine the audio and video using some video editing software like iMovie or Windows Movie Maker. Using the software to "composite" audio and video together is not a good idea and adds additional overhead, synchronization issues, etc.
As far as I know, you can't play AVAudioPlayer and MPMoviePlayer at the same time.

Which format support the audio file with a playing video?

i am playing the video and in the background i have to play the audio file and if i do this audio is not playing its sound as it just play video is it a extension problem i am using the .mp3 for the audio and .mp4 for the video .
how can we do this we have to play simultanisouly .both the file
balraj verma
Have a look at this:
http://developer.apple.com/iphone/library/documentation/MusicAudio/Conceptual/CoreAudioOverview/CoreAudioEssentials/CoreAudioEssentials.html#//apple_ref/doc/uid/TP40003577-CH10-SW67
"To play multiple sounds simultaneously, create one playback audio queue object for each sound. For each audio queue, schedule the first buffer of audio to start at the same time using the AudioQueueEnqueueBufferWithParameters function.
Audio format is critical when you play sounds simultaneously on iPhone or iPod touch. This is because playback of certain compressed formats in iPhone OS employs an efficient hardware codec. Only a single instance of one of the following formats can play on the device at a time:
AAC
ALAC (Apple Lossless)
MP3"