Titanium.Media.AudioPlayer stop() method not actually stopping playback on Android 11

44 Views Asked by At

As the title describes, I'm having issues stopping audio playback with Titanium.Media.AudioPlayer on Android 11. I am using Titanium SDK 10.1.0.GA.

It works just fine on Android versions 8 and 10 (the only two other versions I've tested thus far).

If it helps, this is how I initialize my audioplayer (which is defined in Alloy.Globals):

Alloy.Globals.player = Ti.Media.createAudioPlayer({
  url: audioPath,
  allowBackground: true,
  audioFocus: true,
  audioType: Ti.Media.Sound.AUDIO_TYPE_MEDIA
});

Calling Alloy.Globals.player.stop(); does absolutely nothing. The audio just continues to play.

What should I be doing to get this to work on Android 11?

EDIT: Here is the output of my player, seems to not even be playing, which is odd:

[DEBUG] PLAYER: :{
[DEBUG] "volume": 1,
[DEBUG] "url": "file:///android_asset/Resources/audio/4.mp3",
[DEBUG] "muted": false,
[DEBUG] "time": 0,
[DEBUG] "audioSessionId": 0,
[DEBUG] "playing": false,
[DEBUG] "audioType": 0,
[DEBUG] "paused": false,
[DEBUG] "duration": 0,
[DEBUG] "apiName": "Ti.Media.AudioPlayer",
[DEBUG] "bubbleParent": true,
[DEBUG] "allowBackground": true,
[DEBUG] "audioFocus": true,
[DEBUG] "_events": {
[DEBUG] "complete": {}
[DEBUG] } 
1

There are 1 best solutions below

4
miga On

Working fine in Android 12 (Pixel 4, Titanium 10.1.1.GA).

alloy.js

Alloy.Globals.player = Ti.Media.createAudioPlayer({
  url: 'https://download.samplelib.com/mp3/sample-15s.mp3',
  allowBackground: true,
  audioFocus: true,
  audioType: Ti.Media.Sound.AUDIO_TYPE_MEDIA
});

index.js

var win = Ti.UI.createWindow({});
var btn = Ti.UI.createButton({})
btn.addEventListener("click", function() {
    Alloy.Globals.player.stop();
})
win.add(btn);
win.addEventListener("open",function(e){
    Alloy.Globals.player.play();
})
win.open();

And should also work in Android 11. Try to check if Alloy.Globals.player actually contains something and is not empty. Another thing you can test:

var mediaPlayer = Ti.Media.createAudioPlayer({
  url: 'https://download.samplelib.com/mp3/sample-15s.mp3',
  allowBackground: true,
  audioFocus: true,
  audioType: Ti.Media.Sound.AUDIO_TYPE_MEDIA
});

Alloy.Globals.player = mediaPlayer

setTimeout(function(){
  mediaPlayer.stop();
}, 5000)

so you can see if it works with a local media player and if it stops after 5 seconds.

It might also be good to use global backbone events and use that to start/stop the player. That should be better than having the whole audioplayer in the global space.