I use AVAudioPlayer to play music file. It just works well when playing via headphone or speaker on iPhone body. But not smooth when playing via bluetooth. Why?
Here is my code:
AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:musicData error:&error];
if (!newMusicPlayer)
{
}
else
{
[newMusicPlayer play];
}
Special thx!
This is an apple's bug on 3GS running ios5.1.1, because "Music" is not smooth too.
Related
Is there any way I can access music playing in the native iOS music app? For example on button press in my app it pauses the built in Music app. I've tried MPMusicPlayerController but that doesn't seem to be what I want; or I'm not using it correctly.
This is what I've tried.
if ([musicPlayer playbackState] == MPMusicPlaybackStatePlaying) {
[musicPlayer pause];
} else {
[musicPlayer play];
}
Depends on how you instantiate your music player. To get access to the system global payer you should use:
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
Be aware that if you're using applicationMusicPlayer it will only work for your application and pausing music will not affect the music playing in your Music.app.
See the MPMusicPlayerController documentation overview section for a more extensive description of the two.
My app plays video file without sound. And it interrupts any playing music in backgroud (iPod app for example). How do not interrupt audio session of other apps if it's possible.
My video file is without sound. To play video i use MPMoviePlayerController.
EDIT: Here is my video player code:
_player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
[self installMovieNotificationObservers:nil];
[_player setShouldAutoplay:YES];
[_player setUseApplicationAudioSession:NO];
[_player.view setFrame:self.navController.view.frame];
[_player setMovieSourceType:MPMovieSourceTypeFile];
[_player setRepeatMode:MPMovieRepeatModeNone];
[_player setFullscreen:YES animated:YES];
[_player setControlStyle:MPMovieControlStyleNone];
[_navController.view addSubview:_player.view];
[_player play];
Directly from Apple's docs:
http://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html#//apple_ref/occ/instp/MPMoviePlayerController/useApplicationAudioSession
useApplicationAudioSession
A Boolean value that indicates whether the
movie player should use the app’s audio session.
#property (nonatomic) BOOL useApplicationAudioSession Discussion The
default value of this property is YES. Setting this property to NO
causes the movie player to use a system-supplied audio session with a
nonmixable playback category.
Important In iOS 3.1 and earlier, a movie player always uses a
system-supplied audio session. To obtain that same behavior in iOS 3.2
and newer, you must set this property’s value to NO. When this
property is YES, the movie player shares the app’s audio session. This
give you control over how the movie player content interacts with your
audio and with audio from other apps, such as the iPod. For important
guidance on using this feature, see “Working with Movies and iPod
Music” in Audio Session Programming Guide.
Changing the value of this property does not affect the currently
playing movie. For the new setting to take effect, you must stop
playback and then start it again.
Availability Available in iOS 3.2 and later. Declared In
MPMoviePlayerController.h
Swift 5 answer
do {
if #available(iOS 10.0, *) {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: .mixWithOthers)
} else {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: .mixWithOthers)
}
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
i am develop app with avaudioplayer it's works perfectly, but,my problem is if my iPhone vibrate mode to open my app avaudioplayer plays audio little bit below seconds, i want to not play a avaudioplayer sound in vibrate mode.
here's my code:
- (void)viewDidLoad
{
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"audio" ofType:#"mp3"]] error:NULL];
audioPlayer.numberOfLoops =-1;
audioPlayer.delegate = self;
audioPlayer.currentTime=0.0f;
[audioPlayer prepareToPlay];
[audioPlayer Play];
}
can any one help me please..!
Thanks..!
Try:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error: nil];
As stated in the Apple Docs:
AVAudioSessionCategoryAmbient
For an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off.
This category is also appropriate for “play along” style apps, such as a virtual piano that a user plays over iPod audio. When you use this category, audio from other apps mixes with your audio. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).
Should be what you're looking for specifically.
Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone)
I've got an iPhone app with a short intro video. If a user launches the app while their iPod is playing music, the music will stop while the video plays (whether or not the video has sound), and the audio stays permanently stopped after video playback.
Apple seems to indicate that you can solve this with AudioSession tricks:
https://web.archive.org/web/20100819124854/http://developer.apple.com:80/iPhone/library/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/WorkingWithOpenALiPodMusicandMovies/WorkingWithOpenALiPodMusicandMovies.html
But their suggestions here just don't seem to work; it seems like MPMoviePlayerController overrides the audio session configuration for its own purposes. Ideally I'd mix the movie audio over the iPod audio or maybe use ducking, but even restarting the music might be a passable fix.
I've found a great solution for that. In the .h file, you must create a BOOL called "wasPlaying". Before playing your video, you ask the iPod if it was playing.
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying)
{
NSLog(#"Music was playing, lets put YES to the bool");
wasPlaying = YES;
}
Then, after you tell the movie player to play, you call the following:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(finishedPlaying) name:MPMoviePlayerPlaybackDidFinishNotification object: moviePlayer];
And after that, in the method finishedPlaying:
if (wasPlaying ==YES)
{
NSLog(#"Music was playing, lets play music again");
[[MPMusicPlayerController iPodMusicPlayer] play];
}
For me it worked fine!
I think you can do this by initializing the audio session similar to this:
NSError *audioSessionError = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient
error:&audioSessionError] == YES)
Then when you want to use the audio session you can set the iPod audio to duck the video track:
AudioSessionInitialize (NULL, NULL, NULL, NULL);
OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError |= AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(allowMixing), &allowMixing);
AudioSessionSetActive(YES);
You can only have a single music-providing app at any time and multiple sources of (brief) sounds. If a background app is playing music, your app can overlay brief sounds. If you want to play music, the background app has to be stopped.
So I don't think that what you're trying to achieve is possible using MPMoviePlayerController (or any of the high-level audio frameworks). You might be able to overlay an audio track of a movie, if it's sufficiently short but MPMoviePlayerController is probably not good for this.
How can I get an ipod library music file into AVAudioPlayer?
As David mentions there is more work to do than this, for example you have to manage playing the next track in a collection of media items, but here is one way to do it with a set of MPMediaItems that a user selected from the iPod Picker. The AssetURL is what you use, it gives you a path to the MP3 file (e.g. ipod-library://item/item.mp3?id=-6889145242935454020)
NSURL *anUrl = [[mediaItems objectAtIndex: 0] valueForProperty:MPMediaItemPropertyAssetURL];
self.audioPlayerMusic = [[[AVPlayer alloc] initWithURL:anUrl] retain];
[self.audioPlayerMusic play];
Yes, you can play songs from the iPod library using the SDK without resorting to the MPMusicPlayerController class.
The more basic AVPlayer class can handle audio files from the iPod library by using the NSUrl value from the song's MPMediaItemPropertyAssetURL property. You have to do a lot more work to get everything setup properly, but it can be done.
The SDK has no provision for reading files from the iPod library (as you'd need to do to use AVAudioPlayer with it), probably for anti-piracy reasons. To play iPod library items, use the MPMusicPlayerController class.
Edit: This is no longer accurate. See the below answers which describe the use of the AVPlayer class.
Is there any possibility to get information about dB-metering in MPMusicPlayerController? Perhaps initiating an AVAudioSession for Recording in parallel would do the job?? I need dB-values to build some kind of volume-spectrograph.
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
NSURL *url = [[mediaItemCollection.items objectAtIndex:0] valueForProperty:MPMediaItemPropertyAssetURL];
NSError *error;
self.player = [[AVAudioPlayer alloc] url error:&error];
if (!error) {
[self.player prepareToPlay];
[self.player play];
}
[mediaPicker dismissModalViewControllerAnimated:YES];
}