我已经构建了一个从URL播放mp3的应用程序。问题是暂停,停止&简历无法正常工作。我尝试了所有代码并查看了每个相关问题。但没有任何作用。请为此提供解决方案,谢谢你
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
me
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SeekBar;
public class MainActivity extends Activity implements OnClickListener {
private SeekBar mBar;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.playOnline).setOnClickListener(this);
findViewById(R.id.playOnline2).setOnClickListener(this);
findViewById(R.id.pause).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.stop).setOnClickListener(this);
mBar = (SeekBar) findViewById(R.id.progress);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.playOnline:
PlayOnlineUrl();
break;
case R.id.playOnline2:
PlayOnlineUrl2();
break;
case R.id.pause:
if (mediaPlayer != null) {
pause = true;
mediaPlayer.pause();
}
break;
case R.id.button3:
if (mediaPlayer != null) {
pause = false;
mediaPlayer.start();
}
break;
case R.id.stop:
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer = null;
timer.cancel();
task.cancel();
}
break;
default:
break;
}
}
@Override
public void onBackPressed() {
if( mediaPlayer.isPlaying() ) {
mediaPlayer.stop();
}
finish();
}
private void PlayOnlineUrl2() {
String url = "http://dj-videos.us/Music/XclusiveSinGleTrack/320%20Kbps/November%202013/Yo%20Yo%20Honey%20Singh%20-%20Blue%20Eyes-[DJKANG.Com].mp3"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
// You can show progress dialog here untill it prepared to play
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// Now dismis progress dialog, Media palyer will start playing
mp.start();
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// dissmiss progress bar here. It will come here when
// MediaPlayer
// is not able to play file. You can show error message to user
return false;
}
});
}
public void onPrepared(MediaPlayer mp) {
// Now dismis progress dialog, Media palyer will start playing
mp.start();
int duration = mp.getDuration();
int period = duration / 1000;
task = new TimerTask() {
@Override
public void run() {
mBar.post(new Runnable() {
@Override
public void run() {
if (!pause) {
progresss++;
mBar.setProgress(progresss);
}
}
});
}
};
timer = new Timer();
timer.schedule(task, 0, period * 10);
}
private boolean pause = false;
int progresss = 0;
private TimerTask task;
private Timer timer = null;
private void PlayOnlineUrl() {
String url = "http://starmirchi.com/upload_file/Mp3%20Songs/3605/Yo!%20Yo!%20Honey%20Singh%20_%20Blue%20Eyes%20(DJ%20AKS%20Remix)-(StarMirchi.com).mp3"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
// You can show progress dialog here untill it prepared to play
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// Now dismis progress dialog, Media palyer will start playing
mp.start();
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// dissmiss progress bar here. It will come here when
// MediaPlayer
// is not able to play file. You can show error message to user
return false;
}
});
}
}
这是错误记录
01-15 23:27:18.257: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
01-15 23:27:18.267: E/SoundPool(381): error loading /system/media/audio/ui/KeypressSpacebar.ogg
01-15 23:27:18.267: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
01-15 23:27:18.267: E/SoundPool(381): error loading /system/media/audio/ui/KeypressDelete.ogg
01-15 23:27:18.267: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
01-15 23:27:18.277: E/SoundPool(381): error loading /system/media/audio/ui/KeypressReturn.ogg
01-15 23:27:18.277: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
01-15 23:27:18.277: E/SoundPool(381): error loading /system/media/audio/ui/KeypressInvalid.ogg
01-15 23:27:18.277: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
01-15 23:27:18.277: W/AudioService(381): onLoadSoundEffects(), Error -1 while loading samples
01-15 23:27:18.367: E/AudioSink(52): received unknown event type: 1 inside CallbackWrapper !
01-15 23:27:20.097: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.097: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.107: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.107: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.127: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.127: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.137: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.137: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.137: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.197: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.197: E/SoundPool(381): error loading /system/media/audio/ui/KeypressStandard.ogg
01-15 23:27:20.207: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
01-15 23:27:20.207: E/SoundPool(381): error loading /system/media/audio/ui/KeypressSpacebar.ogg
01-15 23:27:20.207: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
01-15 23:27:20.207: E/SoundPool(381): error loading /system/media/audio/ui/KeypressDelete.ogg
01-15 23:27:20.207: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
01-15 23:27:20.217: E/SoundPool(381): error loading /system/media/audio/ui/KeypressReturn.ogg
01-15 23:27:20.217: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
01-15 23:27:20.217: E/SoundPool(381): error loading /system/media/audio/ui/KeypressInvalid.ogg
01-15 23:27:20.217: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
答案 0 :(得分:2)
如果我没有错,那么你只需要替换它:
findViewById(R.id.playOnline).setOnClickListener(this);
findViewById(R.id.playOnline2).setOnClickListener(this);
findViewById(R.id.pause).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.stop).setOnClickListener(this);
使用以下内容:全局定义所有按钮
Button Play1,Play2,Pause,button2,stop;
并在下面添加OnCreate()
:
Play1=(Button)findViewById(R.id.playOnline);
Play2=(Button)findViewById(R.id.playOnline2);
Pause=(Button)findViewById(R.id.pause);
button2=(Button)findViewById(R.id.button3);
stop=(Button)findViewById(R.id.stop);
Play1.setOnClickListener(this);
Play2.setOnClickListener(this);
Pause.setOnClickListener(this);
button2.setOnClickListener(this);
stop.setOnClickListener(this);
试试这个。
<强>更新强>
以这种方式尝试按钮监听器:
Play1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Here your code
}
});
和所有按钮。
您的代码问题就在这里。您刚刚在全球范围内定义了MediaPlayer
对象,然后为什么在您的New MediaPlayer
方法中创建了另一个PlayOnlineUrl()
对象。你只需添加如下:
mediaPlayer = new MediaPlayer();