This commit is contained in:
Simon Weidacher
2022-01-03 23:33:23 +01:00
parent e104256985
commit 648bbe53e6
6 changed files with 42 additions and 7 deletions

View File

@@ -12,12 +12,12 @@ AudioPlayer::AudioPlayer() {
esp_audio_vol_set(playerHandle, 50); esp_audio_vol_set(playerHandle, 50);
} }
void AudioPlayer::play(std::string uri) { void AudioPlayer::play(std::string uri, int position = 0) {
if (isLastUri(uri)) { if (isLastUri(uri)) {
esp_player_music_resume(); esp_player_music_resume();
} else { } else {
esp_player_music_stop(); esp_player_music_stop();
esp_player_sdcard_music_play(uri.c_str(), 0); esp_player_sdcard_music_play(uri.c_str(), position);
lastUri = std::string(uri); lastUri = std::string(uri);
} }
} }
@@ -28,4 +28,12 @@ void AudioPlayer::pause() {
bool AudioPlayer::isLastUri(std::string uri) { bool AudioPlayer::isLastUri(std::string uri) {
return lastUri == uri; return lastUri == uri;
}
int AudioPlayer::getPosition() {
int position;
esp_player_pos_get(&position);
return position;
} }

View File

@@ -8,8 +8,10 @@ class AudioPlayer {
public: public:
AudioPlayer(); AudioPlayer();
void play(std::string uri);
void play(std::string uri, int position);
void pause(); void pause();
int getPosition();
private: private:
bool isLastUri(std::string uri); bool isLastUri(std::string uri);

View File

@@ -21,6 +21,8 @@
* } * }
*/ */
Tag::Tag() {}
Tag::Tag(char* tagId): Tag(std::string(tagId)) { Tag::Tag(char* tagId): Tag(std::string(tagId)) {
} }
@@ -29,6 +31,15 @@ Tag::Tag(std::string tagId): tagId(tagId) {
this->playlist = std::vector<std::string>(); this->playlist = std::vector<std::string>();
} }
void Tag::setCurrentPosition(uint32_t fileIndex, uint32_t position) {
this->currentFileIndex = fileIndex;
this->currentFilePosition = position;
}
int Tag::getCurrentPosition() {
return this->currentFilePosition;
}
std::string Tag::getNextLink() { std::string Tag::getNextLink() {
return this->playlist.at(this->currentFileIndex); return this->playlist.at(this->currentFileIndex);
} }
@@ -90,6 +101,8 @@ void Tag::read() {
void Tag::write() { void Tag::write() {
std::string contents = createFileContents(); std::string contents = createFileContents();
std::cout << contents << std::endl;
std::string filePath = getFilePath(); std::string filePath = getFilePath();
std::ofstream file(filePath); std::ofstream file(filePath);

View File

@@ -11,12 +11,15 @@ class Tag {
uint32_t currentFilePosition{0}; uint32_t currentFilePosition{0};
public: public:
Tag();
Tag(std::string tagId); Tag(std::string tagId);
Tag(char* tagId); Tag(char* tagId);
void read(); void read();
void write(); void write();
bool isLinked(); bool isLinked();
std::string getNextLink(); std::string getNextLink();
void setCurrentPosition(uint32_t fileIndex, uint32_t position);
int getCurrentPosition();
private: private:
std::string createFileContents(); std::string createFileContents();

View File

@@ -20,16 +20,22 @@ void TagPlayerControl::onTagChanged(char* tagId) {
if (tagId == NULL) { if (tagId == NULL) {
// tag has been removed. stop playback // tag has been removed. stop playback
audioPlayer->pause(); audioPlayer->pause();
storePosition();
} else { } else {
playTag(tagId); playTag(tagId);
} }
} }
void TagPlayerControl::playTag(char* tagId) { void TagPlayerControl::playTag(char* tagId) {
Tag tag(tagId); currentTag = Tag(tagId);
tag.read(); currentTag.read();
if (tag.isLinked()) { if (currentTag.isLinked()) {
audioPlayer->play(tag.getNextLink()); audioPlayer->play(currentTag.getNextLink(), currentTag.getCurrentPosition());
} }
}
void TagPlayerControl::storePosition() {
currentTag.setCurrentPosition(0, audioPlayer->getPosition());
currentTag.write();
} }

View File

@@ -2,9 +2,11 @@
#include <memory> #include <memory>
#include "AudioPlayer.hpp" #include "AudioPlayer.hpp"
#include "Tag.hpp"
class TagPlayerControl { class TagPlayerControl {
std::shared_ptr<AudioPlayer> audioPlayer; std::shared_ptr<AudioPlayer> audioPlayer;
Tag currentTag;
public: public:
TagPlayerControl(std::shared_ptr<AudioPlayer> player); TagPlayerControl(std::shared_ptr<AudioPlayer> player);
@@ -13,4 +15,5 @@ class TagPlayerControl {
private: private:
void playTag(char* tagId); void playTag(char* tagId);
void storePosition();
}; };