Public API for iOS music player? - iphone

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.

Related

Is it possible to interject your own audio once the ios media library has taken over?

I am wondering if it would be possible to insert your own audio once the ios media library has taken over in an app?
Example: Playing a piece of audio after every song that says the number of songs you've listened to that day.
I know that because its a framework there is only so many things they let you do.
This should be doable. To start, you can add your class as an observer of the MPMusicPlayerControllerNowPlayingItemDidChangeNotification to be informed when the song in the iPod library changes. From there, you can tell the iPod library to pause, play your track, and then resume music playback afterwards.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(someSel:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:nil];
[[MPMusicPlayerController iPodMusicPlayer] pause];
// play your sound
[[MPMusicPlayerController iPodMusicPlayer] play];

Playing video without audio interruption - objective c

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)
}

play two video in iPhone simultaneously

I want to play two video in iPhone simultaneously.
There are two way to play video in iphone, One is use AVQueuePlayer. but in this controller I don't get how get the video playing is completed and how to restart video again.
Another way is MPMoviePlayerController . but in this controller I don't get how to seek video at particular time and also it is not able to play two video simultaneously as the AVQueuePlayer is able to play.
as a solution i am using AVQueuePlayer to play video and but can any one help me to restart video and get method to detect end point of the video. or know any other api to so this
Thanks in advance.
I have found the solution to play two video . You can use AVQueuePlayer to play video. Using this controller you can play two video at the same time .
I wonder if this is even possible. Video playback on the iPhone is ensured thanks to a hardware video decoder. And I really think the hardware decoder can only handle one video stream at a time.
Ecco is correct. You're limited to one video at a time due to hardware restrictions.
However, if you're after 7KV7's suggestion of one video ending and triggering the playback of another, you can make use of the MPMoviePlayerPlaybackDidFinishNotification notification in the following way:
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(firstMoviePlayerDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
- (void)moviePlayerDidFinish:(NSNotification *)notification {
if (firstMovie) {
[moviePlayerController setContentURL:nextMovieURL];
[moviePlayerController play];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
}
Also, note that you can find the current playback point in a MPMoviePlayerController instance by examining the currentPlaybackTime property.

iPhone App backgrounding with MPMusicPlayer

I'm working on an iPhone iOS4 application that incorporates playing music from the user's iPod library. I also want to keep track of what songs have been played and be able to change the song randomly, even while in the background. So I set the music player using:
[self setMusicPlayer: [MPMusicPlayerController iPodMusicPlayer]];
Now, I want this application to continue to run and play music in the background, so I have set:
Required background modes: App plays audio
The problem I'm having is that my application loses control when it is moved into the background (when applicationDidEnterBackground is called, ie. on app switches). Since I'm using the iPodMusicPlayer the music continues to play but my app does not have control and therefore can't track or change the song.
Now, the Apple documentation states that your application should continue to execute in the background using this required background modes tag, but mine does not. Is it because I'm using MPMusicPlayer? Is there any way to get around it? Any ideas?
PS. I'm also trying to get the remote locked and multitasking iPod controllers to work with my application. I'm using the code below, but remoteControlReceivedWithEvent never gets called! Does it work with MPMusicPlayer? I've only seen it with AVAudioPlayer.
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
NSLog(#"remoteControlReceivedWithEvent");
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
NSLog(#"Play Pause");
break;
case UIEventSubtypeRemoteControlNextTrack:
NSLog(#"Next");
break;
default:
break;
}
}
- (BOOL)canBecomeFirstResponder {
NSLog(#"canBecomeFirstResponder");
return YES;
}
- (void) viewWillAppear:(BOOL)animated{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
You are correct. Your iPhone app will not run in the background while using MPMusicPlayerController. This will also prevent you from receiving remote control events.
If you want to play audio from the iPod library and have your app continue running in the background, you must use the lower-level AVPlayer class.

MPMoviePlayerController stops iPod playback and doesn't restart

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.