Comparision of 4 approaches to playing audio in iOS

There are at least four different ways of playing audio in iOS.  Each has their own wrinkles and advantages. In this article I briefly compare and contrast some of the differences between the options.  For the TL;DR version skip to the table at the end which compares the approaches. (actually, to be honest, I’ve had this post in my drafts for a very long time, and just wanted to get it published. The body of the article isn’t as good as the table — skim the article and go to the table :))

  • MPMusicPlayerController(using the iPodMusicPlayer)
  • MPMusicPlayerController(using the applicationMusicPlayer)
  • AVAudioPlayer
  • AVPlayer

 

The MPMusicPlayerController using the iPodMusicPlayer is the highest level player, and matches closely to the iPod.  The actual iPod music player can be accessed and used with the factory method:

+[MPMusicPlayerController iPodMusicPlayer];

The nowPlaying item property will expose the current playing item in the iPod when using the iPodMusicPlayer. Changes to the current item in your application will also update the main iPod music player.

To load your own music player with the same API, use:

+[MPMusicPlayerController applicationMusicPlayer];

In iOS 5.1 and below it is not easily possible to use the AVAudioPlayer to play items from the iTunes library.  While it is possible to retrieve the url for the object, it doesn’t really do what you want. When trying this you will see a somewhat unhelpful error:

Domain = NSOSStatusErrorDomain
Code = -43
Description = Error Domain=NSOSStatusErrorDomain Code=-43 “The operation couldn’t be completed. (OSStatus error -43.)”

The AVPlayer however, allows access to the  iTunes library, playing in the background, custom background behaviour, and playing non-Library audio.

Of the accesible API’s this is the most functionality.  For the hard-core/determined developer, there are lower levels to dig down into, but these 4 options provide a decent set of options to play audio in iOS.  The table below outlines the highlights.

iosMediaPlayer applicationMusicPlayer AVAudioPlayer AVPlayer
play library Y Y N Y
access iPod play location Y N N N
Play in Background Y N ? Y
Custom Background behaviour N N/A ? Y
Play non-Library Audio N N Y Y

Options for getting JRuby 1.6 to use Ruby 1.9 Syntax

In 2012 and beyond you really want to be using Ruby 1.9 syntax (as per the standard ruby implementation KRI). JRuby 1.6 uses Ruby 1.8 syntax by default, but this can be changed to be 1.9. There are a bevy of different ways to do this. I’ll outline these here, and give you my recommendation on what to do.

Command Line parameter

The first way to set the ruby syntax version is to call ruby with a –1.9 parameter thusly:

This I don’t like at all because it doesn’t work with irb, and you have to type different things when using KRI and JRuby.

RUBYOPT Environment variable

A second option is to set the RUBYOPT environment variable to –1.9

This works for irb, but will actually prevent KRI from starting up as KRI doesn’t support the –1.9 parameter (KRI is Ruby 1.9 so the parameter doesn’t make sense).

JRUBY_OPTS Environment variable

Another option is to set the JRUBY_OPTS environment variable.

This works great for JRuby , irb in JRuby, and KRI.

.jrubyrc file

The next useful option is in a user specific .jrubyrc file. This gets called before JRuby starts up. Keep it in ~/.jrubyrc

This works well if you are working in an environment where you will always be running 1.9 compatible code. It’s what I currently do.

rvm

If you are using rvm there are a number of ways to do things.

First you can use a .rvmrc and set the JRUBY_OPTS variable, or a PROJECT_JRUBY_OPTS environment variable.

The second option is to enable rvm jruby hooks.

hooks live in $rvm_path/hooks.

In rvm 1.14.3 there are a series of after_use hooks, including some after_use_jruby hooks. Inspect these to see what they do (after_use runs all the other after_use hooks, and the after_use_jruby scripts will get some JRuby magic happening). You could put one of the earlier options here to help make JRuby work.

Conclusion

In conclusion my recommendations for getting JRuby 1.6 to use Ruby 1.9 syntax are:

  1. use a .jrubyrc file wherever possible
  2. use a .rvmrc for situations where you need different versions of JRuby.

Writing Beautiful RSpec Matchers

thoughtbot have created a really nice set of custom matchers for RSpec.  The Shoulda matchers make writing tests for rails models beautiful and clean.

Shoulda matchers make it possible to write specs of the form:

It’s easy to do this for yourself using the friendly matcher DSL that ships with RSpec. Let’s take a look at how.

In the simplest form, all you need to do is to call the define method, passing a name, and a block which in turn calls out to match. The simple case might be:

We’ve just defined a matcher :be_less_than which allows us to do the following:

If we use the describe auto subject feature of RSpec we can also write:

and

With that example we can see that we are getting close to the shoulda matcher syntax.  There’s still an extra level that helps make the shoulda matchers nice, chaining. Happily this is easy to do as well. The dsl provides a chain method.  The chain methods are called before the match, so you can use the chain calls to collect up additional information to validate.  Using our example above, we might want to add in an :and_greater_than chain.

This now will give us the ability to write:

Unfortunately the default descriptions don’t quite work as nice when there are chains in place. You’ll find that you’ll want to write a description in the matcher definition.

Unfortunately the description doesn’t automatically get used in the error messages when the matcher fails, so you’ll want to specify a failure_message_for_should. Basing it off the description is an ok starting point.

Happily RSpec does the right thing with the should not description, so with the details above the matcher will be good. There might be a case where you want a different failure_message_for_should_not, which can also be specified.

That covers how to write simple reusable RSpec matchers to make your test code look as beautiful as your production code. Go ahead and try it yourself.  I’d love to see comments or questions you might have on this.

REST API Design Rulebook By Mark Masse

I’ve been building web based APIs of various forms for a number of years now, interacting with SOAP and RESTful services, and building some myself. It was with some interest that I picked up the book REST API Design Rulebook, getting it from the O’Reilly blogger review program.

The book provides a decent set of information around REST, and had me either nodding my head or thinking at various stages. Its description and thoughts around REST were pretty good, but there is one area to be wary of. The book seems to do two things.

  1. Describe ways of doing REST well.
  2. Sell WRML, the authors own framework to help model REST

I found the first helpful and useful, but wasn’t as excited aboutthe second. While I understand the authors passion and desire to share what they’ve created, it ends up detracting significantly from the book, to the extent where I’d downgrade it from a must read to something that I’d hesitate to recommend.

The problem boils down to this. There are a number of challenges and interesting things to solve with REST APIs. The author has created a framework to help deal with these. Naturally then the author recommends using their framework to solve the problems. Which mean the book becomes Rest API design Rulebook with WRML. I’m not convinced that WRML is the answer, so I’d hesitate to recommend this book.

[This book was reviewed as a part of the O’Reilly Blogger Review Program]

Mobile Design Pattern Gallery By Theresa Neil

Patterns and pattern languages are a contentious topic among developers these days. Stemming from an overuse of them, and using fancy pattern names to excuse overly complex code, they are not the miracle cure they were thought to be 10 years ago. That said, having collections of things that work can be useful, and having names for things is very useful.

Mobile Design Pattern Gallery does a good job of outlining user interface patterns for mobile, giving labels to the elements, and helping developers like me think about what is in a design. I could imagine using the book to help produce a first cut of a mobile app in the lean MVP sense. Of course a UX designer will help produce a much improved experience, but the book gives enough examples to help get an idea for what could be done. In addition there are great links out to other resources and books to learn more.

I’d definitely recommend this book to developers and others who want to have a good set of examples and ideas for mobile user interfaces.

[This book was reviewed as a part of the O’Reilly Blogger Review Program]

Planning for Big Data

Planning for Big Data presents a series of short articles on working with Big Data. Big Data being the large datasets that are available today.

My first experiences with big data date back to last century, working on Large telecommunications datasets. In those days the ideas were to create star schemes and denormilised relational data models.

In the current world big data means working with huge datasets that are often unstructured.  The datasets being worked with will be a mix of proprietary in-house data, and publicly available data.  Working with these datasets will require tools that allow the unstructured data to be worked with, and can also handle the large volumes.  Planning for Big Data gives a good introduction to the current start of the art tools and techniques in a short easy to read series of articles.  It’s a great little overview for a technical or slightly technical person wanting to get a feel for the space without being bogged down in the details.

I’d recommend this for someone who is wanting to move into the world of Big Data.

[This book was reviewed as a part of the O’Reilly Blogger Review Program]

 

Backbone.JS on Rails

A while ago I made a post Two tricks for getting Backbone.js to play well with Ruby on Rails. It was good at the time, but probably not the place you really want to be reading.

For the best information go to quora, and read the answer to the question
How well does backbone.js work with rails? by Jeremy Ashkenas, the creator of Backbone.JS. It’s the best article on the topic, and deserves all the google juice it can get.

In particular the pick function approach mentioned is useful – and would have gone a decent way to preventing the github mass assignment vulnerability.

Thoughts on Prayer

Kristin’s recent “Drive-By Faith Healing” has lead to me thinking quite a lot about prayer, faith and healing. As a committed Christian I am convinced that God is real, and that prayer is effective as a means of communication with God. I have also experienced God’s answer to prayer, and am convinced that God can and will answer prayer. That said, I think we need to be careful in our attitude to prayer, and what our expectations are. We cannot just treat God as a genie and expect that he will do what we want just because we ask.

It is important to remember who God is and who I am. In particular considering how mighty and powerful God is, and how small in comparison I am. God is the originator of the complexity of our universe. He is the creator of all the sciences and mathematics that people can devote years to trying to understand. A PhD student will expect to end up being the expert in their specialisation – only knowing a sliver of the information that God created. God’s knowledge is so much more complete, and it is to this mighty, knowledgeable, and powerful God that I pray. Compared to God, my knowledge is negligible and my understanding is frighteningly limited. It is then to this mighty and powerful God that the Christian comes with prayers and petitions. A big reason that we pray to God is because of this power and wisdom. It only makes sense to ask for things from someone who is able to provide. If God isn’t strong enough or powerful enough to answer prayer, then what is the point?

With this as a background, it is amazingly presumptuous for us to demand things of such a powerful entity. God the creator of the Universe is present and in control. He is powerful and wise and knowing, and so can make much better decisions and work things out in ways that are beyond our understanding. Just as I cannot presume to understand everything that a PhD student has spent years learning, I cannot presume to understand everything that the powerful God does. I certainly need to be careful in demanding things from such a God.

The nuance in praying to the Christian God is that God also loves us, and wants what is best for us. We are told that God is a God of love, and that we are to bring before God our prayers and petitions. God will then answer them in the way that is best for us. Not necessarily how we want, but in the way that is best.

This brings us back to the topic of faith healing. God is certainly capable of healing people. There are clear cases where God has healed people in the past, but, God is God. We can’t expect God to do things just because that is what we want, or because they happened that way in the past. We have the privilege of asking God for things and communicating with God, but need to treat God with the deserved respect and honour. As we pray for healing we just need to remember that the answer that God gives is not always the answer that we want.

If God is the Christian God of far superior knowledge and boundless love, then when we pray for healing we can take great comfort in knowing that his answer, be it yes no or wait, really is the best answer.

Isaiah 45:9 “Woe to the one who quarrels with his Maker– An earthenware vessel among the vessels of earth! Will the clay say to the potter, ‘What are you doing?’

How to get OpenSSL in Ruby 1.9.3 working on OSX 10.7 (fixing the Segmentation Fault with Ruby OpenSSL)

update – this needs to have a recent version of RVM – it works with rvm 1.10 but not with 1.6 – do an rvm update first.

When using the mighty cobweb web crawler on my OSX 10.7 with Ruby 1.9.3 I was getting a seg fault in net http:

net/http.rb:799: [BUG] Segmentation fault

A bit of googling and isolation in IRB showed that this can be caused when hitting HTTPS urls using net http.

I saw a few different solutions to the problem, primarily talking about Ruby 1.9.2.Surprisingly it took me a while to end up at the RVM documentation about the problem: https://rvm.beginrescueend.com/packages/openssl/. This didn’t quite work out of the box for me, so I used the solution in the following gist.

and now I can happily crawl https urls.

It’s Not Luck – Eliyah Goldratt

Updated following feedback from Joel-Henry GROSSARD in the comments below.

It’s Not Luck is a sequel to Eli’s first book The Goal, and follows the same basic strategy, following the career of the protagonist, Rogo, teaching us along the way the thinking and business principals that Eli sees as key.

I really enjoyed the book. Teaching through parables and stories is a great technique, and well followed in this book.

The big idea being communicated was a process for thinking carefully and deeply about problems. In looking through this, we see it being applied primarily in a business context, and additionally in the personal life of Rogo. The different scenarios presented help us to see how general an approach it is, and that it is something that we can apply to al areas of our lives. In looking at the business applications we also see some of the principles of how Eli would run a company. In particular I found the following summary of a good businesses goal and the necessary conditions required to meet very helpful:

  • Make Money now as well as in the future,

  • Provide a secure and satisfying environment for employees now as well as in the future, and

  • Provide satisfaction to the market now as well as in the future.

The goal of making money for a business and the necessary conditions are required to have a business that thrives both now and is sustainable. The conditions are required to ensure that the company is profitable, that employees are happy, and that customers are being served. All of which are required to ensure that the company works well now and it is being done in a sustainable way.

While I found this succinct description of goals for a company helpful, the key of the book is the thinking process presented within. I highly recommend you go and read It’s Not Luck, you’ll learn and have fun while doing so.