Pages

Sunday, May 22, 2016

Playing Audio Files in Android

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/playButton"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="play" />

    <Button
        android:id="@+id/pauseButton"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/playButton"
        android:layout_below="@+id/playButton"
        android:layout_marginTop="34dp"
        android:text="pause" />

    <Button
        android:id="@+id/stopButton"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/pauseButton"
        android:layout_centerVertical="true"
        android:text="stop" />

</RelativeLayout>
MainActivity.java
package com.blogspot.kantapp.ratingbarexampleinandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    float ratingByUser;

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

        RatingBar ratinbar = (RatingBar) findViewById(R.id.ratingBar1);
        ratinbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating,
                                        boolean fromUser) {
                // TODO Auto-generated method stub
                ratingByUser = rating;

            }
        });
        Button displayRating = (Button) findViewById(R.id.showRating);
        displayRating.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(),
                        "rating is:" + ratingByUser, Toast.LENGTH_LONG).show();
            }
        });
    }

}

No comments:

Post a Comment