Ticket #1503: 0001-MediaPlayer-remember-position-and-volume.patch

File 0001-MediaPlayer-remember-position-and-volume.patch, 14.2 KB (added by frizer23, 5 years ago)
  • src/apps/mediaplayer/Controller.cpp

    From d4806992aef33492c7a1dd623678dd3d03492158 Mon Sep 17 00:00:00 2001
    From: Julien Lepiller <julien@lepiller.eu>
    Date: Sat, 20 Sep 2014 20:36:51 +0200
    Subject: [PATCH] MediaPlayer: remember position and volume
    
    ---
     src/apps/mediaplayer/Controller.cpp                | 52 +++++++++++++++++++++-
     src/apps/mediaplayer/Controller.h                  |  4 ++
     src/apps/mediaplayer/MainWin.cpp                   | 11 ++++-
     src/apps/mediaplayer/playlist/FilePlaylistItem.cpp | 32 ++++++++++++-
     src/apps/mediaplayer/playlist/FilePlaylistItem.h   |  7 ++-
     src/apps/mediaplayer/playlist/PlaylistItem.cpp     | 29 ++++++++++++
     src/apps/mediaplayer/playlist/PlaylistItem.h       | 16 ++++++-
     src/apps/mediaplayer/settings/Settings.cpp         |  5 +++
     src/apps/mediaplayer/settings/Settings.h           |  6 +++
     src/apps/mediaplayer/settings/SettingsWindow.cpp   | 12 +++++
     src/apps/mediaplayer/settings/SettingsWindow.h     |  1 +
     11 files changed, 170 insertions(+), 5 deletions(-)
    
    diff --git a/src/apps/mediaplayer/Controller.cpp b/src/apps/mediaplayer/Controller.cpp
    index 5176a3d..4ffc100 100644
    a b  
    2929
    3030#include <Autolock.h>
    3131#include <Bitmap.h>
     32#include <Catalog.h>
    3233#include <Debug.h>
    3334#include <Path.h>
    3435#include <Window.h> // for debugging only
     
    5051#include "TrackSupplier.h"
    5152#include "VideoTrackSupplier.h"
    5253
     54#undef B_TRANSLATION_CONTEXT
     55#define B_TRANSLATION_CONTEXT "MediaPlayer-Controller"
     56#define MIN_WIDTH 250
     57
    5358using std::nothrow;
    5459
    5560
    Controller::SetToAsync(const PlaylistItemRef& item) 
    219224    return B_OK;
    220225}
    221226
    222 
    223227status_t
    224228Controller::SetTo(const PlaylistItemRef& item)
    225229{
    Controller::TimePosition() 
    679683    return _TimePosition();
    680684}
    681685
     686status_t
     687Controller::SaveState(bool reset)
     688{
     689    if (fItem.Get() == NULL)
     690        return B_OK;
     691    if (reset)
     692            fCurrentFrame = 0;
     693    status_t status = fItem.Get()->SetLastVolume(fVolume);
     694    if (status == B_OK)
     695        status = fItem.Get()->SetLastFrame(fCurrentFrame);
     696    else
     697        fItem.Get()->SetLastFrame(fCurrentFrame);
     698    return status;
     699}
     700
     701void
     702Controller::RestoreState()
     703{
     704    PlaylistItem *item =fItem.Get();
     705    if (item == NULL)
     706        return;
     707
     708    Pause();
     709
     710    if (item->LastFrame() > 0) {
     711        bool resume = fResume == mpSettings::RESUME_ALWAYS;
     712        if (fResume == mpSettings::RESUME_ASK) {
     713            BString label;
     714            int32 time = (int32)((float)item->LastFrame() * TimeDuration() / (1000000*_FrameDuration()));
     715            label.SetToFormat(B_TRANSLATE("Do you want to resume %s at %dm%ds?"),
     716                item->Name().String(), time/60, time%60);
     717            BAlert *alert = new BAlert(B_TRANSLATE("Resume?"), label, B_TRANSLATE("Resume"), B_TRANSLATE("Reset"));
     718            resume = alert->Go() == 0;
     719        }
     720
     721        if (resume)
     722            SetFramePosition(item->LastFrame());
     723    }
     724
     725    float lastVolume = item->LastVolume();
     726    if (lastVolume >= 0)
     727        SetVolume(lastVolume);
     728
     729    Play();
     730}
    682731
    683732void
    684733Controller::SetVolume(float value)
    Controller::_AdoptGlobalSettings() 
    9741023    fLoopMovies = settings.loopMovie;
    9751024    fLoopSounds = settings.loopSound;
    9761025    fBackgroundMovieVolumeMode = settings.backgroundMovieVolumeMode;
     1026    fResume = settings.resume;
    9771027}
    9781028
    9791029
  • src/apps/mediaplayer/Controller.h

    diff --git a/src/apps/mediaplayer/Controller.h b/src/apps/mediaplayer/Controller.h
    index 1eeda17..451d55a 100644
    a b  
    2222#define __CONTROLLER_H
    2323
    2424
     25#include <Alert.h>
    2526#include <Entry.h>
    2627#include <MediaDefs.h>
    2728#include <MediaFormats.h>
    public: 
    115116
    116117            bigtime_t           TimeDuration();
    117118            bigtime_t           TimePosition();
     119            status_t            SaveState(bool reset = false);
     120            void                RestoreState();
    118121
    119122    virtual void                SetVolume(float factor);
    120123            float               Volume();
    private: 
    224227            bool                fLoopMovies;
    225228            bool                fLoopSounds;
    226229            uint32              fBackgroundMovieVolumeMode;
     230            uint32              fResume;
    227231
    228232            BList               fListeners;
    229233};
  • src/apps/mediaplayer/MainWin.cpp

    diff --git a/src/apps/mediaplayer/MainWin.cpp b/src/apps/mediaplayer/MainWin.cpp
    index 3229867..f5eb7e7 100644
    a b MainWin::MessageReceived(BMessage* msg) 
    697697        {
    698698            BAutolock _(fPlaylist);
    699699
     700            //The file is finished. Open at start next time.
     701            fController->SaveState(true);
     702
    700703            bool hadNext = fPlaylist->SetCurrentItemIndex(
    701704                fPlaylist->CurrentItemIndex() + 1);
    702705            if (!hadNext) {
    MainWin::MessageReceived(BMessage* msg) 
    799802            float volume;
    800803            if (msg->FindFloat("volume", &volume) == B_OK)
    801804                fControls->SetVolume(volume);
     805            fController->SaveState();
    802806            break;
    803807        }
    804808        case MSG_CONTROLLER_MUTED_CHANGED:
    MainWin::WindowActivated(bool active) 
    10671071bool
    10681072MainWin::QuitRequested()
    10691073{
     1074    fController->SaveState();
    10701075    BMessage message(M_PLAYER_QUIT);
    10711076    GetQuitMessage(&message);
    10721077    be_app->PostMessage(&message);
    MainWin::_PlaylistItemOpened(const PlaylistItemRef& item, status_t result) 
    14011406        }
    14021407        fController->SetTimePosition(fInitialSeekPosition);
    14031408        fInitialSeekPosition = 0;
     1409
     1410        if (fPlaylist->CountItems() == 1)
     1411            fController->RestoreState();
    14041412    }
    14051413    _SetupWindow();
    14061414
    MainWin::_Wind(bigtime_t howMuch, int64 frames) 
    24522460        } else if (seekTime > fController->TimeDuration()) {
    24532461            fInitialSeekPosition = 0;
    24542462            PostMessage(M_SKIP_NEXT);
    2455         } else
     2463        } else {
    24562464            fController->SetTimePosition(seekTime);
     2465        }
    24572466    }
    24582467
    24592468    fController->Unlock();
  • src/apps/mediaplayer/playlist/FilePlaylistItem.cpp

    diff --git a/src/apps/mediaplayer/playlist/FilePlaylistItem.cpp b/src/apps/mediaplayer/playlist/FilePlaylistItem.cpp
    index 4b1cafd..cd41216 100644
    a b status_t 
    190190FilePlaylistItem::SetAttribute(const Attribute& attribute,
    191191    const int64& value)
    192192{
     193    if (attribute == ATTR_INT64_FRAME)
     194        return _SetAttribute("Media:Frame", B_INT64_TYPE, &value,
     195            sizeof(int64));
     196
    193197    return B_NOT_SUPPORTED;
    194198}
    195199
    status_t 
    198202FilePlaylistItem::GetAttribute(const Attribute& attribute,
    199203    int64& value) const
    200204{
     205    if (attribute == ATTR_INT64_FRAME)
     206        return _GetAttribute("Media:Frame", B_INT64_TYPE, &value,
     207            sizeof(int64));
     208
     209    return B_NOT_SUPPORTED;
     210}
     211
     212status_t
     213FilePlaylistItem::SetAttribute(const Attribute& attribute,
     214    const float& value)
     215{
     216        if (attribute == ATTR_FLOAT_VOLUME)
     217            return _SetAttribute("Media:Volume", B_FLOAT_TYPE, &value,
     218                sizeof(float));
     219
     220        return B_NOT_SUPPORTED;
     221}
     222
     223status_t
     224FilePlaylistItem::GetAttribute(const Attribute& attribute,
     225    float& value) const
     226{
     227    if (attribute == ATTR_FLOAT_VOLUME)
     228        return _GetAttribute("Media:Volume", B_FLOAT_TYPE, &value,
     229            sizeof(float));
     230
    201231    return B_NOT_SUPPORTED;
    202232}
    203233
    FilePlaylistItem::_SetAttribute(const char* attrName, type_code type, 
    407437
    408438status_t
    409439FilePlaylistItem::_GetAttribute(const char* attrName, type_code type,
    410     void* data, size_t size)
     440    void* data, size_t size) const
    411441{
    412442    BEntry entry(&fRefs[0], true);
    413443    BNode node(&entry);
  • src/apps/mediaplayer/playlist/FilePlaylistItem.h

    diff --git a/src/apps/mediaplayer/playlist/FilePlaylistItem.h b/src/apps/mediaplayer/playlist/FilePlaylistItem.h
    index a0c4da4..126cf78 100644
    a b public: 
    4343                                    const int64& value);
    4444    virtual status_t            GetAttribute(const Attribute& attribute,
    4545                                    int64& value) const;
     46   
     47    virtual status_t            SetAttribute(const Attribute& attribute,
     48                                    const float& value);
     49    virtual status_t            GetAttribute(const Attribute& attribute,
     50                                    float& value) const;
    4651
    4752    // methods
    4853    virtual BString             LocationURI() const;
    private: 
    6772                                    size_t size);
    6873            status_t            _GetAttribute(const char* attrName,
    6974                                    type_code type, void* data,
    70                                     size_t size);
     75                                    size_t size) const;
    7176            status_t            _MoveIntoTrash(vector<entry_ref>* refs,
    7277                                    vector<BString>* namesInTrash);
    7378            status_t            _RestoreFromTrash(vector<entry_ref>* refs,
  • src/apps/mediaplayer/playlist/PlaylistItem.cpp

    diff --git a/src/apps/mediaplayer/playlist/PlaylistItem.cpp b/src/apps/mediaplayer/playlist/PlaylistItem.cpp
    index fde90a8..de91700 100644
    a b PlaylistItem::TrackNumber() const 
    106106    return trackNumber;
    107107}
    108108
     109int64
     110PlaylistItem::LastFrame() const
     111{
     112    int64 lastFrame;
     113    if (GetAttribute(ATTR_INT64_FRAME, lastFrame) != B_OK)
     114        lastFrame = 0;
     115    return lastFrame;
     116}
     117
     118float
     119PlaylistItem::LastVolume() const
     120{
     121    float lastVolume;
     122    if (GetAttribute(ATTR_FLOAT_VOLUME, lastVolume) != B_OK)
     123        lastVolume = -1;
     124    return lastVolume;
     125}
     126
     127status_t
     128PlaylistItem::SetLastFrame(int64 value)
     129{
     130    return SetAttribute(ATTR_INT64_FRAME, value);
     131}
     132
     133status_t
     134PlaylistItem::SetLastVolume(float value)
     135{
     136    return SetAttribute(ATTR_FLOAT_VOLUME, value);
     137}
    109138
    110139void
    111140PlaylistItem::SetPlaybackFailed()
  • src/apps/mediaplayer/playlist/PlaylistItem.h

    diff --git a/src/apps/mediaplayer/playlist/PlaylistItem.h b/src/apps/mediaplayer/playlist/PlaylistItem.h
    index b896df5..a34f9a7 100644
    a b public: 
    5353
    5454        ATTR_INT32_TRACK            = 'trck',
    5555        ATTR_INT32_YEAR             = 'year',
    56         ATTR_INT32_RATING           = 'rtng'
     56        ATTR_INT32_RATING           = 'rtng',
     57
     58        ATTR_INT64_FRAME            = 'fram',
     59
     60        ATTR_FLOAT_VOLUME           = 'volu'
    5761    } Attribute;
    5862
    5963    virtual status_t            SetAttribute(const Attribute& attribute,
    public: 
    7074                                    const int64& value) = 0;
    7175    virtual status_t            GetAttribute(const Attribute& attribute,
    7276                                    int64& value) const = 0;
     77   
     78    virtual status_t            SetAttribute(const Attribute& attribute,
     79                                    const float& value) = 0;
     80    virtual status_t            GetAttribute(const Attribute& attribute,
     81                                    float& value) const = 0;
    7382
    7483    // convenience access to attributes
    7584            BString             Name() const;
    public: 
    7887            BString             Title() const;
    7988
    8089            int32               TrackNumber() const;
     90            int64               LastFrame() const;
     91            float               LastVolume() const;
     92
     93            status_t            SetLastFrame(int64 value);
     94            status_t            SetLastVolume(float value);
    8195
    8296    // methods
    8397    virtual BString             LocationURI() const = 0;
  • src/apps/mediaplayer/settings/Settings.cpp

    diff --git a/src/apps/mediaplayer/settings/Settings.cpp b/src/apps/mediaplayer/settings/Settings.cpp
    index 7f1d00d..a4b22f9 100644
    a b mpSettings::operator!=(const mpSettings& other) const 
    2626        || useOverlays != other.useOverlays
    2727        || scaleBilinear != other.scaleBilinear
    2828        || scaleFullscreenControls != other.scaleFullscreenControls
     29        || resume != other.resume
    2930        || subtitleSize != other.subtitleSize
    3031        || subtitlePlacement != other.subtitlePlacement
    3132        || backgroundMovieVolumeMode != other.backgroundMovieVolumeMode
    Settings::Get(mpSettings& settings) const 
    6162    settings.scaleFullscreenControls
    6263        = fSettingsMessage.GetValue("scaleFullscreenControls", true);
    6364
     65    settings.resume
     66        = fSettingsMessage.GetValue("resume",
     67            (uint32)mpSettings::RESUME_ASK);
    6468    settings.subtitleSize
    6569        = fSettingsMessage.GetValue("subtitleSize",
    6670            (uint32)mpSettings::SUBTITLE_SIZE_MEDIUM);
    Settings::Update(const mpSettings& settings) 
    9599    fSettingsMessage.SetValue("scaleFullscreenControls",
    96100        settings.scaleFullscreenControls);
    97101
     102    fSettingsMessage.SetValue("resume", settings.resume);
    98103    fSettingsMessage.SetValue("subtitleSize", settings.subtitleSize);
    99104    fSettingsMessage.SetValue("subtitlePlacement", settings.subtitlePlacement);
    100105
  • src/apps/mediaplayer/settings/Settings.h

    diff --git a/src/apps/mediaplayer/settings/Settings.h b/src/apps/mediaplayer/settings/Settings.h
    index f8b3590..5dbc499 100644
    a b struct mpSettings { 
    3434                BG_MOVIES_HALF_VLUME = 1,
    3535                BG_MOVIES_MUTED = 2
    3636            };
     37            enum {
     38                RESUME_NEVER = 0,
     39                RESUME_ASK = 1,
     40                RESUME_ALWAYS = 2
     41            };
    3742
    3843            bool                autostart;
    3944            bool                closeWhenDonePlayingMovie;
    struct mpSettings { 
    4348            bool                useOverlays;
    4449            bool                scaleBilinear;
    4550            bool                scaleFullscreenControls;
     51            uint32              resume;
    4652            uint32              subtitleSize;
    4753            uint32              subtitlePlacement;
    4854            uint32              backgroundMovieVolumeMode;
  • src/apps/mediaplayer/settings/SettingsWindow.cpp

    diff --git a/src/apps/mediaplayer/settings/SettingsWindow.cpp b/src/apps/mediaplayer/settings/SettingsWindow.cpp
    index ea1cc17..d3eaa62 100644
    a b SettingsWindow::SettingsWindow(BRect frame) 
    9999        B_TRANSLATE("Scale controls in full screen mode"),
    100100        new BMessage(M_SETTINGS_CHANGED));
    101101
     102    fResumeOP = new BOptionPopUp("resume",
     103        B_TRANSLATE("Resume:"), new BMessage(M_SETTINGS_CHANGED));
     104    fResumeOP->AddOption(
     105        B_TRANSLATE("never"), mpSettings::RESUME_NEVER);
     106    fResumeOP->AddOption(
     107        B_TRANSLATE("ask every time"), mpSettings::RESUME_ASK);
     108    fResumeOP->AddOption(
     109        B_TRANSLATE("always"), mpSettings::RESUME_ALWAYS);
     110
    102111    fSubtitleSizeOP = new BOptionPopUp("subtitleSize",
    103112        B_TRANSLATE("Subtitle size:"), new BMessage(M_SETTINGS_CHANGED));
    104113    fSubtitleSizeOP->AddOption(
    SettingsWindow::SettingsWindow(BRect frame) 
    156165                    .End()
    157166                    .Add(fLoopMoviesCB)
    158167                    .Add(fLoopSoundsCB)
     168                    .Add(fResumeOP)
    159169                .End()
    160170            .End()
    161171            .AddStrut(kSpacing)
    SettingsWindow::AdoptSettings() 
    270280    fScaleBilinearCB->SetValue(fSettings.scaleBilinear);
    271281    fScaleFullscreenControlsCB->SetValue(fSettings.scaleFullscreenControls);
    272282
     283    fResumeOP->SetValue(fSettings.resume);
    273284    fSubtitleSizeOP->SetValue(fSettings.subtitleSize);
    274285    fSubtitlePlacementOP->SetValue(fSettings.subtitlePlacement);
    275286
    SettingsWindow::ApplySettings() 
    300311    fSettings.scaleFullscreenControls
    301312        = fScaleFullscreenControlsCB->Value() == B_CONTROL_ON;
    302313
     314    fSettings.resume = fResumeOP->Value();
    303315    fSettings.subtitleSize = fSubtitleSizeOP->Value();
    304316    fSettings.subtitlePlacement = fSubtitlePlacementOP->Value();
    305317
  • src/apps/mediaplayer/settings/SettingsWindow.h

    diff --git a/src/apps/mediaplayer/settings/SettingsWindow.h b/src/apps/mediaplayer/settings/SettingsWindow.h
    index 6285620..312927a 100644
    a b private: 
    4747        BCheckBox*              fScaleBilinearCB;
    4848        BCheckBox*              fScaleFullscreenControlsCB;
    4949
     50        BOptionPopUp*           fResumeOP;
    5051        BOptionPopUp*           fSubtitleSizeOP;
    5152        BOptionPopUp*           fSubtitlePlacementOP;
    5253