Adding try/catch to taglib read and write to remove crash with unsupported file formats.

This commit is contained in:
Tristan Roux
2019-05-03 21:25:04 +02:00
parent 2a1e66cb74
commit ec09f3fcfa
5 changed files with 198 additions and 177 deletions

View File

@@ -283,11 +283,19 @@ namespace Opus.Api
/// <returns></returns>
public static string GetYtID(string path)
{
Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
var meta = TagLib.File.Create(new StreamFileAbstraction(path, stream, stream));
string ytID = meta.Tag.Comment;
stream.Dispose();
return ytID;
try
{
Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
var meta = TagLib.File.Create(new StreamFileAbstraction(path, stream, stream));
string ytID = meta.Tag.Comment;
stream.Dispose();
return ytID;
}
catch
{
System.Console.WriteLine("&GetYtID Error: The format of the file at " + path + " is probably unsupported.");
return null;
}
}
/// <summary>

View File

@@ -207,82 +207,90 @@ namespace Opus.Fragments
return false;
}
System.Console.WriteLine("&Creating write stream");
Stream stream = new FileStream(song.Path, FileMode.Open, FileAccess.ReadWrite);
var meta = TagLib.File.Create(new StreamFileAbstraction(song.Path, stream, stream));
System.Console.WriteLine("&Writing tags");
meta.Tag.Title = title.Text;
song.Title = title.Text;
meta.Tag.Performers = new string[] { artist.Text };
song.Artist = artist.Text;
meta.Tag.Album = album.Text;
song.Album = album.Text;
meta.Tag.Comment = youtubeID.Text;
if (queuePosition != -1 && MusicPlayer.queue.Count > queuePosition)
try
{
MusicPlayer.queue[queuePosition] = song;
Player.instance?.RefreshPlayer();
Queue.instance.NotifyItemChanged(queuePosition);
}
System.Console.WriteLine("&Creating write stream");
Stream stream = new FileStream(song.Path, FileMode.Open, FileAccess.ReadWrite);
var meta = TagLib.File.Create(new StreamFileAbstraction(song.Path, stream, stream));
if (ytThumbUri != null)
{
System.Console.WriteLine("&Writing YT Thumb");
await Task.Run(() =>
{
System.Console.WriteLine("&Writing tags");
meta.Tag.Title = title.Text;
song.Title = title.Text;
meta.Tag.Performers = new string[] { artist.Text };
song.Artist = artist.Text;
meta.Tag.Album = album.Text;
song.Album = album.Text;
meta.Tag.Comment = youtubeID.Text;
if (queuePosition != -1 && MusicPlayer.queue.Count > queuePosition)
{
MusicPlayer.queue[queuePosition] = song;
Player.instance?.RefreshPlayer();
Queue.instance.NotifyItemChanged(queuePosition);
}
if (ytThumbUri != null)
{
System.Console.WriteLine("&Writing YT Thumb");
await Task.Run(() =>
{
IPicture[] pictures = new IPicture[1];
Bitmap bitmap = Picasso.With(Application.Context).Load(ytThumbUri).Transform(new RemoveBlackBorder(true)).Get();
byte[] data;
using (var MemoryStream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 0, MemoryStream);
data = MemoryStream.ToArray();
}
bitmap.Recycle();
pictures[0] = new Picture(data);
meta.Tag.Pictures = pictures;
ytThumbUri = null;
});
}
else if (artURI != null)
{
System.Console.WriteLine("&Writing ArtURI");
IPicture[] pictures = new IPicture[1];
Bitmap bitmap = Picasso.With(Application.Context).Load(ytThumbUri).Transform(new RemoveBlackBorder(true)).Get();
byte[] data;
using (var MemoryStream = new MemoryStream())
Bitmap bitmap = null;
if (tempFile)
{
bitmap.Compress(Bitmap.CompressFormat.Png, 0, MemoryStream);
data = MemoryStream.ToArray();
await Task.Run(() =>
{
bitmap = Picasso.With(this).Load(artURI).Transform(new RemoveBlackBorder(true)).Get();
});
}
bitmap.Recycle();
else
{
await Task.Run(() =>
{
bitmap = Picasso.With(this).Load(artURI).Get();
});
}
MemoryStream memoryStream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, memoryStream);
byte[] data = memoryStream.ToArray();
pictures[0] = new Picture(data);
meta.Tag.Pictures = pictures;
ytThumbUri = null;
});
if(!tempFile)
artURI = null;
ContentResolver.Delete(ContentUris.WithAppendedId(Android.Net.Uri.Parse("content://media/external/audio/albumart"), song.AlbumArt), null, null);
}
System.Console.WriteLine("&Saving");
meta.Save();
stream.Dispose();
}
else if (artURI != null)
catch(System.Exception e)
{
System.Console.WriteLine("&Writing ArtURI");
IPicture[] pictures = new IPicture[1];
Bitmap bitmap = null;
if (tempFile)
{
await Task.Run(() =>
{
bitmap = Picasso.With(this).Load(artURI).Transform(new RemoveBlackBorder(true)).Get();
});
}
else
{
await Task.Run(() =>
{
bitmap = Picasso.With(this).Load(artURI).Get();
});
}
MemoryStream memoryStream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, memoryStream);
byte[] data = memoryStream.ToArray();
pictures[0] = new Picture(data);
meta.Tag.Pictures = pictures;
if(!tempFile)
artURI = null;
ContentResolver.Delete(ContentUris.WithAppendedId(Android.Net.Uri.Parse("content://media/external/audio/albumart"), song.AlbumArt), null, null);
Toast.MakeText(this, Resource.String.format_unsupported, ToastLength.Long).Show();
System.Console.WriteLine("&EditMetadata Validate exception: (probably due to an unsupported format) - " + e.Message);
}
System.Console.WriteLine("&Saving");
meta.Save();
stream.Dispose();
System.Console.WriteLine("&Deleting temp file");
if (tempFile)
{

View File

@@ -7186,8 +7186,8 @@ namespace Opus
// aapt resource value: 0x7f0d0072
public const int abc_toolbar_collapse_description = 2131558514;
// aapt resource value: 0x7f0d0133
public const int add = 2131558707;
// aapt resource value: 0x7f0d0134
public const int add = 2131558708;
// aapt resource value: 0x7f0d00fc
public const int add_playlist = 2131558652;
@@ -7213,11 +7213,11 @@ namespace Opus
// aapt resource value: 0x7f0d0080
public const int appbar_scrolling_view_behavior = 2131558528;
// aapt resource value: 0x7f0d013d
public const int appearances = 2131558717;
// aapt resource value: 0x7f0d013e
public const int appearances = 2131558718;
// aapt resource value: 0x7f0d0130
public const int apply = 2131558704;
// aapt resource value: 0x7f0d0131
public const int apply = 2131558705;
// aapt resource value: 0x7f0d011b
public const int artist = 2131558683;
@@ -7231,11 +7231,11 @@ namespace Opus
// aapt resource value: 0x7f0d0111
public const int badplaylisturl = 2131558673;
// aapt resource value: 0x7f0d0137
public const int behavior = 2131558711;
// aapt resource value: 0x7f0d0138
public const int behavior = 2131558712;
// aapt resource value: 0x7f0d0156
public const int beta_available = 2131558742;
// aapt resource value: 0x7f0d0157
public const int beta_available = 2131558743;
// aapt resource value: 0x7f0d0081
public const int bottom_sheet_behavior = 2131558529;
@@ -7243,17 +7243,17 @@ namespace Opus
// aapt resource value: 0x7f0d00b4
public const int browse = 2131558580;
// aapt resource value: 0x7f0d0131
public const int cancel = 2131558705;
// aapt resource value: 0x7f0d0132
public const int cancel = 2131558706;
// aapt resource value: 0x7f0d015c
public const int cancelling = 2131558748;
// aapt resource value: 0x7f0d015d
public const int cancelling = 2131558749;
// aapt resource value: 0x7f0d00f2
public const int cant_delete = 2131558642;
// aapt resource value: 0x7f0d0159
public const int cant_play_non_youtube = 2131558745;
// aapt resource value: 0x7f0d015a
public const int cant_play_non_youtube = 2131558746;
// aapt resource value: 0x7f0d00b9
public const int cast = 2131558585;
@@ -7336,11 +7336,11 @@ namespace Opus
// aapt resource value: 0x7f0d0015
public const int cast_play = 2131558421;
// aapt resource value: 0x7f0d012e
public const int cast_queue_push = 2131558702;
// aapt resource value: 0x7f0d012f
public const int cast_queue_pushed = 2131558703;
public const int cast_queue_push = 2131558703;
// aapt resource value: 0x7f0d0130
public const int cast_queue_pushed = 2131558704;
// aapt resource value: 0x7f0d0016
public const int cast_rewind = 2131558422;
@@ -7405,8 +7405,8 @@ namespace Opus
// aapt resource value: 0x7f0d0083
public const int character_counter_pattern = 2131558531;
// aapt resource value: 0x7f0d0143
public const int check_updates = 2131558723;
// aapt resource value: 0x7f0d0144
public const int check_updates = 2131558724;
// aapt resource value: 0x7f0d00b0
public const int clientID = 2131558576;
@@ -7468,11 +7468,11 @@ namespace Opus
// aapt resource value: 0x7f0d003b
public const int common_signin_button_text_long = 2131558459;
// aapt resource value: 0x7f0d012d
public const int completed = 2131558701;
// aapt resource value: 0x7f0d012e
public const int completed = 2131558702;
// aapt resource value: 0x7f0d0150
public const int country_blocked = 2131558736;
// aapt resource value: 0x7f0d0151
public const int country_blocked = 2131558737;
// aapt resource value: 0x7f0d0117
public const int create_local = 2131558679;
@@ -7492,8 +7492,8 @@ namespace Opus
// aapt resource value: 0x7f0d0118
public const int create_youtube = 2131558680;
// aapt resource value: 0x7f0d0141
public const int dark_theme = 2131558721;
// aapt resource value: 0x7f0d0142
public const int dark_theme = 2131558722;
// aapt resource value: 0x7f0d00dc
public const int delete = 2131558620;
@@ -7501,8 +7501,8 @@ namespace Opus
// aapt resource value: 0x7f0d010b
public const int delete_playlist = 2131558667;
// aapt resource value: 0x7f0d0128
public const int deleted_file = 2131558696;
// aapt resource value: 0x7f0d0129
public const int deleted_file = 2131558697;
// aapt resource value: 0x7f0d00d4
public const int download = 2131558612;
@@ -7510,8 +7510,8 @@ namespace Opus
// aapt resource value: 0x7f0d0120
public const int download_albumart = 2131558688;
// aapt resource value: 0x7f0d0139
public const int download_directory = 2131558713;
// aapt resource value: 0x7f0d013a
public const int download_directory = 2131558714;
// aapt resource value: 0x7f0d0121
public const int download_meta = 2131558689;
@@ -7522,20 +7522,20 @@ namespace Opus
// aapt resource value: 0x7f0d00ed
public const int download_path_not_set = 2131558637;
// aapt resource value: 0x7f0d0127
public const int download_queue = 2131558695;
// aapt resource value: 0x7f0d0128
public const int download_queue = 2131558696;
// aapt resource value: 0x7f0d00f0
public const int downloading = 2131558640;
// aapt resource value: 0x7f0d015a
public const int downloading_notification = 2131558746;
// aapt resource value: 0x7f0d015b
public const int downloading_notification = 2131558747;
// aapt resource value: 0x7f0d012b
public const int downloading_status = 2131558699;
// aapt resource value: 0x7f0d012c
public const int downloading_status = 2131558700;
// aapt resource value: 0x7f0d0157
public const int downloading_update = 2131558743;
// aapt resource value: 0x7f0d0158
public const int downloading_update = 2131558744;
// aapt resource value: 0x7f0d00d3
public const int edit_metadata = 2131558611;
@@ -7660,6 +7660,9 @@ namespace Opus
// aapt resource value: 0x7f0d00e8
public const int folders = 2131558632;
// aapt resource value: 0x7f0d0127
public const int format_unsupported = 2131558695;
// aapt resource value: 0x7f0d0086
public const int hide_bottom_view_on_scroll_behavior = 2131558534;
@@ -7672,11 +7675,11 @@ namespace Opus
// aapt resource value: 0x7f0d00c4
public const int hours = 2131558596;
// aapt resource value: 0x7f0d0129
public const int initialization = 2131558697;
// aapt resource value: 0x7f0d012a
public const int initialization = 2131558698;
// aapt resource value: 0x7f0d0134
public const int later = 2131558708;
// aapt resource value: 0x7f0d0135
public const int later = 2131558709;
// aapt resource value: 0x7f0d00d7
public const int list_songs = 2131558615;
@@ -7693,26 +7696,26 @@ namespace Opus
// aapt resource value: 0x7f0d0100
public const int localpl_noperm = 2131558656;
// aapt resource value: 0x7f0d0147
public const int log_in = 2131558727;
// aapt resource value: 0x7f0d0148
public const int log_out = 2131558728;
// aapt resource value: 0x7f0d0146
public const int logged_in = 2131558726;
public const int log_in = 2131558728;
// aapt resource value: 0x7f0d0149
public const int login_disabled = 2131558729;
public const int log_out = 2131558729;
// aapt resource value: 0x7f0d013a
public const int max_download = 2131558714;
// aapt resource value: 0x7f0d0147
public const int logged_in = 2131558727;
// aapt resource value: 0x7f0d014a
public const int login_disabled = 2131558730;
// aapt resource value: 0x7f0d013b
public const int max_download_dialog = 2131558715;
public const int max_download = 2131558715;
// aapt resource value: 0x7f0d012a
public const int metadata = 2131558698;
// aapt resource value: 0x7f0d013c
public const int max_download_dialog = 2131558716;
// aapt resource value: 0x7f0d012b
public const int metadata = 2131558699;
// aapt resource value: 0x7f0d0124
public const int metdata_error_noid = 2131558692;
@@ -7813,8 +7816,8 @@ namespace Opus
// aapt resource value: 0x7f0d00be
public const int next_loading = 2131558590;
// aapt resource value: 0x7f0d0136
public const int no = 2131558710;
// aapt resource value: 0x7f0d0137
public const int no = 2131558711;
// aapt resource value: 0x7f0d00f7
public const int no_channel = 2131558647;
@@ -7822,8 +7825,8 @@ namespace Opus
// aapt resource value: 0x7f0d00f6
public const int no_lives = 2131558646;
// aapt resource value: 0x7f0d014b
public const int no_permission = 2131558731;
// aapt resource value: 0x7f0d014c
public const int no_permission = 2131558732;
// aapt resource value: 0x7f0d00f5
public const int no_playlist = 2131558645;
@@ -7834,17 +7837,17 @@ namespace Opus
// aapt resource value: 0x7f0d00e9
public const int no_song = 2131558633;
// aapt resource value: 0x7f0d014c
public const int no_song_mix = 2131558732;
// aapt resource value: 0x7f0d014d
public const int no_song_mix = 2131558733;
// aapt resource value: 0x7f0d00f4
public const int no_track = 2131558644;
// aapt resource value: 0x7f0d0145
public const int not_log = 2131558725;
// aapt resource value: 0x7f0d0146
public const int not_log = 2131558726;
// aapt resource value: 0x7f0d0151
public const int not_streamable = 2131558737;
// aapt resource value: 0x7f0d0152
public const int not_streamable = 2131558738;
// aapt resource value: 0x7f0d00bd
public const int nothing = 2131558589;
@@ -7852,14 +7855,14 @@ namespace Opus
// aapt resource value: 0x7f0d00c0
public const int off = 2131558592;
// aapt resource value: 0x7f0d0132
public const int ok = 2131558706;
// aapt resource value: 0x7f0d0133
public const int ok = 2131558707;
// aapt resource value: 0x7f0d00c6
public const int open_youtube = 2131558598;
// aapt resource value: 0x7f0d0142
public const int others = 2131558722;
// aapt resource value: 0x7f0d0143
public const int others = 2131558723;
// aapt resource value: 0x7f0d0088
public const int password_toggle_content_description = 2131558536;
@@ -8011,32 +8014,32 @@ namespace Opus
// aapt resource value: 0x7f0d00de
public const int sync_now = 2131558622;
// aapt resource value: 0x7f0d013c
public const int sync_remove = 2131558716;
// aapt resource value: 0x7f0d013d
public const int sync_remove = 2131558717;
// aapt resource value: 0x7f0d00f1
public const int syncing = 2131558641;
// aapt resource value: 0x7f0d015b
public const int tap_details = 2131558747;
// aapt resource value: 0x7f0d013e
public const int theme = 2131558718;
// aapt resource value: 0x7f0d015c
public const int tap_details = 2131558748;
// aapt resource value: 0x7f0d013f
public const int theme_dialog = 2131558719;
public const int theme = 2131558719;
// aapt resource value: 0x7f0d0140
public const int theme_dialog = 2131558720;
// aapt resource value: 0x7f0d00c5
public const int timer = 2131558597;
// aapt resource value: 0x7f0d014e
public const int timout = 2131558734;
// aapt resource value: 0x7f0d014f
public const int timout = 2131558735;
// aapt resource value: 0x7f0d011a
public const int title = 2131558682;
// aapt resource value: 0x7f0d014a
public const int undo = 2131558730;
// aapt resource value: 0x7f0d014b
public const int undo = 2131558731;
// aapt resource value: 0x7f0d0122
public const int undo_change = 2131558690;
@@ -8047,29 +8050,29 @@ namespace Opus
// aapt resource value: 0x7f0d010e
public const int unfork_playlist = 2131558670;
// aapt resource value: 0x7f0d014f
public const int unknow = 2131558735;
// aapt resource value: 0x7f0d0150
public const int unknow = 2131558736;
// aapt resource value: 0x7f0d00bc
public const int up_next = 2131558588;
// aapt resource value: 0x7f0d0155
public const int up_to_date = 2131558741;
// aapt resource value: 0x7f0d0156
public const int up_to_date = 2131558742;
// aapt resource value: 0x7f0d012c
public const int up_to_date_status = 2131558700;
// aapt resource value: 0x7f0d0153
public const int update = 2131558739;
// aapt resource value: 0x7f0d012d
public const int up_to_date_status = 2131558701;
// aapt resource value: 0x7f0d0154
public const int update_message = 2131558740;
public const int update = 2131558740;
// aapt resource value: 0x7f0d0152
public const int update_no_internet = 2131558738;
// aapt resource value: 0x7f0d0155
public const int update_message = 2131558741;
// aapt resource value: 0x7f0d0158
public const int updating = 2131558744;
// aapt resource value: 0x7f0d0153
public const int update_no_internet = 2131558739;
// aapt resource value: 0x7f0d0159
public const int updating = 2131558745;
// aapt resource value: 0x7f0d003e
public const int v7_preference_off = 2131558462;
@@ -8077,20 +8080,20 @@ namespace Opus
// aapt resource value: 0x7f0d003f
public const int v7_preference_on = 2131558463;
// aapt resource value: 0x7f0d0144
public const int version = 2131558724;
// aapt resource value: 0x7f0d0145
public const int version = 2131558725;
// aapt resource value: 0x7f0d0138
public const int volume = 2131558712;
// aapt resource value: 0x7f0d0139
public const int volume = 2131558713;
// aapt resource value: 0x7f0d0140
public const int white_theme = 2131558720;
// aapt resource value: 0x7f0d0141
public const int white_theme = 2131558721;
// aapt resource value: 0x7f0d0135
public const int yes = 2131558709;
// aapt resource value: 0x7f0d0136
public const int yes = 2131558710;
// aapt resource value: 0x7f0d014d
public const int youtube_endpoint = 2131558733;
// aapt resource value: 0x7f0d014e
public const int youtube_endpoint = 2131558734;
// aapt resource value: 0x7f0d00fd
public const int youtube_loading_error = 2131558653;

View File

@@ -141,7 +141,8 @@
<string name="metdata_error_noid">Impossible de télécharger les metadatas depuis youtube, l\'id youtube n\'est pas défni.</string>
<string name="mount_error">Il y a un problème avec votre disque de stockage, veuillez arreter de l\'utiliser ailleur et réessayer après.</string>
<string name="mount_error_action">ABANDONNER ET QUITER</string>
<string name="format_unsupported">Le format de ce son ne supporte pas les métadatas.</string>
<!--Download Queue-->
<string name="download_queue">Liste de téléchargement</string>
<string name="deleted_file">Fichier supprimé</string>

View File

@@ -141,6 +141,7 @@
<string name="metdata_error_noid">Can\'t get meta data on youtube, Youtube ID isn\'t set.</string>
<string name="mount_error">There is a probleme with your storage, please stop using your storage elsewhere and try again.</string>
<string name="mount_error_action">DISCARD AND LEAVE</string>
<string name="format_unsupported">The format of this song doesn\'t support metadatas.</string>
<!--Download Queue-->
<string name="download_queue">Download Queue</string>