当我点击我的html文件上的html href链接时,播放一首歌。但是我再次点击同一个链接开始播放歌曲而不停止之前的活动。我可以通过点击该链接两次来停止歌曲 请帮我一个人,当我一次又一次点击歌曲链接时,它会同时播放多次。我想要停止歌曲并从头开始。
这是我的主要活动
heres-the-problem
这是My MyWebViewClient类
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
public class MainActivity extends Activity {
WebView webView;
Button btnstart;
SeekBar seekbar;
RelativeLayout frame;
@SuppressLint({ "SetJavaScriptEnabled", "NewApi" })
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=(WebView)findViewById(R.id.webView1);
webView.setWebViewClient(new MyWebViewClient(this));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.loadUrl("file:///android_asset/puretest.html");
btnstart = (Button) findViewById(R.id.button1);
frame = (RelativeLayout) findViewById(R.id.rl02);
frame.setVisibility(View.GONE);
seekbar = (SeekBar)findViewById(R.id.SeekBarTestPlay);
}
@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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的PlayTune类
import android.content.Context;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebViewClient extends WebViewClient {
Context cxt;
public MyWebViewClient(Context cxt) {
// TODO Auto-generated constructor stub
this.cxt = cxt;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("tune")){
//view.loadUrl(url);
new AudioPlayer(cxt).stopPlaying();
new playTunes(cxt).tune1(url);
return true;
}
if(url.contains("stop")){
//view.loadUrl(url);
new AudioPlayer(cxt).stopPlaying();
return true;
}
if(url.contains("song")){
//view.loadUrl(url);
//new AudioPlayer(cxt).stopPlaying();
new playTunes(cxt).song1(url);
}
return true;
}
}
这是我的AudioPlayer类。虽然Toast工作,这是在stopPlaying方法,但是if语句内的toast不能正常工作以及stop和reset命令。
import android.content.Context;
import android.media.MediaPlayer;
public class playTunes {
Context cxt;
MediaPlayer player;
public playTunes(Context cxt) {
// TODO Auto-generated constructor stub
this.cxt = cxt;
}
public void tune1(String url) {
// TODO Auto-generated method stub
new AudioPlayer(cxt).stopPlaying();
int tuneUrl=R.raw.e0007;
new AudioPlayer(cxt).player(tuneUrl);
}
public void song1(String url) {
// TODO Auto-generated method stub
new AudioPlayer(cxt).stopPlaying();
int tuneUrl=R.raw.test;
new AudioPlayer(cxt).player(tuneUrl);
}
}
这是我的HTML - puretest.html
import java.io.IOException;
import android.content.Context;
import android.media.MediaPlayer;
import android.widget.Toast;
public class AudioPlayer {
MediaPlayer mplayer;
Context cxt;
public AudioPlayer(Context cxt) {
// TODO Auto-generated constructor stub
this.cxt = cxt;
}
public void player(int tuneUrl){
stopPlaying();
mplayer = MediaPlayer.create(cxt, tuneUrl);
try {
mplayer.prepare();
} catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mplayer.start();
}
public void stopPlaying() {
// TODO Auto-generated method stub
Toast.makeText(cxt, "working stop playing", Toast.LENGTH_SHORT).show();
if (mplayer != null) {
Toast.makeText(cxt, "it seems not working", Toast.LENGTH_LONG).show();
mplayer.stop();
mplayer.pause();
mplayer.release();
mplayer = null;
}
}
}
Thsi是我的布局
<html>
<head>
SELECT TUNE
</head>
<br><br><body>
<a href="tune">plays</a>
<a href="song"><br><br>Tune</a><form>
<a href="stop"><input type="" style="color: Black; background-color: Green"></a>
</form>
<form name="form1" method="post" action="">
testong
<a href="stop"><input type="submit" name="testing" id="testing" value="Submit" onClick="testing" ></a>
</form>
</body>
</html>
答案 0 :(得分:0)
每次在PlayTune类和MyWebViewClient类中创建新的AudioPlayer实例,并且每次在MyWebViewClient中创建一个新的PlayTune实例。每次你这样做,你都会创建一个新玩家。
对于PlayTune,我将MediaPlayer player
更改为AudioPlayer player
。在构造函数中,我删除this.ctx=ctx;
并将其替换为player = new AudioPlayer(ctx);
。此外,在Java类中以大写字母开头,因此我将类名从playTune
更改为PlayTune
。我也改变了你的方法:
public void tune1(String url) {
// TODO Auto-generated method stub
new AudioPlayer(cxt).stopPlaying();
int tuneUrl=R.raw.e0007;
new AudioPlayer(cxt).player(tuneUrl);
}
public void song1(String url) {
// TODO Auto-generated method stub
new AudioPlayer(cxt).stopPlaying();
int tuneUrl=R.raw.test;
new AudioPlayer(cxt).player(tuneUrl);
}
到
public void tune1(String url) {
// TODO Auto-generated method stub
int tuneUrl=R.raw.e0007;
player.stopPlaying();
player.player(tuneUrl);
}
public void song1(String url) {
// TODO Auto-generated method stub
int tuneUrl=R.raw.test;
player.stopPlaying();
player.player(tuneUrl);
}
public void stop() {
player.stopPlaying();
}
对于AudioPlayer
,在stopPlaying()
方法内删除mp.pause();
,否则您将获得例外。
对于MyWebViewClient
课程,您需要将其更改为:
public class MyWebViewClient extends WebViewClient {
PlayTune player;
public MyWebViewClient(Context cxt) {
// TODO Auto-generated constructor stub
player = new PlayTune(ctx);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("tune")){
//view.loadUrl(url);
player.tune1(url);
return true;
}
if (url.contains("stop")){
//view.loadUrl(url);
player.stop();
return true;
}
if (url.contains("song")){
//view.loadUrl(url);
player.song1(url);
}
return true;
}
}
这应该可以解决您遇到的问题。