GitHub Issue | n/a |
Type | Bug |
Priority | Medium |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2012-12-12T22:50:59.000+0000 |
Affected Version/s | Release 1.7.2, Release 1.8.0 |
Fix Version/s | Release 3.0.2, Release 3.1.0, 2012 Sprint 25, 2012 Sprint 25 API |
Components | Android |
Labels | android, api, insight, module_filesystem, notificationManager, qe-testadded, sound, triage |
Reporter | Eduardo Gomez |
Assignee | Hieu Pham |
Created | 2011-09-09T10:54:20.000+0000 |
Updated | 2013-09-19T00:43:21.000+0000 |
Problem
If you add a sound to a notification the sound is not found.
http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Android.NotificationManager-module
Repro sequence
var win= Titanium.UI.createWindow();
var btn = Ti.UI.createButton({
title: 'Add Notification'
});
btn.addEventListener('click', function(e) {
var activity = Ti.Android.currentActivity();
var intent = Ti.Android.createIntent({
});
var pending = Ti.Android.createPendingIntent({
'activity' : activity,
'intent' : intent,
'type' : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
'flags' : 1073741824
});
var ts = new Date().getTime();
var notification = Ti.Android.createNotification({
contentIntent : pending,
contentTitle : 'Test',
contentText : 'test',
when : ts,
//icon: 'KS_nav_ui.png',TIMOB-4651
sound: '1.mp3',
defaults: Titanium.Android.NotificationManager.DEFAULT_VIBRATE
});
Ti.Android.NotificationManager.notify(1, notification);
});
win.add(btn);
// testing if the image exist
var lbl = Titanium.UI.createLabel({
text : ' ',
backgroundImage: 'KS_nav_ui.png',
top : 10,
left: 10
});
win.add(lbl);
win.open();
Console Output - With customized Sound
Sound file is located at resources folder, so property is being set as:
sound: '1.mp3'
{noformat}
I/StagefrightPlayer( 34): setDataSource('/android_asset/Resources/1.mp3')
E/MediaPlayer( 60): error (1, -2147483648)
W/NotificationService( 60): error loading sound for file:///android_asset/Resources/1.mp3
W/NotificationService( 60): java.io.IOException: Prepare failed.: status=0x1
W/NotificationService( 60): at android.media.MediaPlayer.prepare(Native Method)
W/NotificationService( 60): at com.android.server.NotificationPlayer$CreationAndCompletionThread.run(NotificationPlayer.java:90)
{noformat}
Tested on
Device: HTC Dream Android 2.2 OS version & Emulator: Android HGVA
Associated HD ticket
PRO -
http://appc.me/c/APP-923324
Additional info
Get default vibrate by setting permissions in Tiapp.xml
{noformat}
http://schemas.android.com/apk/res/android">
{noformat}
Work Around
To allow the app at this time to play the default sound and vibration as well:
defaults: Titanium.Android.NotificationManager.DEFAULT_ALL
To play default sound:
defaults: Titanium.Android.NotificationManager.DEFAULT_SOUND
To get phone vibration:
defaults: Titanium.Android.NotificationManager.DEFAULT_VIBRATE
I do not believe this is trivial. This is one of many holes in the Ti API and documentation. Every one of these is a huge time-waster for developers. If the API worked as documented, or the documentation reflected the status of the API, I wouldn't have wasted time trying to get it to work.
I'm with Jason. This is not trivial. I think the handling of sound is incorrect and should be fixed. I was unable to get it to play a sound from the resources folder directly, and I wasn't able to play a sound from the raw Android assets either (either by placing them in Resources/android/assets or platform/android/res/raw). I tried referring to them using Ti.App.Android.R.raw.filename, and it turns out we don't support R.raw. I'll file a separate bug for that. What I was able to do after a little experimentation was play a sound from the SD card. Files in resources are all compressed by the Android build process into a single archive, so I'm guessing that extracting the file in this way makes it playable. But since we can play a sound directly from Resources using Ti.Media.Sound, I'm betting we could handle this case correctly for Notification. Anyway, here's my workaround:
Then, when you create the notification, use:
I will open a doc bug to document this workaround, but the workaround shouldn't be required in the first place.
Possibly related to TIMOB-11036? Or it could be entirely specific to Notification.
Adding workaround to docs in: https://github.com/appcelerator/titanium_mobile/pull/3343 When we fix this bug, we need to include an update to the docs to describe how the sound can be specified.
Hi, I was able to make it work with no workaround, but with some 'rules'. It was quite easy, take a look at this commit on my 2_1_X fork branch: https://github.com/iamyellow/titanium_mobile/commit/dbd4ce7aa70e947c11c757bcaf8076b20c79b199#android The 'rule' now is you must provide the sound in: TI_PROJECT_ROOT/platform/android/res/raw/yay.mp3 Then, in Javascript just: var notification = Ti.Android.createNotification({ ... sound: 'yay' }); (notice there's no extension!) Hope it helps. ps: I don't know why, but there's no sound in emulator, BUT there IS in a real device, which is the point, isn't it?
Hi Jordi, Yes, the emulator doesn't support sound, so that's expected. Glad you found a way to make it work. I know we have existing apps that use audio from the SD card, so we can't fix it this way in the platform--it should accept both Android-style resource IDs _and_ Titanium resource URLs.
Testing Preparations: Download cricket.wav and put it in /platform/android/res/raw (you can also get this file in KitchenSink) Test 1:
- Click on button, you should hear sound Test 2:
1. Click on play, sound should play
PR: https://github.com/appcelerator/titanium_mobile/pull/3546
Added sound, cricket.wav is being played when it's clicked. Verified on: Android version : 2.3.6, 4.0.4 SDK version: 3.1.0.v20130123144204,3.0.2.v20130122172624 CLI version : 3.0.23 OS : MAC OSX 10.7.5 XCode : 4.5.1