检测音乐播放器何时暂停 [英] Detect when music player is being paused
问题描述
我想知道是否可以检测到手机上"iPod"中的音乐何时被暂停,而不是只是在音乐被暂停的那一刻才被检测到是否正在暂停或当前正在播放.我想我应该使用AVAudio
?
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
?
谢谢!
推荐答案
MPMusicPlayerController
具有方法:
+(MPMusicPlayerController *)iPodMusicPlayer;
iPod音乐播放器代表您使用iPod应用程序.在 实例化,它采用当前iPod应用程序的状态并进行控制 应用运行时的状态.具体来说,共享状态包括 以下:
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:
重复模式(请参阅重复模式")随机模式(请参阅随机模式") 正在播放的项目(请参阅nowPlayingItem)播放状态(请参阅 播放状态)iPod状态的其他方面,例如移动中 播放列表,不共享.当播放音乐继续播放时 您的应用将移至后台.
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.
您可以检查其playbackState
可能是:
enum {
MPMusicPlaybackStateStopped,
MPMusicPlaybackStatePlaying,
MPMusicPlaybackStatePaused,
MPMusicPlaybackStateInterrupted,
MPMusicPlaybackStateSeekingForward,
MPMusicPlaybackStateSeekingBackward
};
typedef NSInteger MPMusicPlaybackState;
如果playbackState
随MPMusicPlayerControllerPlaybackStateDidChangeNotification
更改,您还可以得到通知.
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;
}
}
这篇关于检测音乐播放器何时暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!