here is the source code:
AppDelegate, default
ViewController.h 改成這樣
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
ViewController.m 改成這樣,以便接收藍芽事件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)viewDidAppear:(BOOL)animated { | |
[super viewDidAppear:animated]; | |
// Turn on remote control event delivery | |
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; | |
// Set itself as the first responder | |
[self becomeFirstResponder]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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 ]; | |
} |
ViewController.m 改成這樣,音量改變時,改變曲目資訊
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(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 ]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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; | |
} | |
} | |
} |
沒有留言:
張貼留言