Trong ứng dụng của tôi, tôi muốn cho phép người dùng điều khiển phát lại âm thanh dưới nền. Tôi thiết lập chế độ backGround trong .plist, và trong lượt chơi trong bg giống như tôi muốn. Nhưng tôi không thể nhận được bất kỳ phản ứng nào từ việc chạm vào các nút điều khiển.Không thể bắt đầuReceivingRemoteControlEvents trong iOS
tôi đặt AudioSession
như thế này
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance]setActive:YES error:nil];
Sau đó, trong viewContoller nơi máy nghe nhạc của tôi được đặt i beginReceivingRemoteControlEvents
như thế này
if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
[self becomeFirstResponder];
NSLog(@"Responds!");
}
Và nó in Responds!
Nhưng vấn đề là phương pháp này không bao giờ được gọi là
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
NSLog(@"Where is my event?");
if(event.type == UIEventTypeRemoteControl)
{
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
NSLog(@"Pause");
[self playWords:playButton];
break;
case UIEventSubtypeRemoteControlNextTrack:
NSLog(@"Next");
[self next];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
NSLog(@"Prev");
[self prev];
break;
}
}
Tôi thậm chí đã cố gắng để viết một thể loại trên UIApplication
để cho nó trở thành responder đầu tiên, nhưng nó không giúp
@implementation UIApplication (RemoteEvents)
-(BOOL)canBecomeFirstResponder
{
return YES;
}
@end
Tại sao điều này có thể xảy ra?
SOLUTION Đây là những gì giải quyết vấn đề của tôi Entering background on iOS4 to play audio
Cảm ơn bạn, nhưng theo như tôi có thể thấy, nó chỉ giống nhau –
Một điều nữa là bạn không thể thử nghiệm trên trình mô phỏng, sử dụng thiết bị nếu bạn đang sử dụng trình mô phỏng để kiểm tra xóa sự kiện và nhạc nền. –
Tôi thử nghiệm trên thiết bị) –