如何使用搜索栏设置音频播放

时间:2014-01-16 22:28:07

标签: java android xml audio seekbar

我对java的经验很少,希望你帮助我。

我有一个链接到xml的类,其中包含3个播放,暂停和搜索栏按钮。 我在原始文件夹中有3个音频文件,我想让xml播放这些文件。

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class Recitation extends Activity implements OnClickListener  {

    Button play1, play2, play3, pause1, pause2, pause3, back;
    TextView title1, subject1, subject2, subject3;
    SeekBar seek1, seek2, seek3;


    MediaPlayer media1, media2, media3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recitation);


        getInit();
        seekUpdation(); 
    }





    public void getInit() {
        // TODO Auto-generated method stub
        seek1 = (SeekBar) findViewById(R.id.seek_bar1);
        seek2 = (SeekBar) findViewById(R.id.seek_bar2);
        seek3 = (SeekBar) findViewById(R.id.seek_bar3);
        play1 = (Button) findViewById(R.id.Bpl1);
        play2 = (Button) findViewById(R.id.Bpl2);
        play3 = (Button) findViewById(R.id.Bpl3);
        pause1 = (Button) findViewById(R.id.Bps1);
        pause2 = (Button) findViewById(R.id.Bps2);
        pause3 = (Button) findViewById(R.id.Bps3);
        title1 = (TextView) findViewById (R.id.tvRecitMain);
        subject1 = (TextView) findViewById (R.id.tvRec1);
        subject2 = (TextView) findViewById (R.id.tvRec2);
        subject3 = (TextView) findViewById (R.id.tvRec3);
        seek1.setMax(media1.getDuration());
        seek2.setMax(media2.getDuration());
        seek3.setMax(media3.getDuration());
        media1 = MediaPlayer.create(Recitation.this , R.raw.alnajm);
        media2 = MediaPlayer.create(Recitation.this , R.raw.alrahman);
        media3 = MediaPlayer.create(Recitation.this , R.raw.qaf);

    }

     Runnable run = new Runnable() {

            @Override
            public void run() {
                seekUpdation();
            }
        };


        public void seekUpdation() {
            // TODO Auto-generated method stub
            seek1.setProgress(media1.getCurrentPosition());
            seek2.setProgress(media2.getCurrentPosition());
            seek3.setProgress(media3.getCurrentPosition());


        }
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
            case R.id.Bpl1:
                media1.start();
                break;
            case R.id.Bps1:
                media1.pause();
                break;
            case R.id.Bpl2:
                media2.start();
                break;
            case R.id.Bps2:
                media2.pause();
                break; 
            case R.id.Bpl3:
                media3.start();
                break;
            case R.id.Bps3:
                media3.pause();
                break; 


           }


}}
那边我哪里错了? 帮助我,谢谢你。

1 个答案:

答案 0 :(得分:0)

activity_main.xml中

<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical" >

    <SeekBar android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100" />

    <TextView android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

package com.example.mediaplayer;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends Activity {


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

        final MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.music);
        mediaPlayer.start();

        final Handler handler = new Handler();

        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                int currentPosition = mediaPlayer.getCurrentPosition() / 1000;
                int duration = mediaPlayer.getDuration() / 1000;
                int progress = (currentPosition * 100) / duration;

                SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
                seekBar.setProgress(progress);

                TextView txt = (TextView) findViewById(R.id.progress);
                txt.setText(String.valueOf(progress) + "%");

                handler.postDelayed(this, 1000);
            }
        };

        handler.postDelayed(runnable, 1000);
    }
}