I wonder if it's possible to detect when the the music in my "iPod" on the phone is being paused, not if it's paused or currently playing, just the moment when the music is being paused. I guess i should be using AVAudio?
Thanks!
MPMusicPlayerController has a method:
+(MPMusicPlayerController *)iPodMusicPlayer;
The iPod music player employs the iPod app on your behalf. On
instantiation, it takes on the current iPod app state and controls
that state as your app runs. Specifically, the shared state includes
the following:
Repeat mode (see “Repeat Modes”) Shuffle mode (see “Shuffle Modes”
Now-playing item (see nowPlayingItem) Playback state (see
playbackState) Other aspects of iPod state, such as the on-the-go
playlist, are not shared. Music that is playing continues to play when
your app moves to the background.
You can check its playbackState which may be:
enum {
MPMusicPlaybackStateStopped,
MPMusicPlaybackStatePlaying,
MPMusicPlaybackStatePaused,
MPMusicPlaybackStateInterrupted,
MPMusicPlaybackStateSeekingForward,
MPMusicPlaybackStateSeekingBackward
};
typedef NSInteger MPMusicPlaybackState;
You can also get notified if its playbackState change with the MPMusicPlayerControllerPlaybackStateDidChangeNotification.
#property (nonatomic, strong) MPMusicPlayerController *musicPlayer;
-(void)iPodMusicPlayer
{
musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
switch ([musicPlayer playbackState])
{
case: MPMusicPlaybackStateStopped:
NSLog(#"iPod player is stopped)";
//Do something
break;
case: MPMusicPlaybackStatePaused:
NSLog(#"iPod player is paused");
//Do something
break;
case: MPMusicPlaybackStatePlaying:
NSLog(#"iPod player is playing");
//Do something
break;
//Etc.
default:
break;
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(musicPlayerPlayBackStatusChanged:)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
object:nil];
[musicPlayer beginGeneratingPlaybackNotifications];
}
-(void)musicPlayerPlayBackStatusChanged:(NSNotification *)notification
{
switch ([musicPlayer playbackState])
{
case: MPMusicPlaybackStateStopped:
NSLog(#"iPod player is stopped)";
//Do something
break;
case: MPMusicPlaybackStatePaused:
NSLog(#"iPod player is paused");
//Do something
break;
case: MPMusicPlaybackStatePlaying:
NSLog(#"iPod player is playing");
//Do something
break;
//Etc.
default:
break;
}
}
Related
I have tried setting the playinginfo using the below method :
-(void)handleTheMediaPropertyList
{
UIImage *artWork = [UIImage imageNamed:#"sample.png"];
NSDictionary *nowPlaying = #{MPMediaItemPropertyArtist: #"SAMPLE TEST",
MPMediaItemPropertyAlbumTitle: #"SAMPLE TEST",
MPMediaItemPropertyArtwork:[[MPMediaItemArtwork alloc] initWithImage:artWork]
};
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nowPlaying];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
-(void)remoteControlReceivedWithEvent:(UIEvent *)event
{
if (event.type == UIEventTypeRemoteControl) {
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
break;
case UIEventSubtypeRemoteControlPause:
break;
case UIEventSubtypeRemoteControlPreviousTrack:
break;
case UIEventSubtypeRemoteControlNextTrack:
break;
default:
break;
} }
}
After playing the audio, whenever I go to the control center, playing info shows fine there. But when I PAUSE it playing info gets changed to and instead of showing Artist name which I had set above it shows the URL of the link where it is getting streamed from. And later after pausing then playing it again then same data is showed.
So for the first time when the audio is played the Playinfo shows the proper data, but once I open the control center and press PAUSE and PLAY then the playing info gets changed.
Please help me with this.
I would like my app to be notified when microphone interruption has ended before i start recording.
say for example my application is trying to start recording from background and during this time if any other applications like skype is accessing microphone and i shouldn't do. so i want to start register/observe for interruption so that when skype call ended, my app should be notified.
I can able to achieve only when app is recording and interruption began . i will be notified when interruption ended not on while about to start recording .
please help .
I tried to register using
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleAudioSessionInterruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
// observe the interruption begin / end
- (void)handleAudioSessionInterruption:(NSNotification*)notification
{
AVAudioSessionInterruptionType interruptionType = [notification.userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
AVAudioSessionInterruptionOptions interruptionOption = [notification.userInfo[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
NSLog(#"interruptionType:%lu",(unsigned long)interruptionType);
switch (interruptionType) {
case AVAudioSessionInterruptionTypeBegan:{
NSLog(#"interruption began");
break;
}
case AVAudioSessionInterruptionTypeEnded:{
NSLog(#"interruption ended");
break;
}
default:
break;
}
}
I added the Spotify player to my app which also plays music using the MPMusicPlayerController. When music is playing from Spotify and the screen is locked, the remote control events are not received for play/pause and FFW/RWD when the user presses these buttons on the locked screen.
If music is playing from the MPMusicPlayerController, I am able to receive the remote control events based on the following code:
-(void) ViewDidLoad {
...
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
...
}
and
- (BOOL) canBecomeFirstResponder
{
return YES;
}
- (void) remoteControlReceivedWithEvent: (UIEvent*) event
{
// see [event subtype] for details
if (event.type == UIEventTypeRemoteControl) {
// We may be receiving an event from the lockscreen
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
case UIEventSubtypeRemoteControlPlay:
case UIEventSubtypeRemoteControlPause:
// User pressed play or pause from lockscreen
[self playOrPauseMusic:nil];
break;
case UIEventSubtypeRemoteControlNextTrack:
// User pressed FFW from lockscreen
[self fastForwardMusic:nil];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
// User pressed rewind from lockscreen
[self rewindMusic:nil];
break;
default:
break;
}
}
}
While the iPod controls are visible when the app enters the background, they do not respond when I press pause. Instead, the iPod controls disappear when I press pause. What addition is needed to enable detection of play/pause and FFW/RWD when streaming audio such as Spotify is playing in the background from lock screen?
I believe I ran into this in the past. If I remember correctly I added in the
-(void)remoteControlReceivedWithEvent:(UIEvent *) event { ... }
as well as
- (BOOL) canBecomeFirstResponder { return YES; }
to the app delegate (This is also where my audio controller lived). I was having having the issue where the UIViewControllers were not alive during the time I wanted to catch the UIEventTypeRemoteControl notifications.
Give that a try and see if that helps.
After further investigation, I have found that if include the following code when my app enters the background and when the remote control events are received, the iPod controls do not disappear.
// Set up info center to display album artwork within ipod controls (needed for spotify)
MPMediaItemArtwork *ipodControlArtwork = [[MPMediaItemArtwork alloc]initWithImage:artworkImage];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = [NSDictionary dictionaryWithObjectsAndKeys:nowPlayingTitle, MPMediaItemPropertyTitle,
nowPlayingArtist, MPMediaItemPropertyArtist, ipodControlArtwork, MPMediaItemPropertyArtwork, [NSNumber numberWithDouble:0.0], MPNowPlayingInfoPropertyPlaybackRate, nil];
I am not sure what code to add, so let me know what you need to see. I am using MPMoviePlayer in conjunction with Widevine. I am having an issue where the movie stops playing. I check the MoviePlaybackStates and rarely, if ever does it catch. Most of the time it just stops. I want to believe it has something to do with buffering. I am streaming the video, and widevine callbacks gives me no errors. Any ideas how I can track this down or what the issue is?
You should follow loadState instead of playbackState.
The way to do it is to observe MPMoviePlayerLoadStateDidChangeNotification notification, to see how's that buffering going.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(loadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
somewhere before initializing your player.
and
-(void)loadStateChanged:(NSNotification *)notif
{
NSString *loadState=#"";
switch (self.player.loadState) {
case MPMovieLoadStateUnknown: {
loadState=#"MPMovieLoadStateUnknown";
break;
}
case MPMovieLoadStatePlayable: {
loadState=#"MPMovieLoadStatePlayable";
break;
}
case MPMovieLoadStatePlaythroughOK: {
loadState=#"MPMovieLoadStatePlaythroughOK";
break;
}
case MPMovieLoadStateStalled: {
loadState=#"MPMovieLoadStateStalled";
break;
}
}
NSLog(#"%#", loadState);
}
i tried to look for it for days in google and i couldn't find an answer.
i have an app that play audio stream from the internet and i use MPNowPlayingInfoCenter to display the artist title, song title and artwork. now my question is how to use the play/pause button in the MPNowPlayingInfoCenter to play or stop my audio stream.
For this you have to handle remote control Events --
//Add these lines in viewDidAppear()
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
//Add these lines in viewWillDisappear()
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
Then Use
-(void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent
{
NSLog(#"received event!");
if (receivedEvent.type == UIEventTypeRemoteControl)
{
switch (receivedEvent.subtype)
{
case UIEventSubtypeRemoteControlPlay:
// play the video
break;
case UIEventSubtypeRemoteControlPause:
// pause the video
break;
case UIEventSubtypeRemoteControlNextTrack:
// to change the video
break;
case UIEventSubtypeRemoteControlPreviousTrack:
// to play the privious video
break;
default:
break;
}
}
}