当我单击我的 html 文件中的 html href 链接时,即播放歌曲。但是我再次点击了相同的链接,它开始播放歌曲而没有停止以前的活动。我无法通过单击该链接两次来停止这首歌。
请帮助我的人,当我一次又一次地点击歌曲链接时,它会同时播放多次。我想停止这首歌并从头开始。
这是我的主要活动
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);
}
}
这是我的 MyWebViewClient 类
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;
}
}
这是我的 PlayTune 课程
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);
}
}
这是我的 AudioPlayer 类。虽然在 stopPlaying 方法中的 Toast 可以工作,但是在 if 语句中的 toast 以及 stop 和 reset 命令都不起作用。
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;
}
}
}
这是我的 HTML - puretest.html
<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>
这是我的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/rl02"
android:animateLayoutChanges="true"
android:fitsSystemWindows="false"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:scrollbars="none" />
<RelativeLayout
android:id="@+id/rl02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#000000"
android:gravity="center" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center"
android:minHeight="30dp"
android:onClick="onClick"
android:singleLine="true"
android:text="STOP"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="12dp"
android:textStyle="bold" />
<SeekBar
android:id="@+id/SeekBarTestPlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/button1"
android:background="#000000"
android:fadingEdge="horizontal"
android:indeterminate="false"
android:minHeight="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:scrollbarStyle="outsideOverlay" />
</RelativeLayout>
</RelativeLayout>