Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Enhance/Fix getCurrentTrack #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions RNiTunes/RNiTunes.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,30 @@ @implementation RNiTunes
successCallback(@[mutableAlbumsToSerialize]);
}

RCT_EXPORT_METHOD(getCurrentTrack: (RCTResponseSenderBlock)successCallback) {
RCT_EXPORT_METHOD(getCurrentTrack:(NSDictionary *)params successCallback:(RCTResponseSenderBlock)successCallback) {
NSLog(@"%@ %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd));

MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];

NSString *playerType = [RCTConvert NSString:params[@"playerType"]];
BOOL includeArtwork = [RCTConvert BOOL:params[@"includeArtwork"]];

MPMusicPlayerController *musicPlayer = nil;

if ([playerType isEqualToString:@"system"]) {
musicPlayer = [MPMusicPlayerController systemMusicPlayer];
} else if ([playerType isEqualToString:@"application"]) {
musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
} else {
successCallback(@[@"Invalid playerType provided.", [NSNull null]]);
return;
}

MPMediaItem *song = [musicPlayer nowPlayingItem];


if (song == nil) {
successCallback(@[[NSNull null], [NSNull null]]);
return;
}

NSString *title = [song valueForProperty: MPMediaItemPropertyTitle]; // filterable
NSString *albumTitle = [song valueForProperty: MPMediaItemPropertyAlbumTitle]; // filterable
NSString *albumArtist = [song valueForProperty: MPMediaItemPropertyAlbumArtist]; // filterable
Expand All @@ -87,7 +105,7 @@ @implementation RNiTunes
NSString *base64 = @"";
// http://stackoverflow.com/questions/25998621/mpmediaitemartwork-is-null-while-cover-is-available-in-itunes
MPMediaItemArtwork *artwork = [song valueForProperty: MPMediaItemPropertyArtwork];
if (artwork != nil) {
if (artwork != nil && includeArtwork == true) {
//NSLog(@"artwork %@", artwork);
UIImage *image = [artwork imageWithSize:CGSizeMake(100, 100)];
// http://www.12qw.ch/2014/12/tooltip-decoding-base64-images-with-chrome-data-url/
Expand Down
16 changes: 13 additions & 3 deletions itunes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ var React = require("react-native");

var { RNiTunes } = require("react-native").NativeModules;

var defaultGetCurrentTrackOptions = {
playerType: "application",
includeArtwork: true,
}

module.exports = {
getPlaylists: function(params) {
return new Promise(resolve => {
Expand Down Expand Up @@ -43,13 +48,18 @@ module.exports = {
});
},

getCurrentTrack: function() {
getCurrentTrack: function(opts) {
return new Promise((resolve, reject) => {
RNiTunes.getCurrentTrack((err, track) => {
const mergedOptions = Object.assign({}, defaultGetCurrentTrackOptions , opts);
RNiTunes.getCurrentTrack(mergedOptions, (err, track) => {
if (!err) {
if (track && track.artwork === "") {
track.artwork = null
}
resolve(track);
} else {
reject(err);
}
reject(err);
});
});
},
Expand Down
22 changes: 20 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,27 @@ Returns [TrackItem]

`[DEPRECATED]` use `getCurrentTrack()` instead

### getCurrentTrack()
### getCurrentTrack({ playerType = "application", includeArtwork = false })

Returns TrackItem with additional properties: currentPlayTime and artwork
Returns a Promise that resolves to an `TrackItem` with additional properties: `currentPlayTime` and `artwork` or `null` if there is no track played.

The property playerType can be `application` or `system`.
By setting the property `includeArtwork` to `false` you can save some memory resources.

Sample Object:

```js
{
currentPlayTime: number,
genre: string,
playCount: number,
title: string,
albumTitle: string,
duration: number,
albumArtist: string,
artwork: string | null,
}
```

### getPlaylists({ fields = [], query = {} })

Expand Down