私のhtmlファイルにあるhtml hrefリンクをクリックすると、曲が再生されます。しかし、もう一度同じリンクをクリックすると、以前のアクティビティを停止せずに曲の再生が開始されます。そのリンクを 2 回クリックしても曲を止めることはできません。
誰か助けてください。曲のリンクを何度もクリックすると、同時に複数回再生されます。曲を止めて最初からやり直したい。
これは私の MainActivity です
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);
}
}
これが My AudioPlayer クラスです。stopPlaying メソッド内の Toast は機能しますが、if ステートメント内の Toast は停止およびリセット コマンドと同様に機能しません。
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>