inaka

Latest blog entries

/
The Art of Writing a Blogpost

The Art of Writing a Blogpost

Mar 09 2017 : Matias Vera

/
SpellingCI: No more spelling mistakes in your markdown flies!

Feb 14 2017 : Felipe Ripoll

/
Fast reverse geocoding with offline-geocoder

Do you need a blazing fast reverse geocoder? Enter offline-geocoder!

Jan 18 2017 : Roberto Romero

/
Using Jayme to connect to the new MongooseIM REST services

MongooseIM has RESTful services!! Here I show how you can use them in an iOS application.

Dec 13 2016 : Sergio Abraham

/
20 Questions, or Maybe a Few More

20 Questions, or Maybe a Few More

Nov 16 2016 : Stephanie Goldner

/
The Power of Meeting People

Because conferences and meetups are not just about the technical stuff.

Nov 01 2016 : Pablo Villar

/
Finding the right partner for your app build

Sharing some light on how it is to partner with us.

Oct 27 2016 : Inaka

/
Just Play my Sound

How to easily play a sound in Android

Oct 25 2016 : Giaquinta Emiliano

/
Opening our Guidelines to the World

We're publishing our work guidelines for the world to see.

Oct 13 2016 : Brujo Benavides

/
Using NIFs: the easy way

Using niffy to simplify working with NIFs on Erlang

Oct 05 2016 : Hernan Rivas Acosta

/
Function Naming In Swift 3

How to write clear function signatures, yet expressive, while following Swift 3 API design guidelines.

Sep 16 2016 : Pablo Villar

/
Jenkins automated tests for Rails

How to automatically trigger rails tests with a Jenkins job

Sep 14 2016 : Demian Sciessere

/
Erlang REST Server Stack

A description of our usual stack for building REST servers in Erlang

Sep 06 2016 : Brujo Benavides

/
Replacing JSON when talking to Erlang

Using Erlang's External Term Format

Aug 17 2016 : Hernan Rivas Acosta

/
Gadget + Lewis = Android Lint CI

Integrating our Android linter with Github's pull requests

Aug 04 2016 : Fernando Ramirez and Euen Lopez

/
Passwordless login with phoenix

Introducing how to implement passwordless login with phoenix framework

Jul 27 2016 : Thiago Borges

/
Beam Olympics

Our newest game to test your Beam Skills

Jul 14 2016 : Brujo Benavides

/
Otec

Three Open Source Projects, one App

Jun 28 2016 : Andrés Gerace

/
CredoCI

Running credo checks for elixir code on your github pull requests

Jun 16 2016 : Alejandro Mataloni

/
Thoughts on rebar3

Thoughts on rebar3

Jun 08 2016 : Hernán Rivas Acosta

/
See all Inaka's blog posts >>

/
Just Play my Sound

Giaquinta Emiliano wrote this on October 25, 2016 under android, inaka, library, playing, sound, wrapper .

When you start developing a new App, you usually put a lot of effort into the app's core functionality and, of course, into a nice design to keep your users happy. But there's not always spare time to invest on adding sounds to it too. So, here we are to help you out!

The Complexity

To play a sound in Android, the dev should be aware of an important class: MediaPlayer.

This class, in combination with AudioManager, has all the tools required to manage and play sounds in Android. You can check them at the Android Developer Official Website where you will find the required knowledge.

But even then, even with such a nice tutorial, the dev won't be able to escape from:

  • Managing the MediaPlayer life cycle, including setting the proper media source.
  • Creating a task every time you need an async reproduction.

Da Solution

So, what could we possibly have done to solve this? Think about it...yes, you almost have it...a bit more...YES, YOU GOT IT MAN!

We developed a simple wrapper to handle both issues that even leaves the media source input divided into three equally named methods through polymorphism.

This, is JustPlay: a simple sound player manager wrapper.

It currently supports three media sources:

  • RAW: Local stored raw files
  • URI: URI pointing to audio files, regardless of their origin.
  • URL: Directly stream a audio file and play it.

How-To

Whenever you need to play a sound, you will only have to call first the library (with the sound source in any of the formats), and then the library method 'thisAudio' with both the desired sound and app context as parameters.

Accessing the audio source

This step depends on the type of your source:

  • The RAW files, that you save in the res/raw folder, can be accessed through the R generated class in this way: R.raw.sound, returning the associated int.
  • The URLs can be sent just as a regular String parameter.
  • The internal URIs formatted in the URI class, such as one you might obtain from a Content Resolver. For example:
Uri.parse("android.resource" + "://" + 
getPackageName() + "/raw/sound");

Play the sound

As the library suggest, this step is a piece of cake:

JustPlay.thisAudio(soundSource, this);
    1. If you try to play the same sound repeatedly, or different files together, it will work as expected.
    1. It initiates a new task per sound.

This library gives you a simpler approach to the problematic, but at the same time you lose control over the reproduction.

And then...

That's all! Easy and short, ideal for those who want to play a few user event-related sounds in their app without adding complexity to the code... besides that, it's nice to read JustPlay.thisSound(source, context); inside your code!