iOSで、自分のアプリがバックグラウンドにいても、他のミュージック再生アプリで再生している曲情報を収集できるようにしたいと思っています。
http://marunouchi-tech.i-studio.co.jp/2569/
こちらのサイトの情報によると、Apple標準ミュージック再生アプリの情報であれば取得できるようなのですが、
自分のアプリがバックグラウンドにいるときでも監視してログとして、取得することはできるでしょうか?
どなたかアドバイスお願いいたします。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答2件
0
iOSは許可された特定の状態のアプリ以外はプロセスが完全に停止します。
Appleが規定する動作(アプリがオーディオを再生しているとかGPSを取得している等)以外でバックグラウンドで動くことはできないのでそういったログ行為はできないと思います。
Objective
1 2- (BOOL)application:(UIApplication *)application 3didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 4{ 5 _player = [MPMusicPlayerController systemMusicPlayer]; 6 7 [_player beginGeneratingPlaybackNotifications]; 8 9 [[NSNotificationCenter defaultCenter] addObserver:self 10 selector:@selector(onPlayItemChanged:) 11 name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification 12 object:nil]; 13 14 [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; 15 16 return YES; 17} 18 19 20- (void)onPlayItemChanged:(NSNotification*)notify 21{ 22 MPMediaPropertyPredicate* pred; 23 NSNumber* persistentId = notify.userInfo[@"MPMusicPlayerControllerNowPlayingItemPersistentIDKey"]; 24 25 pred = [MPMediaPropertyPredicate predicateWithValue:persistentId 26 forProperty:MPMediaItemPropertyPersistentID]; 27 28 MPMediaQuery* query; 29 30 query = [[MPMediaQuery alloc] initWithFilterPredicates:[NSSet setWithObject:pred]]; 31 32 for(MPMediaItem* item in query.items){ 33 NSLog(@"%s '%@' '%@' '%@' %@", __func__, item.title, item.artist, item.albumTitle, item.lastPlayedDate); 34 } 35} 36 37- (void)application:(UIApplication *)application 38performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 39{ 40 NSLog(@"%s", __func__); 41 completionHandler(UIBackgroundFetchResultNewData); 42} 43 44
投稿2016/10/17 12:20
編集2016/10/19 15:11総合スコア2850
0
ベストアンサー
出来るか出来ないかという話でしたら、出来ます。というか出来ました。
CoreLocationを使ってバックグラウンド処理を行い、1時間ほど放置した結果です。
2016-10-18 09:32:25.378 test[3519:239320] not playing 2016-10-18 09:32:35.719 test[3519:239449] Title : California (There Is No End to Love) : 2016-10-18 10:20:35.243 test[3519:249911] Title : This Is Where You Can Reach Me Now 2016-10-18 10:20:45.252 test[3519:249910] not playing : 2016-10-18 10:48:45.250 test[3519:254771] not playing
BackgroundFetch + Notification
うっかりObjective-Cで書いてしまいました。
objectivec
1- (void)initNotif 2{ 3 MPMusicPlayerController *player = [MPMusicPlayerController systemMusicPlayer]; 4 [[NSNotificationCenter defaultCenter] addObserver:self 5 selector:@selector(nowPlayingItemDidChange:) 6 name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification 7 object:player]; 8 [player beginGeneratingPlaybackNotifications]; 9} 10 11- (void)nowPlayingItemDidChange:(NSNotification *)notif 12{ 13 MPMediaEntityPersistentID persistentID = [notif.userInfo[@"MPMusicPlayerControllerNowPlayingItemPersistentIDKey"] unsignedLongLongValue]; 14 NSLog(@"persistentID=%llu", persistentID); 15 16 MPMediaQuery *query = [MPMediaQuery new]; 17 MPMediaPropertyPredicate *pred = [MPMediaPropertyPredicate predicateWithValue:@(persistentID) 18 forProperty:MPMediaItemPropertyPersistentID 19 comparisonType:MPMediaPredicateComparisonEqualTo]; 20 [query addFilterPredicate:pred]; 21 NSArray<MPMediaItem *> *items = [query items]; 22 //NSLog(@"items=%@", items); 23 24 [items enumerateObjectsUsingBlock:^(MPMediaItem *item, NSUInteger idx, BOOL *stop) { 25 MPMediaType mediaType = [[item valueForProperty:MPMediaItemPropertyMediaType] unsignedIntegerValue]; 26 if (mediaType == MPMediaTypeMusic) 27 { 28 NSLog(@"%@ [%@] [%@] [%@] %f", item.lastPlayedDate, item.title, item.albumTitle, item.artist, item.playbackDuration); 29 } 30 }]; 31}
performFetchWithCompletionHandlerの中ではNSLogを空撃ちしているだけです。
これでしばらく放置してみました。
2016-10-19 19:48:46 test[8916] persistentID=1294602247482248857 2016-10-19 19:48:46 test[8916] 2016-10-19 10:30:22 +0000 [The Miracle (Of Joey Ramone)] [Songs of Innocence] [U2] 255.382000 2016-10-19 20:00:39 test[8916] persistentID=1294602247482248854 2016-10-19 20:00:39 test[8916] 2016-10-19 10:54:55 +0000 [Every Breaking Wave] [Songs of Innocence] [U2] 252.162000 2016-10-19 20:00:39 test[8916] [fetch] 2016-10-19 20:00:39 test[8916] persistentID=1294602247482248852 2016-10-19 20:00:39 test[8916] 2016-10-19 10:58:55 +0000 [California (There Is No End to Love)] [Songs of Innocence] [U2] 239.846000 2016-10-19 20:23:17 test[8916] persistentID=1294602247482248855 2016-10-19 20:23:17 test[8916] 2016-10-19 11:02:41 +0000 [Song for Someone] [Songs of Innocence] [U2] 226.763000 2016-10-19 20:23:17 test[8916] [fetch] 2016-10-19 20:23:17 test[8916] persistentID=1294602247482248860 2016-10-19 20:23:17 test[8916] 2016-10-19 11:08:01 +0000 [Iris (Hold Me Close)] [Songs of Innocence] [U2] 319.457000 2016-10-19 20:32:18 test[8916] persistentID=1294602247482248862 2016-10-19 20:32:18 test[8916] 2016-10-19 11:29:52 +0000 [This Is Where You Can Reach Me Now] [Songs of Innocence] [U2] 305.134000 2016-10-19 20:32:18 test[8916] [fetch] 2016-10-19 20:32:18 test[8916] persistentID=1294602247482248861 2016-10-19 20:32:18 test[8916] 2016-10-18 01:15:35 +0000 [The Troubles] [Songs of Innocence] [U2] 285.843000
うまくいってる?
投稿2016/10/18 01:58
編集2016/10/19 11:39総合スコア16731
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/19 12:16 編集
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/18 01:46
2016/10/18 17:19
2016/10/18 17:23
2016/10/19 08:21 編集
2016/10/19 03:08
2016/10/19 08:14 編集
2016/10/19 11:10 編集
2016/10/19 15:34
2016/10/20 02:01
2016/10/20 15:55