Play sound in android apps

456 views
Skip to first unread message

mattstep...@googlemail.com

unread,
Oct 13, 2010, 10:34:35 AM10/13/10
to phonegap
Hi,

I am new to developing apps for android and I am struggling to get a
sound to play in the app. The app works perfectly fine for iphone and
I am trying to get it to work for android. In the iphone verison the
code for playing a sound is:

warningsound = new Media('warning.wav');
warningsound.play();

As it is only 2 lines of code I imagine it can't be too difficult to
do for android. Any help would be greatly appreciated.

Thanks in advance.

Matt

Bryce Curtis

unread,
Oct 13, 2010, 3:10:03 PM10/13/10
to phonegap
You need the latest edge version of PhoneGap to play audio on
Android. The default directory for a file is the root of the sdcard.
You can specify a file bundled in the .apk using "/android_asset/www/
warning.wav" or specify a file over the network using "http://server/
warning.wav". Either .wav or .mp3 formats should work.

On Oct 13, 9:34 am, "mattstephenso...@googlemail.com"

mattstep...@googlemail.com

unread,
Oct 14, 2010, 7:25:35 AM10/14/10
to phonegap
Thanks for this Bryce but i have put this into my code and it isn't
working, i had to put in a permission for the vibrate function so do
you know if there is any permission or anything similar that is needed
to play sound?

Thanks

Raju Bitter

unread,
Oct 14, 2010, 9:27:38 AM10/14/10
to phonegap
I have the same problem:
http://groups.google.com/group/phonegap/browse_thread/thread/cba7aff686be0b24

On Thu, Oct 14, 2010 at 3:26 PM, Raju Bitter
<r.bitter.m...@googlemail.com> wrote:
> I have the same problem:
> http://groups.google.com/group/phonegap/browse_thread/thread/cba7aff686be0b24

>> --
>> You received this message because you are subscribed to the Google
>> Groups "phonegap" group.
>> To post to this group, send email to phon...@googlegroups.com
>> To unsubscribe from this group, send email to
>> phonegap+u...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/phonegap?hl=en?hl=en
>>
>> For more info on PhoneGap or to download the code go to www.phonegap.com
>

Bryce Curtis

unread,
Oct 14, 2010, 11:09:59 AM10/14/10
to phonegap
The permissions I have are:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission
android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission
android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" /
>
<uses-permission android:name="android.permission.WRITE_CONTACTS" /
>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />

They're not all needed for audio, but I haven't gone through to
determine which ones are.

On Oct 14, 8:27 am, Raju Bitter <r.bitter.mailingli...@googlemail.com>
wrote:
> I have the same problem:http://groups.google.com/group/phonegap/browse_thread/thread/cba7aff6...
>
> On Thu, Oct 14, 2010 at 3:26 PM, Raju Bitter
>
> <r.bitter.mailingli...@googlemail.com> wrote:
> > I have the same problem:
> >http://groups.google.com/group/phonegap/browse_thread/thread/cba7aff6...

Raju Bitter

unread,
Oct 14, 2010, 6:25:53 PM10/14/10
to Bryce Curtis, phonegap
Even if you don't give my app any permissions, I still can play an MP3
file with PhoneGap. But without the INTERNET permission

<uses-permission android:name="android.permission.INTERNET" />

I get the following exception:
D/PhoneGapLog( 3949): file:///android_asset/www/phonegap.js: Line 579
: JSCallback Error: Request failed.
I/Web Console( 3949): JSCallback Error: Request failed. at
file:///android_asset/www/phonegap.js:579
W/System.err( 3949): java.net.SocketException: Permission denied
W/System.err( 3949): at
org.apache.harmony.luni.platform.OSNetworkSystem.createServerStreamSocketImpl(Native
Method)
W/System.err( 3949): at
org.apache.harmony.luni.platform.OSNetworkSystem.createServerStreamSocket(OSNetworkSystem.java:170)
W/System.err( 3949): at
org.apache.harmony.luni.net.PlainServerSocketImpl.create(PlainServerSocketImpl.java:45)
W/System.err( 3949): at java.net.ServerSocket.<init>(ServerSocket.java:135)
W/System.err( 3949): at java.net.ServerSocket.<init>(ServerSocket.java:89)
I/System.out( 3949): PluginManager.exec(Accelerometer,
getAcceleration, Accelerometer21, [], true)
W/System.err( 3949): at
com.phonegap.CallbackServer.run(CallbackServer.java:107)
W/System.err( 3949): at java.lang.Thread.run(Thread.java:1096)

That means, you should at least set the INTERNET permission for your app.

Raju

Raju Bitter

unread,
Oct 14, 2010, 6:27:36 PM10/14/10
to Bryce Curtis, phonegap
To make it clear: Without ANY permission I can start audio playback,
despite the JSCallback Error being thrown. The version I'm using is
"platform":"Android","uuid":"200142d4dfdc1842","phonegap":"pre-0.92
EDGE","name":"passion_vf","version":"2.2"
Reply all
Reply to author
Forward
0 new messages