Android: Fixed audio files not loading (and possibly crashing)

* Added a missing return value for Android's `sf::FileInputStream::open()`.
* Added a missing return value for Android's `sf::priv::ResourceStream::seek()`.
* Moved error logging for `sf::InputSoundFile` and `sf::OutputSoundFile` to `sf::SoundFileFactory`, since this allows more details on *why* reading/writing failed. Before missing files would return "format not supported".
This commit is contained in:
Mario Liebisch 2015-05-12 10:40:29 +02:00 committed by Lukas Dürrenberger
parent 11e2901403
commit 717bd85537
5 changed files with 10 additions and 15 deletions

View File

@ -65,10 +65,7 @@ bool InputSoundFile::openFromFile(const std::string& filename)
// Find a suitable reader for the file type // Find a suitable reader for the file type
m_reader = SoundFileFactory::createReaderFromFilename(filename); m_reader = SoundFileFactory::createReaderFromFilename(filename);
if (!m_reader) if (!m_reader)
{
err() << "Failed to open sound file \"" << filename << "\" (format not supported)" << std::endl;
return false; return false;
}
// Wrap the file into a stream // Wrap the file into a stream
FileInputStream* file = new FileInputStream; FileInputStream* file = new FileInputStream;
@ -108,10 +105,7 @@ bool InputSoundFile::openFromMemory(const void* data, std::size_t sizeInBytes)
// Find a suitable reader for the file type // Find a suitable reader for the file type
m_reader = SoundFileFactory::createReaderFromMemory(data, sizeInBytes); m_reader = SoundFileFactory::createReaderFromMemory(data, sizeInBytes);
if (!m_reader) if (!m_reader)
{
err() << "Failed to open sound file from memory (format not supported)" << std::endl;
return false; return false;
}
// Wrap the memory file into a stream // Wrap the memory file into a stream
MemoryInputStream* memory = new MemoryInputStream; MemoryInputStream* memory = new MemoryInputStream;
@ -147,10 +141,7 @@ bool InputSoundFile::openFromStream(InputStream& stream)
// Find a suitable reader for the file type // Find a suitable reader for the file type
m_reader = SoundFileFactory::createReaderFromStream(stream); m_reader = SoundFileFactory::createReaderFromStream(stream);
if (!m_reader) if (!m_reader)
{
err() << "Failed to open sound file from stream (format not supported)" << std::endl;
return false; return false;
}
// store the stream // store the stream
m_stream = &stream; m_stream = &stream;

View File

@ -28,7 +28,6 @@
#include <SFML/Audio/OutputSoundFile.hpp> #include <SFML/Audio/OutputSoundFile.hpp>
#include <SFML/Audio/SoundFileWriter.hpp> #include <SFML/Audio/SoundFileWriter.hpp>
#include <SFML/Audio/SoundFileFactory.hpp> #include <SFML/Audio/SoundFileFactory.hpp>
#include <SFML/System/Err.hpp>
namespace sf namespace sf
@ -57,10 +56,7 @@ bool OutputSoundFile::openFromFile(const std::string& filename, unsigned int sam
// Find a suitable writer for the file type // Find a suitable writer for the file type
m_writer = SoundFileFactory::createWriterFromFilename(filename); m_writer = SoundFileFactory::createWriterFromFilename(filename);
if (!m_writer) if (!m_writer)
{
err() << "Failed to open sound file \"" << filename << "\" (format not supported)" << std::endl;
return false; return false;
}
// Pass the stream to the reader // Pass the stream to the reader
if (!m_writer->open(filename, sampleRate, channelCount)) if (!m_writer->open(filename, sampleRate, channelCount))

View File

@ -34,6 +34,7 @@
#include <SFML/Audio/SoundFileWriterWav.hpp> #include <SFML/Audio/SoundFileWriterWav.hpp>
#include <SFML/System/FileInputStream.hpp> #include <SFML/System/FileInputStream.hpp>
#include <SFML/System/MemoryInputStream.hpp> #include <SFML/System/MemoryInputStream.hpp>
#include <SFML/System/Err.hpp>
namespace namespace
@ -69,8 +70,10 @@ SoundFileReader* SoundFileFactory::createReaderFromFilename(const std::string& f
// Wrap the input file into a file stream // Wrap the input file into a file stream
FileInputStream stream; FileInputStream stream;
if (!stream.open(filename)) if (!stream.open(filename)) {
err() << "Failed to open sound file \"" << filename << "\" (couldn't open stream)" << std::endl;
return NULL; return NULL;
}
// Test the filename in all the registered factories // Test the filename in all the registered factories
for (ReaderFactoryArray::const_iterator it = s_readers.begin(); it != s_readers.end(); ++it) for (ReaderFactoryArray::const_iterator it = s_readers.begin(); it != s_readers.end(); ++it)
@ -81,6 +84,7 @@ SoundFileReader* SoundFileFactory::createReaderFromFilename(const std::string& f
} }
// No suitable reader found // No suitable reader found
err() << "Failed to open sound file \"" << filename << "\" (format not supported)" << std::endl;
return NULL; return NULL;
} }
@ -104,6 +108,7 @@ SoundFileReader* SoundFileFactory::createReaderFromMemory(const void* data, std:
} }
// No suitable reader found // No suitable reader found
err() << "Failed to open sound file from memory (format not supported)" << std::endl;
return NULL; return NULL;
} }
@ -123,6 +128,7 @@ SoundFileReader* SoundFileFactory::createReaderFromStream(InputStream& stream)
} }
// No suitable reader found // No suitable reader found
err() << "Failed to open sound file from stream (format not supported)" << std::endl;
return NULL; return NULL;
} }
@ -141,6 +147,7 @@ SoundFileWriter* SoundFileFactory::createWriterFromFilename(const std::string& f
} }
// No suitable writer found // No suitable writer found
err() << "Failed to open sound file \"" << filename << "\" (format not supported)" << std::endl;
return NULL; return NULL;
} }

View File

@ -63,7 +63,7 @@ Int64 ResourceStream::read(void *data, Int64 size)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Int64 ResourceStream::seek(Int64 position) Int64 ResourceStream::seek(Int64 position)
{ {
AAsset_seek(m_file, position, SEEK_SET); return AAsset_seek(m_file, position, SEEK_SET);
} }

View File

@ -61,6 +61,7 @@ bool FileInputStream::open(const std::string& filename)
if (m_file) if (m_file)
delete m_file; delete m_file;
m_file = new sf::priv::ResourceStream(filename); m_file = new sf::priv::ResourceStream(filename);
return m_file->tell() != -1;
#else #else
if (m_file) if (m_file)
std::fclose(m_file); std::fclose(m_file);