mình viết 1 chương trình nghe nhạc mini, có làm cái phần seekbar nhưng bị lỗi mong cả nhà sửa giúp:
đây là layout:

Mã:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <TextView
        android:id="@+id/text_shown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120px"
        android:text=""
        android:textSize="42px" />
 
    <SeekBar
        android:id="@+id/seek_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
 
    <Button
        android:id="@+id/play_button"
        android:layout_width="120px"
        android:layout_height="60px"
        android:layout_below="@+id/seek_bar"
        android:layout_marginTop="60px"
        android:gravity="center"
        android:text="Play" />
 
    <Button
        android:id="@+id/pause_button"
       android:layout_width="120px"
        android:layout_height="60px"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/seek_bar"
        android:layout_marginTop="60px"
        android:text="Pause" />
 
</RelativeLayout>
đây là chương trình:
Mã:
package com.nkm.mediaplayback;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
 
public class MediaPlaybackActivity extends Activity implements OnClickListener {
    SeekBar seek_bar;
    Button play_button, pause_button;
    MediaPlayer player;
    TextView text_shown;
    Handler seekHandler = new Handler();
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        getInit();
        seekUpdation();
    }
 
    public void getInit() {
        seek_bar = (SeekBar) findViewById(R.id.seek_bar);
        play_button = (Button) findViewById(R.id.play_button);
        pause_button = (Button) findViewById(R.id.pause_button);
        text_shown = (TextView) findViewById(R.id.text_shown);
        play_button.setOnClickListener(this);
        pause_button.setOnClickListener(this);
        player = MediaPlayer.create(this, R.raw.a);
        seek_bar.setMax(player.getDuration());
 
    }
 
    Runnable run = new Runnable() {
 
        @Override
        public void run() {
            seekUpdation();
        }
    };
 
    public void seekUpdation() {
 
        seek_bar.setProgress(player.getCurrentPosition());
        seekHandler.postDelayed(run, 1000);
    }
 
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.play_button:
            text_shown.setText("Playing...");
            player.start();
            break;
        case R.id.pause_button:
            player.pause();
            text_shown.setText("Paused...");
        }
 
    }}


Chủ đề tương tự: