Настройка Seek Bar с медиаплеером - PullRequest
0 голосов
/ 24 сентября 2018

Я пытаюсь добавить аудиофайл на панель поиска, который будет управляться с помощью панели поиска, без добавления каких-либо кнопок в мое приложение.Аудиофайл будет прикреплен к панели поиска, и при выполнении панели поиска громкость будет увеличиваться или уменьшаться.Ниже мой код, и мое приложение падает, снова и снова, я не знаю почему.Пожалуйста, помогите мне в этом.

class MainActivity : AppCompatActivity(){

   private var mediaPlayer  : MediaPlayer? = null
    private var runnable : Runnable? = null
    private var handler : Handler? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val start = findViewById<Button>(R.id.start)
        val seekbar = findViewById<SeekBar>(R.id.seekBar)

        mediaPlayer = MediaPlayer.create(this,R.raw.lowest_frequency)
        mediaPlayer!!.setOnPreparedListener { mediaPlayer ->
            seekbar.max = mediaPlayer.duration
            mediaPlayer.start()
            changeSeekBar()
        }
        seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                if (p2){
                    mediaPlayer!!.seekTo(p1)
                }
            }

        })

        start.setOnClickListener{
            startActivity(Intent(this,StartActivity::class.java))
        }


    }

    private fun changeSeekBar() {
        seekBar.progress = mediaPlayer!!.currentPosition
        if (mediaPlayer!!.isPlaying){
            runnable = Runnable{
                changeSeekBar()


        }
            handler!!.postDelayed(runnable,50)
            handler!!.postDelayed(runnable,0)
        }
    }
}  

Ниже моя ошибка logcat

09-24 10:49:58.508 15907-15929/? E/libmtp: [mtp_init_device_info : 0586] -->[mtp_init_device_info  586]Error opening file /data/misc/radio/ahrh,(fd<0) IMEI [
    000000000000000
    ]
09-24 10:49:58.543 15907-15931/? E/SQLiteLog: (1) Process .MtpApplication : Pid (15907) Uid (1000) Euid (1000) Gid (1000) Egid (1000)
    (1) osStat failed "/data/data/com.android.providers.settings/databases/settings.db" due to error (2)
    (1) osStat failed "/data/data/com.android.providers.settings/databases" due to error (2)
    (1) Stat of /data/data/com.android.providers.settings : st_mode(40700) st_uid(1000) st_gid(1000) st_ino(65726)
    (1) Stat of /data/data : st_mode(40771) st_uid(1000) st_gid(1000) st_ino(12)
    (1) Stat of /data : st_mode(40771) st_uid(1000) st_gid(1000) st_ino(2)
    (21) API call with unopened database connection pointer
    (21) misuse at line 119179 of [605907e73a]
09-24 10:51:30.576 6373-6373/? E/BufferItemConsumer: [unnamed-6373-935] Failed to release buffer: Unknown error -1 (1)
09-24 10:51:30.707 6373-6373/? E/BufferItemConsumer: [unnamed-6373-936] Failed to release buffer: Unknown error -1 (1)
09-24 10:51:34.498 16102-16102/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num3L.ttf
09-24 10:51:34.499 16102-16102/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num3T.ttf
09-24 10:51:34.499 16102-16102/? E/Typeface: Unable to load Family: samsung-sans-num3T:null
    Error mapping font file /system/fonts/SamsungSans-Num4L.ttf
09-24 10:51:34.499 16102-16102/? E/Typeface: Unable to load Family: samsung-sans-num4L:null
    Error mapping font file /system/fonts/SamsungSans-Num4T.ttf
09-24 10:51:34.499 16102-16102/? E/Typeface: Unable to load Family: samsung-sans-num4T:null
    Error mapping font file /system/fonts/SamsungSans-Num35.ttf
09-24 10:51:34.500 16102-16102/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num45.ttf
09-24 10:51:34.500 16102-16102/? E/Typeface: Unable to load Family: samsung-sans-num45:null
    Error mapping font file /system/fonts/SamsungSans-Num3Lv.ttf
09-24 10:51:34.500 16102-16102/? E/Typeface: Unable to load Family: samsung-sans-num3LV:null
    Error mapping font file /system/fonts/SamsungSans-Num3R.ttf
09-24 10:51:34.501 16102-16102/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num4Tv.ttf
09-24 10:51:34.502 16102-16102/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num4Lv.ttf
09-24 10:51:36.101 16231-16231/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num3L.ttf
09-24 10:51:36.102 16231-16231/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num3T.ttf
09-24 10:51:36.102 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num3T:null
    Error mapping font file /system/fonts/SamsungSans-Num4L.ttf
09-24 10:51:36.102 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num4L:null
    Error mapping font file /system/fonts/SamsungSans-Num4T.ttf
09-24 10:51:36.102 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num4T:null
    Error mapping font file /system/fonts/SamsungSans-Num35.ttf
09-24 10:51:36.103 16231-16231/? E/Typeface: Error mapping font file /system/fonts/SamsungSans-Num45.ttf
09-24 10:51:36.103 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num45:null
    Error mapping font file /system/fonts/SamsungSans-Num3Lv.ttf
09-24 10:51:36.103 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num3LV:null
    Error mapping font file /system/fonts/SamsungSans-Num3R.ttf
09-24 10:51:36.103 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num3R:null
    Error mapping font file /system/fonts/SamsungSans-Num4Tv.ttf
09-24 10:51:36.103 16231-16231/? E/Typeface: Unable to load Family: samsung-sans-num4Tv:null
    Error mapping font file /system/fonts/SamsungSans-Num4Lv.ttf
09-24 10:57:19.587 4405-4422/? E/zygote: Error encountered killing process cgroup uid 99472 pid 15122: No such file or directory

1 Ответ

0 голосов
/ 20 декабря 2018

Я думаю, что это поможет вам

public class MainActivity extends AppCompatActivity {
    MediaPlayer mp;
    SeekBar seekBar;
    Handler mHandler;
    Runnable runnable;

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

        mHandler=new Handler();

        seekBar=findViewById(R.id.seekBar);

        mp=MediaPlayer.create(MainActivity.this,R.raw.ed);
        final int Duration=mp.getDuration();
        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                seekBar.setMax(mp.getDuration());
                updateSeekBar();
                mp.start();
            }
        });


        runnable=new Runnable() {
    @Override
    public void run() {
    updateSeekBar();
    }
    };

//Seekbar Listener

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mp.seekTo(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }
//update method

    private void updateSeekBar() {
        seekBar.setProgress(mp.getCurrentPosition());
        mHandler.postDelayed(runnable, 100);

    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...