gpt4 book ai didi

Android MediaPlayer 问题

转载 作者:行者123 更新时间:2023-11-30 04:43:27 28 4
gpt4 key购买 nike

我正在触发 MediaPlayer 在单击按钮时播放声音。有时,播放器会播放整个声音,有时则不会。它总是在最后切断。我在这里阅读了一些线程,其中人们遇到了同样的问题,但没有一个建议有效。例如,有人说添加 mediaPlayer.onCompletionListener() 可以解决问题,但事实并非如此。

这是 MediaPlayer 的代码:

public View getView(int position, View convertView, ViewGroup parent) {



LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.vocab_row, parent, false);

ImageView playIcon = (ImageView) row
.findViewById(R.id.blueplay_icon);
TextView vocabWord = (TextView) row
.findViewById(R.id.vocab_text_word);
TextView vocabMeaning = (TextView) row
.findViewById(R.id.vocab_text_meaning);

vocabWord.setText(data.get(position).getKey());
vocabMeaning.setText(data.get(position).getDefinition());

final String fileName = "audio/" + data.get(position).getAudio();

// set the click listener for the play button
playIcon.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

final MediaPlayer player = new MediaPlayer();
AssetManager manager = SingleLesson.this.getAssets();
final AssetFileDescriptor descriptor;

try {

descriptor = manager.openFd(fileName);
long start = descriptor.getStartOffset();
long end = descriptor.getLength();

//reset player
if (player != null) {
player.reset();
}

player.setDataSource(descriptor.getFileDescriptor(),
start, end);

} catch (IOException e) {
Log.e("IO EXCEPTION: ", "while getting mp3 assets.");
e.printStackTrace();
}

// set volume
player.setVolume(100, 100);

try {
player.prepare();
} catch (IllegalStateException e) {
Log.e("ERROR: ", "media player, illegal state");
e.printStackTrace();
} catch (IOException e) {
Log.e("ERROR: ", "media player, IO exception");
e.printStackTrace();
}

player.setOnPreparedListener(new OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer inPlayer) {
player.start();
}
});

// called when the file is finished playing
player.setOnCompletionListener(new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer player) {
player.stop();
player.release();
}

});
}

这个问题发生在模拟器上,但不是我的 htc incredible。它也不会发生在我女朋友的 moto droid2 上,但声音在 moto droid2 上确实有“咔嗒声”。

最佳答案

模拟器有多旧?我已经看到模拟器文件在一段时间后损坏的问题。如果不是新的,请尝试创建一个。

关于Android MediaPlayer 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5540831/

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