problems with Ti.Media.Sound on Android

You must Login before you can answer or comment on any questions.

Hi,

It seems that I can choose among these alternatives:

Play different sounds, but lose playback at some point with no errors raised

OR

Play the same sound over and over - because you can change the URL on the sound object, but you can't change what file it plays! (this is probably why it doesn't run out of resources)

The following program demonstrates that:

var win = Ti.UI.createWindow({
    navBarHidden : true,
    exitOnClose : true,
    backgroundColor : 'white',
})
 
// Replace these with any sounds you have lying around
// Do NOT use mp3s - Ti fails miserably with mp3s on Android
var soundUrl = ['/51.ogg', '/50.ogg']
 
var counter1 = 0
var btn1 = Ti.UI.createLabel({
    text : 'new soundobject 0',
    backgroundColor : 'yellow',
    color : 'black',
    height : 50,
    top : 50,
})
 
 
// This way we can play different sounds
// But we'll run out of resources, and the sounds
// will stop playing. With the sounds i use, and my phone, this happens after 30-35 times 
// Ti KNOWS that something went wrong
// (the debug-log says so) but it does not raise an error
btn1.addEventListener('click', function(e) {
    var sound = Ti.Media.createSound({
        url : soundUrl[counter1 % 2]
    })
 
    sound.play()
 
    // This error is almost never triggered
    // Would have been nice if it triggered when the sound fails to play!
    sound.addEventListener('error', function() {
        alert(counter1)
    })
 
    btn1.text = 'new soundobject ' + ++counter1 + '\n' + sound.url
})
var counter2 = 0
var btn2 = Ti.UI.createLabel({
    text : 'same soundobject 0',
    backgroundColor : 'yellow',
    color : 'black',
    height : 50,
    top : 150,
})
 
var sound2 = Ti.Media.createSound({})
 
 
// This way we can keep on without running out of resources
// BUT we can't change the sound. We can change the URL property
// of the sound object, but the first URL we gave it, remains in effect.
btn2.addEventListener('click', function(e) {
    sound2.stop()
 
    // this does not work
    sound2.url = soundUrl[counter2 % 2]
 
    // Ti.Media.Sound does not have a setUrl method
    // the docs says it has
    // sound2.setUrl(soundUrl[counter2 % 2])
 
    sound2.play()
 
    sound2.addEventListener('error', function() {
        alert(counter2)
    })
 
    btn2.text = 'same soundobject ' + ++counter2 + '\n' + sound2.url
})
 
win.add(btn1)
win.add(btn2)
win.open()
This is all on android of course.

Summary:

  • No errors raised when errors happen

  • Sound-object does not have a setUrl method, which contradicts the documentation ( http://docs.appcelerator.com/titanium/2.1/index.html#!/api/Titanium.Media.Sound )

  • Impossible to assign new Url to soundobject using soundobject.url = 'xxx.ogg'

  • mp3 files causes even more errors and might terminate your program abruptly

Is this state of affairs likely to change any time soon?

— asked 10 months ago by Esben Maaløe
4 Comments
  • It would be so nice if the docs reflected what Ti does, not what we WISH that Ti can do sometime sooner or later in the future!

    — commented 10 months ago by Esben Maaløe

  • same problem i am also facing when i try to play different videos back to back.

    — commented 10 months ago by Ashutosh Chaturvedi

  • hey, i got the solution, if you still not got then i can post it here

    — commented 10 months ago by Ashutosh Chaturvedi

  • Show 1 more comment

1 Answer

I was facing problem in videos,replace the code for the audio accordingly for your case

var btn = Ti.UI.createButton();
win.add(btn);
btn.addEventListener('click',function(){
var Movie = Titanium.Media.createVideoPlayer({
        url:'titanium'+abc+'.mp4',
        backgroundColor:'#111',
        movieControlMode:Titanium.Media.VIDEO_SCALING_ASPECT_FIT,
    });
    Movie.addEventListener('complete',function(e){
        abc++;
        if(abc!=6)
            btn.fireEvent('click');
        else
            abc = 1;
        Movie.hide();
    });
    Movie.play();       
});

— answered 10 months ago by Ashutosh Chaturvedi
answer permalink
5 Comments
  • Oh you solved the problem of 'how to play a sequence of media back-to-back'

    My problem is:

    At some point sound simply just disappear because you've created too many sound objects.

    You can't reuse soundobjects unless you want them to play the same sound again, because the documented way of changing the URL of the sound, does not work.

    So it's two different problems really, but thanks for being helpful!

    — commented 10 months ago by Esben Maaløe

  • You told that sound simply just disappear because you've created too many sound objects.okk then you can destroy previous views by using remove and its listeners by using removeEventListener.

    Mark the answer if it helps you.

    — commented 10 months ago by Ashutosh Chaturvedi

  • Actually I did try that. In this case there is no need to remove the eventlistener since it doesn't have any references in it.

    But I tried null'ing out and deleting the sound on 'complete', and it does not help :/

    — commented 10 months ago by Esben Maaløe

  • Show 2 more comments

Your Answer

Think you can help? Login to answer this question!