gpt4 book ai didi

Android MediaPlayer 仅播放 m3u8 音频流无效

转载 作者:行者123 更新时间:2023-11-29 01:39:18 24 4
gpt4 key购买 nike

大家好,我写了一个代码来播放调频 radio 波。在我的代码中有一个进度条和两个按钮,其中一个用于播放,另一个用于停止。当我点击播放按钮时,它不会发生。我怎么解决这个问题?或者有好的建议让我播放音频 m3u8 文件吗?
我使用的代码是:

    public class Main extends Activity implements OnClickListener {

private ProgressBar playSeekBar;

private Button buttonPlay;

private Button buttonStopPlay;

private MediaPlayer player;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

initializeUIElements();

initializeMediaPlayer();

}

private void initializeUIElements() {

playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);

buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);

buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);

}

public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}

private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);

playSeekBar.setVisibility(View.VISIBLE);

player.prepareAsync();

player.setOnPreparedListener(new OnPreparedListener() {

public void onPrepared(MediaPlayer mp) {
player.start();
}
});

}

private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}

buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}

private void initializeMediaPlayer() {
player = new MediaPlayer();
try {

player.setDataSource("http://url.m3u8");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}

@Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}

最佳答案

我通过 vitamio 解决了这个问题图书馆。谢谢

关于Android MediaPlayer 仅播放 m3u8 音频流无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25769737/

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