具有列表视图和服务的Android媒体播放器

时间:2015-07-10 19:06:12

标签: java android

我已经实现了一个Android媒体播放器,点击歌曲文本视图将显示布局。显示播放暂停的布局..并点击播放停止效果很好。在这里,我正在考虑用户的角度来实现以下内容。

  1. 点击歌曲文本视图应该在列表视图中显示歌曲,在布局的底部,小控件应显示播放暂停并停止
  2. 即使打开其他应用程序也应该播放。
  3. 为了达到第2点,我使用了媒体播放器服务,而对于第1点,我使用了`ListView'但没有成功实施。下面是播放歌曲并直到结束的代码。从这一点开始,有人可以帮我实现列表视图歌曲服务吗?

    public class Songs extends Activity {
    
        private MediaPlayer mediaPlayer;
        public TextView songName, duration;
        private double timeElapsed = 0, finalTime = 0;
        private int forwardTime = 2000, backwardTime = 2000;
        private Handler durationHandler = new Handler();
        private SeekBar seekbar;
        private Context context;
        private int getCurrentPos=0;
        private int[] songs={R.raw.sample_song,R.raw.sleepaway};
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            context=getApplicationContext();
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mediaplayer);
            initializeViews();
        }
    
        public void initializeViews(){
            songName = (TextView) findViewById(R.id.songName);
            mediaPlayer = MediaPlayer.create(this, songs[getCurrentPos]);
            mediaPlayer.setLooping(true);
            finalTime = mediaPlayer.getDuration();
            duration = (TextView) findViewById(R.id.songDuration);
            seekbar = (SeekBar) findViewById(R.id.seekBar);
            songName.setText("Sample_Song.mp3");
            seekbar.setMax((int) finalTime);
            seekbar.setClickable(false);
        }
    
        // play mp3 song
        public void play(View view) {
            mediaPlayer.start();
            timeElapsed = mediaPlayer.getCurrentPosition();
            seekbar.setProgress((int) timeElapsed);
            durationHandler.postDelayed(updateSeekBarTime, 100);
        }
    
    
        private Runnable updateSeekBarTime = new Runnable() {
            public void run() {
                //get current position
                timeElapsed = mediaPlayer.getCurrentPosition();
                //set seekbar progress
                seekbar.setProgress((int) timeElapsed);
                //set time remaing
                double timeRemaining = finalTime - timeElapsed;
                //              duration.setText(String.format("%d min, %d sec", 12, TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - 12));
    
                //repeat yourself that again in 100 miliseconds
                durationHandler.postDelayed(this, 100);
            }
        };
    
        // pause mp3 song
        public void pause(View view) {
            mediaPlayer.pause();
        }
    
        // go forward at forwardTime seconds
        public void forward(View view) {
            //check if we can go forward at forwardTime seconds before song endes
            if ((timeElapsed + forwardTime) <= finalTime) {
                timeElapsed = timeElapsed + forwardTime;
    
                //seek to the exact second of the track
                mediaPlayer.seekTo((int) timeElapsed);
            }
        }
    
        // go backwards at backwardTime seconds
        public void rewind(View view) {
            //check if we can go back at backwardTime seconds after song starts
            if ((timeElapsed - backwardTime) > 0) {
                timeElapsed = timeElapsed - backwardTime;
    
                //seek to the exact second of the track
                mediaPlayer.seekTo((int) timeElapsed);
            }
        }
    
        // Go to next song
        public void next(View view) throws IOException{
            try {
    
                mediaPlayer.reset();
                mediaPlayer = MediaPlayer.create(this, songs[getSongPosition()]);
                mediaPlayer.start();
    
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
        private int getSongPosition(){
            if(getCurrentPos==songs.length-1){
                getCurrentPos=0;
            }else if(getCurrentPos==0){
                getCurrentPos=getCurrentPos+1;
            }else{
                getCurrentPos=getCurrentPos+1;
            }
            return getCurrentPos;
        }
    
    }
    

    布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="#333333"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >
    
        <TextView
            android:id="@+id/songName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="songName" />
    
        <ImageView
            android:id="@+id/mp3Image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
             android:padding="30dp"
            android:src="@drawable/music"
            android:background="#ffffff"
            android:layout_margin="30dp" />
    
        <TextView
            android:id="@+id/songDuration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="songDuration" />
    
        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="30dp"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >
    
            <ImageButton
                android:id="@+id/media_rew"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="7dp"
                android:onClick="rewind"
                android:src="@android:drawable/ic_media_rew" />
    
            <ImageButton
                android:id="@+id/media_pause"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="7dp"
                android:onClick="pause"
                android:src="@android:drawable/ic_media_pause" />
    
            <ImageButton
                android:id="@+id/media_play"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="7dp"
                android:onClick="play"
                android:src="@android:drawable/ic_media_play" />
    
            <ImageButton
                android:id="@+id/media_ff"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="7dp"
                android:onClick="forward"
                android:src="@android:drawable/ic_media_ff" />
            <ImageButton
                android:id="@+id/media_next"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="7dp"
                android:onClick="next"
                android:src="@android:drawable/ic_media_next" />
        </LinearLayout>
    
    </LinearLayout>
    

0 个答案:

没有答案