2015年2月26日 星期四

iOS volume button press detect & bluetooth headset play / next / prev / volume button, change song info display

prepare a mp3 file for testing

here is the source code:

AppDelegate, default

ViewController.h 改成這樣

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController : UIViewController
{
int volumecount;
AVAudioPlayer *audioPlayer;
NSMutableDictionary *songInfo;
}
@end
view raw gistfile1.m hosted with ❤ by GitHub
如果需要背景也能播放 改一下plist


ViewController.m 改成這樣,以便接收藍芽事件

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Turn on remote control event delivery
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// Set itself as the first responder
[self becomeFirstResponder];
}
view raw gistfile2.m hosted with ❤ by GitHub
ViewController.m 改成這樣,以多緒播放音樂,監聽音量事件,並設定曲目資訊
- (void)viewDidLoad {
[super viewDidLoad];
//以多執行續播放音樂 達成背景執行程式
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// 設定可以背景播放音樂
AVAudioSession *session = [AVAudioSession sharedInstance];
if ([session setCategory:AVAudioSessionCategoryPlayback error:nil]) {
NSLog(@"背景播放設定OK");
} else {
NSLog(@"背景播放設定失敗");
}
// 以下為播放音樂的程式碼
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp3"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:nil];
[audioPlayer setVolume:0.2];
[audioPlayer setNumberOfLoops:-1];
if (audioPlayer != nil) {
if ([audioPlayer prepareToPlay])
[audioPlayer play];
}
});
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
songInfo = [ [NSMutableDictionary alloc] init];
[songInfo setObject: @"Audio Title" forKey:MPMediaItemPropertyTitle ];
[songInfo setObject: @"Audio Author" forKey:MPMediaItemPropertyArtist ];
[songInfo setObject: @"Audio Album" forKey:MPMediaItemPropertyAlbumTitle ];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo ];
}
view raw 3.m hosted with ❤ by GitHub

ViewController.m 改成這樣,音量改變時,改變曲目資訊
-(void)volumeChanged:(NSNotification *)notification
{
float volume=[[[notification userInfo] objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]floatValue];
NSLog(@"volume = %f",volume);
NSString* str = [NSString stringWithFormat:@"%f",volume];
[ songInfo setObject: str forKey:MPMediaItemPropertyTitle ];
[ songInfo setObject: @"volume" forKey:MPMediaItemPropertyArtist ];
[ songInfo setObject: @"Audio Album" forKey:MPMediaItemPropertyAlbumTitle ];
[ [MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo ];
}
view raw 4.m hosted with ❤ by GitHub
ViewController.m 改成這樣,接收到事件,改變曲目資訊
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
NSLog(@"play");
[ songInfo setObject: @"play" forKey:MPMediaItemPropertyTitle ];
[ songInfo setObject: @"Audio Author" forKey:MPMediaItemPropertyArtist ];
[ songInfo setObject: @"Audio Album" forKey:MPMediaItemPropertyAlbumTitle ];
[ [MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo ];
break;
case UIEventSubtypeRemoteControlPause:
NSLog(@"pause");
[ songInfo setObject: @"pause" forKey:MPMediaItemPropertyTitle ];
[ songInfo setObject: @"Audio Author" forKey:MPMediaItemPropertyArtist ];
[ songInfo setObject: @"Audio Album" forKey:MPMediaItemPropertyAlbumTitle ];
[ [MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo ];
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
//[self playOrStop: nil];
NSLog(@"play pause/play2");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
//[self previousTrack: nil];
NSLog(@"play previous");
[ songInfo setObject: @"previous" forKey:MPMediaItemPropertyTitle ];
[ songInfo setObject: @"Audio Author" forKey:MPMediaItemPropertyArtist ];
[ songInfo setObject: @"Audio Album" forKey:MPMediaItemPropertyAlbumTitle ];
[ [MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo ];
break;
case UIEventSubtypeRemoteControlNextTrack:
//[self nextTrack: nil];
NSLog(@"play next");
[ songInfo setObject: @"next" forKey:MPMediaItemPropertyTitle ];
[ songInfo setObject: @"Audio Author" forKey:MPMediaItemPropertyArtist ];
[ songInfo setObject: @"Audio Album" forKey:MPMediaItemPropertyAlbumTitle ];
[ [MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo ];
break;
default:
break;
}
}
}
view raw 5.m hosted with ❤ by GitHub

沒有留言:

張貼留言