- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序包含一个mediaplayer,它在audio.xml中带有一个(播放/暂停)按钮,但是问题是我无法找到正确的代码来在页面被破坏之前停止MediaPlayer,并且因为该应用程序在我登录时崩溃音频页面(xml)并在不运行mediaplayer的情况下退出,这是我使用的代码:
public class audio extends Langue implements Runnable,
OnClickListener, OnSeekBarChangeListener {
private SeekBar seekBar;
private Button startMedia;
// private Button stopMedia;
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
startMedia = (Button) findViewById(R.id.button1);
//stopMedia = (Button) findViewById(R.id.button2);
startMedia.setOnClickListener(this);
//stopMedia.setOnClickListener(this);
seekBar.setOnSeekBarChangeListener(this);
seekBar.setEnabled(false);
}
public void run() {
int currentPosition = mp.getCurrentPosition();
int total = mp.getDuration();
while (mp != null && currentPosition < total) {
try {
Thread.sleep(1000);
currentPosition = mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekBar.setProgress(currentPosition);
}
}
public void onClick(View v) {
if (v.equals(startMedia)) {
if (mp == null) {
mp = MediaPlayer.create(getApplicationContext(), R.raw.espoir);
seekBar.setEnabled(true);
}
if (mp.isPlaying()) {
mp.pause();
startMedia.setText("Play");
} else {
mp.start();
startMedia.setText("Pause");
seekBar.setMax(mp.getDuration());
new Thread(this).start();
}
}
/* if (v.equals(stopMedia) && mp != null) {
if (mp.isPlaying() || mp.getDuration() > 0) {
mp.stop();
mp = null;
startMedia.setText("Play");
seekBar.setProgress(0);
}
}
*/
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
try {
if (mp.isPlaying() || mp != null) {
if (fromUser)
mp.seekTo(progress);
} else if (mp == null) {
Toast.makeText(getApplicationContext(), "Media is not running",
Toast.LENGTH_SHORT).show();
seekBar.setProgress(0);
}
} catch (Exception e) {
Log.e("seek bar", "" + e);
seekBar.setEnabled(false);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
// stop song
@Override
protected void onPause(){
super.onPause();
mp.stop();
finish();
}
}
最佳答案
在onPause()方法中执行类似的操作以防止崩溃
try{
mp.stop();
} catch(Exception e) {
}
finish();
关于java - 停止MediaPlayer的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36138258/
我在 logcat 中收到一条警告消息 W/MediaPlayer-JNI(16795): MediaPlayer finalized without being released 请告诉我我错在哪里
我有以下代码。 例如: if(player1 != null){ if(player1.isPlaying()){ //check if it playing //other
我在安卓系统工作。我正在创建一个正在运行音频文件的 mediaPlayer。我有 10 个按钮。我为每个按钮分配了不同的 url。因此,当我按下按钮 1 时,将播放与按钮 1 相关的 url 歌曲。然
我正在使用 MediaPlayer。我想制作在线流媒体广播。现在我有这个 mediaPlayer 对象(MediaPlayer 的),在 fragment 1 中说,当我在 fragment 1 上时
我使用了以下代码: mp = MediaPlayer.create(this, Uri.parse("file://"+filePath)); mp.start(); 这很好用。然后我想播放文件夹中的
我通过下载新文件来启动我的应用程序,如果有任何文件开始播放循环。然后我每 x 秒触发一个任务来寻找新媒体。调用任务后,视频将停止播放,并显示错误 MediaPlayer finalized witho
我的应用程序允许用户从多个音频流中进行选择,当用户点击其中一个播放按钮时,将调用以下代码: @Override public void onClick(View view) { mMediaP
我正在尝试在我的 ViewController 中播放视频。 Mediaplayer.framework 和所有 Headers 都在我的 Frameworks 文件夹中,包括 MediaPlayer
我正在使用 MediaPlayer 和 SurfaceView 播放本地视频文件。 SurfaceView 是 Activity 中的唯一控件,而我的视频文件是 QVGA 或其他。问题是视频被拉伸(s
我正在使用 JavaFX 的 MediaPlayer 播放 mp3 文件,getCurrentTime() 工作正常,但是当我在某个时候使用 seek() 方法时, getCurrentTime()
我将音量设置为 0.0,然后在 while 循环中一点一点地改变音量。然而,音量从 0.0 跳到 1.0?如何顺利改变音量?我试过了 public class EngineSound extends
我为“播放”和“暂停”按钮创建了两个 onClick 方法,但如果先点击“暂停”,然后点击“播放”按钮,则音乐不会播放。下面是代码 package com.example.myaudio; impor
我用 MediaPlayer 控件创建了一个绑定(bind)服务,它可以加载和播放音频。但碰巧的是,有时 mediaPlayer.prepare(); 速度不够快,并不总是准备好播放。 那么我怎样才能
我正在我的应用程序中播放从网络流式传输的 mp3 文件,一些 mp3 文件有奇怪的行为:mediaPlayer.getCurrentPosition() 大于 mediaPlayer.getDurat
我想制作广播播放器示例,但我有一些错误。我无法修复它。 我的源代码 String url = ""; //Shoutcast Radio URL mp = new MediaPlayer();
我知道有一些类似的问题,但没有一个回答我的问题。当我点击一个按钮时,媒体播放器被调用,并且它出现在日志中。 06-02 00:20:38.980 26035-26035/myapp.com.facad
我正在尝试从 URL 流式传输音频。该代码适用于其他 URL,但其中一个 URL 在 OnPrepared 方法中失败,返回此错误代码:(1, -2147483648)。我看过一些人说这是因为权限,但
我在我的 Android 应用程序中使用 MediaPlayer 来播放视频。当通过 mediaPlayer.pause() 方法暂停播放并调用 mediaPlayer.seekTo(msec) 时,
我的应用程序在 MediaPlayer 流媒体方面存在问题,特别是在 Nexus 5 上。我不确定这是 Nexus 5 还是 API 级别 19 导致的问题。基本上我的 MediaPlayer 准备好
不过,我正在使用 RingtoneManager 来获取 Ringtone。 此信息已包含在 this question and answer 中,但 Google 员工不太可能找到它,除非他们已经知
我是一名优秀的程序员,十分优秀!