【问题标题】:Titanium.Media.AudioPlayer stop() method not actually stopping playback on Android 11Titanium.Media.AudioPlayer stop() 方法实际上并未在 Android 11 上停止播放
【发布时间】:2022-01-07 07:58:16
【问题描述】:

如标题所述,我在 Android 11 上使用 Titanium.Media.AudioPlayer 停止音频播放时遇到问题。我使用的是 Titanium SDK 10.1.0.GA。

它在 Android 版本 8 和 10(迄今为止我测试过的仅有的另外两个版本)上运行良好。

如果有帮助,这就是我初始化音频播放器(在 Alloy.Globals 中定义)的方式:

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

致电Alloy.Globals.player.stop(); 绝对没有任何作用。音频继续播放。

我应该怎么做才能让它在 Android 11 上运行?

编辑:这是我的播放器的输出,似乎甚至没有播放,这很奇怪:

[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] } 

【问题讨论】:

    标签: android titanium


    【解决方案1】:

    在 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();
    

    并且应该也可以在 Android 11 中使用。尝试检查 Alloy.Globals.player 是否确实包含某些内容并且不为空。您可以测试的另一件事:

    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)
    

    这样您就可以查看它是否适用于本地媒体播放器,以及是否在 5 秒后停止。

    使用全局backbone events 并使用它来启动/停止播放器也可能很好。这应该比将整个音频播放器放在全局空间中要好。

    【讨论】:

    • 谢谢,我已经更新了我的问题以包含我的播放器对象的输出。
    • "duration": 0, 看起来它没有加载文件。如果您在我的示例中使用该文件(外部文件),它将显示持续时间。
    • 奇怪。如果它没有加载文件,音频是如何播放的?
    • 抱歉,我将您的最后一次更改误读为“它现在甚至没有播放”。输出看起来像一个新玩家(第二个)或在它播放之前。不确定你把日志放在哪里
    猜你喜欢
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    相关资源
    最近更新 更多