java - 如何设置音频与搜索栏播放

标签 java android xml audio seekbar

我在Java方面经验很少,希望您能帮助我。

我有一个类链接到包含3个播放,暂停和搜寻栏按钮的xml。
我在原始文件夹中有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; 


           }


}}

我哪里错了?
帮帮我,谢谢。

最佳答案

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);
    }
}

关于java - 如何设置音频与搜索栏播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21174238/

相关文章:

android - 如果应用程序由于内存不足而被系统杀死,AlarmManager 警报是否会持续存在?

java - 如何在android代码中创建MongoID?

java - OneLogin java-saml-tookit-jspsample 在解析响应时崩溃

java - fatal error - 序言中不允许内容

java - 如何使用 JavaFX 和 scenebulider 配置 IntelliJ IDEA?

java - 如何在 Spring 使用@NamedQuery CrudRepository @Query?

java - Java : Special type, bad operand type for binary operator

java - 你能放一个 <c :cout> tag inside the `value` attribute of a <c:set> tag?

java - 为 Android 应用程序开发设置服务器

c# - 如何使用动态元素名称反序列化 XML?