Skip to content

Commit

Permalink
dynamic cover art for radio streams
Browse files Browse the repository at this point in the history
change the cover art for radio streams from local to get images from
internet that is located in github pages of radiosai

also:
- update packages
- fix analysis issues
  • Loading branch information
immadisairaj committed Mar 3, 2023
1 parent f96e09b commit ee46cc9
Show file tree
Hide file tree
Showing 31 changed files with 194 additions and 174 deletions.
1 change: 1 addition & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ GeneratedPluginRegistrant.java
# Remember to never publicly share your keystore.
# See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app
key.properties
upload-keystore.jks
4 changes: 3 additions & 1 deletion ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 51;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -201,6 +201,7 @@
/* Begin PBXShellScriptBuildPhase section */
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -237,6 +238,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down
2 changes: 2 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
2 changes: 1 addition & 1 deletion lib/audio_service/audio_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class MyAudioHandler extends BaseAudioHandler {
index = _player.shuffleIndices![index];
}
final oldMediaItem = newQueue[index]!;
final MediaItem? newMediaItem =
final MediaItem newMediaItem =
oldMediaItem.copyWith(duration: duration ?? Duration.zero);
newQueue[index] = newMediaItem;
queue.add(newQueue as List<MediaItem>);
Expand Down
91 changes: 47 additions & 44 deletions lib/audio_service/audio_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AudioManager {
final isLastSongNotifier = ValueNotifier<bool>(true);
final isShuffleModeEnabledNotifier = ValueNotifier<bool>(false);

final AudioHandler? _audioHandler = getIt<AudioHandler>();
final AudioHandler _audioHandler = getIt<AudioHandler>();

/// if [mediaType] is [MediaType.radio],
/// pass radioStream map which is located in "MyConstants"
Expand All @@ -42,7 +42,7 @@ class AudioManager {
}

_setMediaType(MediaType mediaType) {
_audioHandler!.customAction('setMediaType', {'mediaType': mediaType});
_audioHandler.customAction('setMediaType', {'mediaType': mediaType});
}

/// pass radioStream map which is located in constants
Expand All @@ -51,7 +51,8 @@ class AudioManager {
/// and index of the radio playing as params['index']
Future<void> _initRadio(Map<String, dynamic> params) async {
mediaTypeNotifier.value = MediaType.radio;
final radio = await _getRadio(params['radioStream'], params['index']);
final radio = await _getRadio(
params['radioStream'], params['index'], params['artImages']);
await _loadRadio(radio);
_listenToChangesInQueue();
_listenToPlaybackState();
Expand All @@ -64,31 +65,33 @@ class AudioManager {

/// pass [radioStream] map which is located in constants
/// and [index] of the radio playing
Future<MediaItem> _getRadio(
Map<String, String> radioStream, int index) async {
Future<MediaItem> _getRadio(Map<String, String> radioStream, int index,
Map<String, String> artLinks) async {
// Get the path of image for artUri in notification
String path = await MediaHelper.getDefaultNotificationImage();
// String path = await MediaHelper.getDefaultNotificationImage();
String key = radioStream.keys.toList()[index];
String value = radioStream.values.toList()[index];
String artUri = artLinks.values.toList()[index];
return MediaItem(
id: key,
title: key,
album: 'Radio Sai Global Harmony',
artist: 'Radio Sai',
artUri: Uri.parse('file://$path'),
artUri: Uri.parse(artUri),
// artUri: Uri.parse('file://$path'),
extras: {'uri': value});
}

Future<void> _loadRadio(MediaItem radio) async {
_audioHandler!.addQueueItem(radio);
_audioHandler.addQueueItem(radio);
}

/// pass [radioIndex] - the index of 'radioStream'
playRadio(int radioIndex) async {
// radio title is media Id
await load();
_audioHandler!.skipToQueueItem(radioIndex);
_audioHandler!.play();
_audioHandler.skipToQueueItem(radioIndex);
_audioHandler.play();
}

Future<void> _initMedia(Map<String, dynamic> params) async {
Expand Down Expand Up @@ -122,15 +125,15 @@ class AudioManager {
}

_loadMediaItem(MediaItem mediaItem) async {
_audioHandler!.addQueueItem(mediaItem);
_audioHandler.addQueueItem(mediaItem);
await load();
}

get queue => _audioHandler!.queue;
get currentMediaItem => _audioHandler!.mediaItem;
get queue => _audioHandler.queue;
get currentMediaItem => _audioHandler.mediaItem;

void _listenToChangesInQueue() {
_audioHandler!.queue.listen((queue) {
_audioHandler.queue.listen((queue) {
if (queue.isEmpty) {
queueNotifier.value = [];
currentSongTitleNotifier.value = '';
Expand All @@ -143,7 +146,7 @@ class AudioManager {
}

void _listenToPlaybackState() {
_audioHandler!.playbackState.listen((playbackState) {
_audioHandler.playbackState.listen((playbackState) {
final processingState = playbackState.processingState;
if (processingState == AudioProcessingState.loading ||
processingState == AudioProcessingState.buffering) {
Expand All @@ -158,8 +161,8 @@ class AudioManager {
} else if (processingState != AudioProcessingState.completed) {
playButtonNotifier.value = PlayButtonState.playing;
} else {
_audioHandler!.seek(Duration.zero);
_audioHandler!.pause();
_audioHandler.seek(Duration.zero);
_audioHandler.pause();
}
});
}
Expand All @@ -176,7 +179,7 @@ class AudioManager {
}

void _listenToBufferedPosition() {
_audioHandler!.playbackState.listen((playbackState) {
_audioHandler.playbackState.listen((playbackState) {
final ProgressBarState oldState = progressNotifier.value;
progressNotifier.value = ProgressBarState(
current: oldState.current,
Expand All @@ -187,7 +190,7 @@ class AudioManager {
}

void _listenToTotalDuration() {
_audioHandler!.mediaItem.listen((mediaItem) {
_audioHandler.mediaItem.listen((mediaItem) {
final ProgressBarState oldState = progressNotifier.value;
progressNotifier.value = ProgressBarState(
current: oldState.current,
Expand All @@ -198,15 +201,15 @@ class AudioManager {
}

void _listenToChangesInSong() {
_audioHandler!.mediaItem.listen((mediaItem) {
_audioHandler.mediaItem.listen((mediaItem) {
currentSongTitleNotifier.value = mediaItem?.title ?? '';
_updateSkipButtons();
});
}

void _updateSkipButtons() {
final mediaItem = _audioHandler!.mediaItem.value;
final playlist = _audioHandler!.queue.value;
final mediaItem = _audioHandler.mediaItem.value;
final playlist = _audioHandler.queue.value;
if (playlist.length < 2 || mediaItem == null) {
isFirstSongNotifier.value = true;
isLastSongNotifier.value = true;
Expand All @@ -216,63 +219,63 @@ class AudioManager {
}
}

get playbackState => _audioHandler!.playbackState;
get playbackState => _audioHandler.playbackState;

Future<void> addQueueItem(MediaItem mediaItem) =>
_audioHandler!.addQueueItem(mediaItem);
_audioHandler.addQueueItem(mediaItem);

Future<void> removeQueueItemWithTitle(String mediaTitle) async {
final index = queueNotifier.value.indexOf(mediaTitle);
if (index == -1) return;
return _audioHandler!.removeQueueItemAt(index);
return _audioHandler.removeQueueItemAt(index);
}

void play() => _audioHandler!.play();
void pause() => _audioHandler!.pause();
void play() => _audioHandler.play();
void pause() => _audioHandler.pause();

void seek(Duration position) => _audioHandler!.seek(position);
void seek(Duration position) => _audioHandler.seek(position);

Future<void> skipToQueueItem(int index) =>
_audioHandler!.skipToQueueItem(index);
_audioHandler.skipToQueueItem(index);

void previous() => _audioHandler!.skipToPrevious();
void next() => _audioHandler!.skipToNext();
void previous() => _audioHandler.skipToPrevious();
void next() => _audioHandler.skipToNext();

/// Stops the audio.
Future<void> stop() async {
await _audioHandler!.pause();
return _audioHandler!.stop();
await _audioHandler.pause();
return _audioHandler.stop();
}

/// Clears the audio queue
Future<void> clear() async {
await _audioHandler!.pause();
await _audioHandler!.stop();
return _audioHandler!.customAction('clear');
await _audioHandler.pause();
await _audioHandler.stop();
return _audioHandler.customAction('clear');
}

/// Clears and initializes the player for the next set
/// to play.
Future<void> initAudioPlayer() {
return _audioHandler!.customAction('init');
return _audioHandler.customAction('init');
}

void dispose() {
_audioHandler!.customAction('dispose');
_audioHandler.customAction('dispose');
}

void repeat() {
repeatButtonNotifier.nextState();
final RepeatState repeatMode = repeatButtonNotifier.value;
switch (repeatMode) {
case RepeatState.off:
_audioHandler!.setRepeatMode(AudioServiceRepeatMode.none);
_audioHandler.setRepeatMode(AudioServiceRepeatMode.none);
break;
case RepeatState.repeatSong:
_audioHandler!.setRepeatMode(AudioServiceRepeatMode.one);
_audioHandler.setRepeatMode(AudioServiceRepeatMode.one);
break;
case RepeatState.repeatQueue:
_audioHandler!.setRepeatMode(AudioServiceRepeatMode.all);
_audioHandler.setRepeatMode(AudioServiceRepeatMode.all);
break;
}
}
Expand All @@ -281,13 +284,13 @@ class AudioManager {
final enable = !isShuffleModeEnabledNotifier.value;
isShuffleModeEnabledNotifier.value = enable;
if (enable) {
_audioHandler!.setShuffleMode(AudioServiceShuffleMode.all);
_audioHandler.setShuffleMode(AudioServiceShuffleMode.all);
} else {
_audioHandler!.setShuffleMode(AudioServiceShuffleMode.none);
_audioHandler.setShuffleMode(AudioServiceShuffleMode.none);
}
}

Future<void> load() {
return _audioHandler!.customAction('load');
return _audioHandler.customAction('load');
}
}
11 changes: 11 additions & 0 deletions lib/constants/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ class MyConstants extends InheritedWidget {
'Telugu Stream': 'https://stream.sssmediacentre.org:8443/telugu'
};

final Map<String, String> radioStreamImages = const {
'Prasanthi Stream':
'https://www.immadisairaj.dev/radiosai/images/prasanthiStream.png',
'Bhajan Stream':
'https://www.immadisairaj.dev/radiosai/images/bhajanStream.png',
'Discourse Stream':
'https://www.immadisairaj.dev/radiosai/images/discourseStream.png',
'Telugu Stream':
'https://www.immadisairaj.dev/radiosai/images/teluguStream.png'
};

/// The list of items in the top menu bar
final Map<dynamic, String> menuTitles = const {
MenuNavigation.schedule: 'Schedule',
Expand Down
4 changes: 2 additions & 2 deletions lib/helper/media_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class MediaHelper {

String fileId = await getFileIdFromUri(link!);

Map<String, dynamic> _extras = {
Map<String, dynamic> extras = {
'uri': link,
};

Expand All @@ -59,7 +59,7 @@ class MediaHelper {
// art of the media
artUri: Uri.parse('file://$path'),
// extras['uri'] contain the uri of the media
extras: _extras,
extras: extras,
);

return tempMediaItem;
Expand Down
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ class MyApp extends StatelessWidget {
],
child: Consumer<AppThemeBloc>(
// listen to change of app theme
builder: (context, _appThemeBloc, child) {
builder: (context, appThemeBloc, child) {
return StreamBuilder<String?>(
stream: _appThemeBloc.appThemeStream as Stream<String?>?,
stream: appThemeBloc.appThemeStream as Stream<String?>?,
builder: (context, snapshot) {
String appTheme =
snapshot.data ?? MyConstants.of(context)!.appThemes[2];
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/audio_archive/audio_archive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AudioArchive extends StatefulWidget {
static const String route = 'audioArchive';

@override
_AudioArchive createState() => _AudioArchive();
State<AudioArchive> createState() => _AudioArchive();
}

class _AudioArchive extends State<AudioArchive> {
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Home extends StatefulWidget {
});

@override
_Home createState() => _Home();
State<Home> createState() => _Home();
}

class _Home extends State<Home> {
Expand Down
12 changes: 6 additions & 6 deletions lib/screens/media/media.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Media extends StatefulWidget {
final String? title;

@override
_Media createState() => _Media();
State<Media> createState() => _Media();
}

class _Media extends State<Media> {
Expand Down Expand Up @@ -131,10 +131,10 @@ class _Media extends State<Media> {

// updates the media screen based on download state
child: Consumer<MediaScreenBloc>(builder:
(context, _mediaScreenStateBloc, child) {
(context, mediaScreenStateBloc, child) {
return StreamBuilder<bool?>(
stream: _mediaScreenStateBloc
.mediaScreenStream as Stream<bool?>?,
stream: mediaScreenStateBloc.mediaScreenStream
as Stream<bool?>?,
builder: (context, snapshot) {
// can use the below commented line to know if updated
// bool screenUpdate = snapshot.data ?? false;
Expand Down Expand Up @@ -545,7 +545,7 @@ class _Media extends State<Media> {
await MediaHelper.generateMediaItem(name, link, isFileExists);

// passing params to send the source to play
Map<String, dynamic> _params = {
Map<String, dynamic> params = {
'id': tempMediaItem.id,
'album': tempMediaItem.album,
'title': tempMediaItem.title,
Expand All @@ -555,7 +555,7 @@ class _Media extends State<Media> {
};

_audioManager!.stop();
await _audioManager!.init(MediaType.media, _params);
await _audioManager!.init(MediaType.media, params);
}

/// add a new media item to the end of the queue
Expand Down
Loading

0 comments on commit ee46cc9

Please sign in to comment.