Home i wanna make an application that play sound when clicked and the sounds don't start on top of each other
Reply: 0

i wanna make an application that play sound when clicked and the sounds don't start on top of each other

Ahmed A
1#
Ahmed A Published in 2017-12-04 14:08:04Z

I'm making an application that plays music from a folder inside my application. the way I set it up is by making two buttons the first is the name of the first music and the second is the second music. when the first button clicked the music one start and when the second button clicked the second music start on top of the first one I need to stop the other music before starting the second music, so for that I made a stop player method but it broke my application cuz whenever i clicked the button the application stops . here is my code: please help

package com.example.m.hkaynaudio;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
     MediaPlayer hk1;
     MediaPlayer hk2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    private void stopPlayers() {
        if (hk1.isPlaying()) hk1.release();
        if (hk2.isPlaying()) hk2.release();
        if (hk3.isPlaying()) hk3.release();

    }
    public void playhk1(View view) {

        if (hk1==null){

            stopPlayers();
            hk1 = MediaPlayer.create(this,R.raw.baraka);
            hk1.start();
        }
    }


    public void playhk2(View view) {

        if (hk2==null){
            stopPlayers();
            hk2 = MediaPlayer.create(this,R.raw.brigade);
            hk2.start();
        }
    }  
}



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.m.hkaynaudio.MainActivity">


    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="playhk1"
        android:text="1-Baraka"
        android:visibility="visible"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="64dp"
        android:layout_marginTop="17dp"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/button4"
        android:layout_alignEnd="@+id/button4" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="playhk2"
        android:text="2-brigade"
        android:visibility="visible"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="112dp"
        android:layout_marginTop="17dp"
        android:layout_below="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />




</RelativeLayout>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303741 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO