gpt4 book ai didi

java - 错误 "E/MediaPlayer: Should have subtitle controller already set"导致没有音频播放

转载 作者:行者123 更新时间:2023-11-30 01:43:03 25 4
gpt4 key购买 nike

我已经阅读了这两个帖子:

Should have subtitle controller already set Mediaplayer error Android

这个讨论忽略和抑制错误。不能解决我的问题。

还有这个:

MediaPlayer : Should have subtitle controller already set: KitKat

没有有效答案。

我遇到的问题是:以前可以播放音频,但我们不得不更改我们的框架(我们删除了 CrossWalk),现在音频不再播放。事实是,删除 CrossWalk 与我的媒体播放器代码无关。

所以我得到的错误连续发生了两次。在我的代码中,我这样做:

        _activeMediaPlayer.prepare();
_activeMediaPlayer.start();

当调用这两行中的任何一行时,它会抛出错误:

E/MediaPlayer: Should have subtitle controller already set

设置流类型不会以任何方式影响这一点。此外,它不会抛出异常,但也不会触发 OnCompletionListener

      _activeMediaPlayer.setOnCompletionListener
(
new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
_activeMediaPlayer = null;
Log.d("AudioPlayer", "Completed audio playing");
}
}
);

您知道音频可能无法播放的原因吗? (100% 可以确认电话音频正常工作并且音量开到最大)。这段代码曾经有效!

更新这是我的整个音频播放课。这曾经 完美地工作。它播放的音频文件没有改变路径或格式。它实际上只是停止工作,没有抛出任何错误。

public class AudioPlayer
{
//region Variables

private static final String AUDIO_TYPE_DOWNLOADED = "downloaded";
private static final String AUDIO_TYPE_BUNDLED = "bundled";
private static final String AUDIO_BUNDLED_PATH = "www/audio/";
private static final String AUDIO_DOWNLOADED_PATH = "file:///data/data/co.app.app/Documents/";

private static AudioManager _audioManager;

private static AudioPlayer _singleton;

private static Activity _activity;

private static ArrayList<MediaPlayer> _audioList;

private static MediaPlayer _activeMediaPlayer = null;
//endregion

//region Initialization

/*
* Must be called before using Singleton. Will initialize with activity.
*/
public static void initialize(Activity activity)
{
_activity = activity;

_singleton = new AudioPlayer();

_audioList = new ArrayList<>();

_audioManager = SINGLETON().createAudioManager();
}

/**
* Access this to play audio.
*/
public static AudioPlayer SINGLETON()
{
if (_singleton == null)
{
System.out.println("Error audio helper not initialized, call Initialize first");
return null;
}

return _singleton;
}

/**
* Creates an audio manager.
*/
private AudioManager createAudioManager()
{
final AudioManager audioManager =
(AudioManager) _activity.getBaseContext().getSystemService(Context.AUDIO_SERVICE);
return audioManager;
}
//endregion

//region Audio Focus

/**
* Listener for audio focus changes.
*/
private AudioManager.OnAudioFocusChangeListener createAudioFocusChangeListener()
{
return new AudioManager.OnAudioFocusChangeListener()
{
public void onAudioFocusChange(int focusChange)
{
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
{
//TODO leave in case features are desired here
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN)
{
//TODO leave in case features are desired here
}
}
};
}

/*
* Sets audio focus to duck others.
*/
private int requestAudioFocus(AudioManager audioManager,
AudioManager.OnAudioFocusChangeListener audioFocusListener)
{
// Use the music stream and request permanent focus.
return audioManager.requestAudioFocus
(
audioFocusListener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
);
}
//endregion

//region Playing & Queueing Audio

/*
* Enqueue audio for play.
*/
private static void enqueueAudio(String path, String audioType)
{
MediaPlayer mediaPlayer = new MediaPlayer();

if (audioType.equals(AUDIO_TYPE_BUNDLED))
{
try
{
AssetFileDescriptor afd =
_activity.getAssets().openFd(AUDIO_BUNDLED_PATH + path + ".m4a");
mediaPlayer.setDataSource
(
afd.getFileDescriptor(),
afd.getStartOffset(),
afd.getLength()
);
} catch (IOException e)
{
System.out.println("Error bundled file not found ");
e.printStackTrace();
return;
}
} else
{
Uri uri = Uri.parse(AUDIO_DOWNLOADED_PATH + path + ".m4a");
try
{
mediaPlayer.setDataSource(_activity,uri);
} catch (IOException e)
{
e.printStackTrace();
System.out.println("Could not find audio at " + path);
return;
}
}

_audioList.add(mediaPlayer);
}

/**
* Enqueues all audio then plays it.
* Example format: String json = "{\"audio\":[{\"path\":\"a\",\"type\":\"bundled\"},
* {\"path\":\"444-1438314705229\",\"type\":\"downloaded\"}]}";
*/
public static void playAudio(String json)
{
try
{
JSONObject rootJson = new JSONObject(json);
JSONArray arrayJson = rootJson.getJSONArray("audio");

for (int i = 0; i < arrayJson.length(); i++)
{
JSONObject audioJson = (JSONObject) arrayJson.get(i);
enqueueAudio(audioJson.getString("path"), audioJson.getString("type"));
}
} catch (Exception e)
{
System.err.println("JSON parsing error " + e.getMessage());

if (Bridge.audioCallback != null)
{
Bridge.audioCallback.error(e.getMessage());
}
}

playQueuedAudio();
}

/**
* Plays the top most queued audio (is called by the onComplete listener)
*/
private static void playQueuedAudio()
{
final AudioManager.OnAudioFocusChangeListener audioFocusListener =
SINGLETON().createAudioFocusChangeListener();

int result = SINGLETON().requestAudioFocus(_audioManager, audioFocusListener);

if (_audioList.isEmpty())
{
_audioManager.abandonAudioFocus(audioFocusListener);

if (Bridge.audioCallback != null)
{
Bridge.audioCallback.success();
}

return;
}

if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
{
System.err.println("Could not gain audio focus, not playing audio");
return;
}

try
{
_activeMediaPlayer = _audioList.get(0);

_audioList.remove(0);

_activeMediaPlayer.prepare();
_activeMediaPlayer.start();
_activeMediaPlayer.setOnCompletionListener
(
new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
_activeMediaPlayer = null;
playQueuedAudio();
}
}
);
} catch (IOException e)
{
String message = "Unable to open media file: " + e.getMessage();
System.err.println(message);

if(Bridge.audioCallback != null)
{
Bridge.audioCallback.error(message);
}
}

}

/**
* Stops audio.
*/
public static void stopAudio(CallbackContext callbackContext)
{
if (_activeMediaPlayer != null && _activeMediaPlayer.isPlaying())
{
_activeMediaPlayer.stop();
}

_audioList.clear();

_activeMediaPlayer = null;

callbackContext.success();
}
//endregion
}

最后一点

作为测试,我创建了一个测试 Activity ,并在应用安装后立即将其写入其中:

MediaPlayer mediaPlayer = MediaPlayer.create(context,R.raw.test);
mediaPlayer.start();

这也行不通。这里有事情发生......

最佳答案

经过大量研究,我得出结论,用于对音频文件进行编码的编解码器已损坏。 m4a 适用于 Android,但似乎某些编解码器无法正常工作(据我所知, header 部分可能会弄乱)。

因此,我将所有内容都转换为 mp3,现在可以正常工作了。

关于java - 错误 "E/MediaPlayer: Should have subtitle controller already set"导致没有音频播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34168739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com