Skip to content

Commit

Permalink
Android: Pitch shifting and time shifting now work properly! Connecte…
Browse files Browse the repository at this point in the history
…d presenter/view logic for PitchShiftingFragment and TimeShiftingFragment. EqualizerPresetsActivity can now be opened from PlayerActivity.

Related to issue #406.
  • Loading branch information
ycastonguay committed Jul 2, 2013
1 parent e69bac3 commit 7fd437e
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 145 deletions.
Expand Up @@ -91,7 +91,9 @@ public override bool OnOptionsItemSelected(IMenuItem item)
{
case global::Android.Resource.Id.Home:
var intent = new Intent(this, typeof (MainActivity));
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
// TODO: If this activity is opened from the PlayerActivity, this returns to the MainActivity because of SingleTop.
// But if SingleTop isn't added, the view returns to a new MainActivity. The standard back button works properly though. ARGH!!!
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
this.StartActivity(intent);
this.Finish();
return true;
Expand Down
18 changes: 17 additions & 1 deletion MPfm/MPfm.Android/Classes/Activities/PlayerActivity.cs
Expand Up @@ -36,14 +36,15 @@
using MPfm.MVP.Views;
using MPfm.Player.Objects;
using MPfm.Sound.AudioFiles;
using TinyMessenger;
using Exception = System.Exception;

namespace MPfm.Android
{
[Activity(Label = "Player", ScreenOrientation = ScreenOrientation.Sensor, Theme = "@style/MyAppTheme", ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.Orientation | ConfigChanges.ScreenSize)]
public class PlayerActivity : BaseActivity, IPlayerView
{
private bool _isPositionChanging;
private ITinyMessengerHub _messengerHub;
private BitmapCache _bitmapCache;
private ImageView _imageViewAlbumArt;
private TextView _lblPosition;
Expand All @@ -56,11 +57,14 @@ public class PlayerActivity : BaseActivity, IPlayerView
private ViewPager _viewPager;
private TabPagerAdapter _tabPagerAdapter;
private MobileNavigationManager _navigationManager;
private bool _isPositionChanging;

protected override void OnCreate(Bundle bundle)
{
Console.WriteLine("PlayerActivity - OnCreate");
_messengerHub = Bootstrapper.GetContainer().Resolve<ITinyMessengerHub>();
base.OnCreate(bundle);

SetContentView(Resource.Layout.Player);
ActionBar.SetDisplayHomeAsUpEnabled(true);
ActionBar.SetHomeButtonEnabled(true);
Expand Down Expand Up @@ -161,6 +165,13 @@ protected override void OnSaveInstanceState(Bundle outState)
outState.PutString("key", DateTime.Now.ToLongTimeString());
}

public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.player_menu, menu);
Console.WriteLine("PlayerActivity - OnCreateOptionsMenu");
return true;
}

public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
Expand All @@ -172,6 +183,11 @@ public override bool OnOptionsItemSelected(IMenuItem item)
this.Finish();
return true;
break;
case Resource.Id.playerMenu_item_effects:
Console.WriteLine("PlayerActivity - Menu item click - Showing equalizer presets view...");
_messengerHub.PublishAsync<MobileNavigationManagerCommandMessage>(new MobileNavigationManagerCommandMessage(this, MobileNavigationManagerCommandMessageType.ShowEqualizerPresetsView));
return true;
break;
default:
return base.OnOptionsItemSelected(item);
break;
Expand Down
55 changes: 52 additions & 3 deletions MPfm/MPfm.Android/Classes/Fragments/PitchShiftingFragment.cs
Expand Up @@ -27,9 +27,17 @@

namespace MPfm.Android.Classes.Fragments
{
public class PitchShiftingFragment : BaseFragment, IPitchShiftingView, View.IOnClickListener
public class PitchShiftingFragment : BaseFragment, IPitchShiftingView
{
private View _view;
private TextView _lblCurrentKeyValue;
private TextView _lblReferenceKeyValue;
private TextView _lblNewKeyValue;
private SeekBar _seekBar;
private Button _btnReset;
private Button _btnIncrement;
private Button _btnDecrement;
private Tuple<int, string> _currentKey;

// Leave an empty constructor or the application will crash at runtime
public PitchShiftingFragment() : base(null) { }
Expand All @@ -42,12 +50,28 @@ public PitchShiftingFragment(Action<IBaseView> onViewReady)
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_view = inflater.Inflate(Resource.Layout.PitchShifting, container, false);
_lblCurrentKeyValue = _view.FindViewById<TextView>(Resource.Id.pitchShifting_lblCurrentIntervalValue);
_lblReferenceKeyValue = _view.FindViewById<TextView>(Resource.Id.pitchShifting_lblReferenceKeyValue);
_lblNewKeyValue = _view.FindViewById<TextView>(Resource.Id.pitchShifting_lblNewKeyValue);
_seekBar = _view.FindViewById<SeekBar>(Resource.Id.pitchShifting_seekBar);
_btnReset = _view.FindViewById<Button>(Resource.Id.pitchShifting_btnReset);
_btnIncrement = _view.FindViewById<Button>(Resource.Id.pitchShifting_btnIncrement);
_btnDecrement = _view.FindViewById<Button>(Resource.Id.pitchShifting_btnDecrement);

_btnReset.Click += (sender, args) => OnResetInterval();
_btnIncrement.Click += (sender, args) => OnIncrementInterval();
_btnDecrement.Click += (sender, args) => OnDecrementInterval();

_seekBar.ProgressChanged += SeekBarOnProgressChanged;
return _view;
}

public void OnClick(View v)
private void SeekBarOnProgressChanged(object sender, SeekBar.ProgressChangedEventArgs e)
{

// Pitch shifting range: -12 to +12. Seek bar range: 0-23
int interval = _seekBar.Progress - 12;
Console.WriteLine("SeekBarProgressChanged progress: {0} interval: {1}", _seekBar.Progress, interval);
OnSetInterval(interval);
}

#region IPitchShiftingView implementation
Expand All @@ -60,6 +84,13 @@ public void OnClick(View v)

public void PitchShiftingError(Exception ex)
{
Activity.RunOnUiThread(() => {
AlertDialog ad = new AlertDialog.Builder(Activity).Create();
ad.SetCancelable(false);
ad.SetMessage(string.Format("An error has occured in PitchShifting: {0}", ex));
ad.SetButton("OK", (sender, args) => ad.Dismiss());
ad.Show();
});
}

public void RefreshKeys(List<Tuple<int, string>> keys)
Expand All @@ -68,6 +99,24 @@ public void RefreshKeys(List<Tuple<int, string>> keys)

public void RefreshPitchShifting(PlayerPitchShiftingEntity entity)
{
try
{
Activity.RunOnUiThread(() => {
_currentKey = entity.ReferenceKey;
_lblReferenceKeyValue.Text = entity.ReferenceKey.Item2;
_lblNewKeyValue.Text = entity.NewKey.Item2;
_lblCurrentKeyValue.Text = entity.Interval;
// Pitch shifting range: -12 to +12. Seek bar range: 0-23
int seekBarProgress = entity.IntervalValue + 12;
Console.WriteLine("PitchShiftingFragment - RefreshPitchShifting - interval: {0} seekBarProgress: {1}", entity.IntervalValue, seekBarProgress);
_seekBar.Progress = seekBarProgress;
});
}
catch (Exception ex)
{
Console.WriteLine("PitchShiftingFragment - RefreshTimeShifting - Exception: {0}", ex);
}
}

#endregion
Expand Down
65 changes: 57 additions & 8 deletions MPfm/MPfm.Android/Classes/Fragments/TimeShiftingFragment.cs
Expand Up @@ -27,9 +27,16 @@

namespace MPfm.Android.Classes.Fragments
{
public class TimeShiftingFragment : BaseFragment, ITimeShiftingView, View.IOnClickListener
public class TimeShiftingFragment : BaseFragment, ITimeShiftingView
{
private View _view;
private TextView _lblCurrentTempoValue;
private TextView _lblReferenceTempoValue;
private TextView _lblDetectedTempoValue;
private SeekBar _seekBar;
private Button _btnReset;
private Button _btnIncrement;
private Button _btnDecrement;

// Leave an empty constructor or the application will crash at runtime
public TimeShiftingFragment() : base(null) { }
Expand All @@ -42,12 +49,29 @@ public TimeShiftingFragment(Action<IBaseView> onViewReady)
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_view = inflater.Inflate(Resource.Layout.TimeShifting, container, false);
_lblCurrentTempoValue = _view.FindViewById<TextView>(Resource.Id.timeShifting_lblCurrentTempoValue);
_lblReferenceTempoValue = _view.FindViewById<TextView>(Resource.Id.timeShifting_lblReferenceTempoValue);
_lblDetectedTempoValue = _view.FindViewById<TextView>(Resource.Id.timeShifting_lblDetectedTempoValue);
_seekBar = _view.FindViewById<SeekBar>(Resource.Id.timeShifting_seekBar);
_btnReset = _view.FindViewById<Button>(Resource.Id.timeShifting_btnReset);
_btnIncrement = _view.FindViewById<Button>(Resource.Id.timeShifting_btnIncrement);
_btnDecrement = _view.FindViewById<Button>(Resource.Id.timeShifting_btnDecrement);

_btnReset.Click += (sender, args) => OnResetTimeShifting();
_btnIncrement.Click += (sender, args) => OnIncrementTempo();
_btnDecrement.Click += (sender, args) => OnDecrementTempo();

_seekBar.ProgressChanged += SeekBarOnProgressChanged;

return _view;
}

public void OnClick(View v)
{

private void SeekBarOnProgressChanged(object sender, SeekBar.ProgressChangedEventArgs progressChangedEventArgs)
{
// Time shifting range: 50% to 150%. Seek bar range: 0-1000
float timeShiftingValue = (((float)_seekBar.Progress) / 10f) + 50f;
Console.WriteLine("SeekBarProgressChanged progress: {0} timeShiftingValue: {1}", _seekBar.Progress, timeShiftingValue);
OnSetTimeShifting(timeShiftingValue);
}

#region ITimeShiftingView implementation
Expand All @@ -58,14 +82,39 @@ public void OnClick(View v)
public Action OnIncrementTempo { get; set; }
public Action OnDecrementTempo { get; set; }

public void RefreshTimeShifting(PlayerTimeShiftingEntity entity)
{
}

public void TimeShiftingError(Exception ex)
{
Activity.RunOnUiThread(() => {
AlertDialog ad = new AlertDialog.Builder(Activity).Create();
ad.SetCancelable(false);
ad.SetMessage(string.Format("An error has occured in TimeShifting: {0}", ex));
ad.SetButton("OK", (sender, args) => ad.Dismiss());
ad.Show();
});
}

public void RefreshTimeShifting(PlayerTimeShiftingEntity entity)
{
try
{
Activity.RunOnUiThread(() => {
_lblCurrentTempoValue.Text = entity.CurrentTempo;
_lblReferenceTempoValue.Text = entity.ReferenceTempo;
_lblDetectedTempoValue.Text = entity.DetectedTempo;
// The seekbar in Android doesn't have a minimum value and doesn't support floats. Lazy Google!
// Time shifting range: 50% to 150%. Seek bar range: 0-1000
int seekBarProgress = (int)Math.Ceiling(entity.TimeShiftingValue * 10f - 500f);
Console.WriteLine("TimeShiftingFragment - RefreshTimeShifting - timeShiftingValue: {0} seekBarProgress: {1}", entity.TimeShiftingValue, seekBarProgress);
_seekBar.Progress = seekBarProgress;
});
}
catch (Exception ex)
{
Console.WriteLine("TimeShiftingFragment - RefreshTimeShifting - Exception: {0}", ex);
}
}

#endregion

}
Expand Down
Expand Up @@ -42,14 +42,14 @@ public sealed class AndroidNavigationManager : MobileNavigationManager
public AndroidNavigationManager(ITinyMessengerHub messageHub)
{
_messageHub = messageHub;
_messageHub.Subscribe<MobileNavigationManagerCommandMessage>((m) =>
{
_messageHub.Subscribe<MobileNavigationManagerCommandMessage>((m) => {
switch (m.CommandType)
{
case MobileNavigationManagerCommandMessageType.ShowPlayerView:
CreatePlayerView(MobileNavigationTabType.More, null);
break;
case MobileNavigationManagerCommandMessageType.ShowEqualizerPresetsView:
CreateEqualizerPresetsView();
break;
}
});
Expand Down
1 change: 1 addition & 0 deletions MPfm/MPfm.Android/MPfm.Android.csproj
Expand Up @@ -241,6 +241,7 @@
<AndroidResource Include="Resources\animator\slide_out.xml" />
<AndroidResource Include="Resources\animator\fade_in.xml" />
<AndroidResource Include="Resources\xml\widget_player.xml" />
<AndroidResource Include="Resources\Menu\player_menu.xml" />
</ItemGroup>
<ItemGroup>
<Folder Include="Lib\armeabi\" />
Expand Down
47 changes: 44 additions & 3 deletions MPfm/MPfm.Android/Resources/Layout/PitchShifting.axml
Expand Up @@ -12,6 +12,44 @@
android:textSize="20dp"
android:textColor="#ffffffff"
android:padding="6dip" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical">
<TextView
android:id="@+id/pitchShifting_lblReferenceKey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Reference key:"
android:textSize="14dp"
android:textColor="#ffffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/pitchShifting_lblReferenceKeyValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="C (Am)"
android:textSize="14dp"
android:textColor="#ffffffff" />
</LinearLayout>
<Button
android:id="@+id/pitchShifting_btnChangeKey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Change key"
android:textColor="#ffffffff"
android:textSize="14dp"
android:padding="12dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -72,28 +110,30 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="100" />
android:max="24" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="8dp">
<Button
android:id="@+id/pitchShifting_btnIncrement"
android:id="@+id/pitchShifting_btnDecrement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="-"
android:textColor="#ffffffff"
android:textSize="14dp"
android:padding="8dp" />
<Button
android:id="@+id/pitchShifting_btnDecrement"
android:id="@+id/pitchShifting_btnIncrement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="+"
android:textColor="#ffffffff"
android:textSize="14dp"
android:padding="8dp" />
<Button
android:id="@+id/pitchShifting_btnReset"
Expand All @@ -102,6 +142,7 @@
android:layout_alignParentRight="true"
android:text="Reset"
android:textColor="#ffffffff"
android:textSize="14dp"
android:padding="8dp" />
</LinearLayout>
</LinearLayout>

0 comments on commit 7fd437e

Please sign in to comment.