Reputation: 23
When I click on the html href link which is on my html file, that playing a song. but again I hit on the same link it starts playing the song without stopping previous activity. I canot stop the song by clicking twice on that link.
Please help me somebody, when i click on song link again and again, it playing multiple times simultaneously. I want stop the song and start it from beginning.
This is My 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);
}
}
This is My MyWebViewClient class
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;
}
}
This is my PlayTune Class
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);
}
}
And this is My AudioPlayer class. Although the Toast working, which is in the stopPlaying method, but the toast that within the if statement not working as well as stop and reset command.
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;
}
}
}
This is my 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>
Thsi is my Layout
<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>
Upvotes: 2
Views: 180
Reputation: 2611
You are creating new AudioPlayer instance in your PlayTune class and MyWebViewClient class each time AND creating a new PlayTune instance each time in MyWebViewClient. Each time that you do that you create a new player.
For PlayTune I'd change MediaPlayer player
to AudioPlayer player
. In the contructor I'd remove this.ctx=ctx;
and replace it with player = new AudioPlayer(ctx);
. Also, in Java classes begin with a Capital letter, so I'd change the class name from playTune
to PlayTune
. I'd also change your methods from:
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);
}
to
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();
}
For AudioPlayer
, inside the stopPlaying()
method, remove mp.pause();
or you will get an Exception.
For your MyWebViewClient
class you will need to change it to:
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;
}
}
That should fix the issue you are experiencing.
Upvotes: 0