求助Android进度条在音乐播放器中的使用方法

MainAvctivity.java

`
package com.example.shinelon.myapplication;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
MusicPlayService.MusicBinder musicBinder;
MusicServiceConnection musicServiceConnection;
EditText songNameEt;
//以下是获取权限的
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
“android.permission.READ_EXTERNAL_STORAGE”,
“android.permission.WRITE_EXTERNAL_STORAGE” };
//以上是获取权限的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_layout);
songNameEt = (EditText)findViewById(R.id.editText);
bindService();
verifyStoragePermissions(this);
//以上是获取权限的
}
//以下是获取权限的
public static void verifyStoragePermissions(Activity activity) {

    int permission = ActivityCompat.checkSelfPermission(activity,
            "android.permission.WRITE_EXTERNAL_STORAGE");
    if (permission != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
    }
}
//以上是获取权限的
public void bindService(){
    if(musicServiceConnection == null){
        musicServiceConnection = new MusicServiceConnection();
    }
    Intent intent = new Intent(this,MusicPlayService.class);
    bindService(intent, musicServiceConnection, BIND_AUTO_CREATE);
}
public void unbindService(){
    if(musicServiceConnection != null){
        unbindService(musicServiceConnection);
        musicServiceConnection = null;
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unbindService();
}

public void control_play(View view) throws IOException {
    if(musicBinder.call_getMediaPlayer().isPlaying()){
        musicBinder.call_pause();
    }
    else if (!musicBinder.call_getMediaPlayer().isPlaying()){
        musicBinder.call_play();
    }
}

public void nextSong(View view) throws IOException {
    musicBinder.call_playNext();
    String musicName = musicBinder.call_getCurrMusicName();
    songNameEt.setText(musicName);
}
public void lastSong(View view) throws IOException {
    musicBinder.call_playPrevious();
    String musicName = musicBinder.call_getCurrMusicName();
    songNameEt.setText(musicName);
}
class MusicServiceConnection implements ServiceConnection{

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        musicBinder = (MusicPlayService.MusicBinder)service;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
}

}
`

MusicPlayService.java

package com.example.shinelon.myapplication;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import java.io.IOException;
import java.util.Map;

public class MusicPlayService extends Service {
    MediaPlayer mediaPlayer;
    //以下验证是否能读取到SD卡文件
    SongList songList;
    String currMusicName = "";
    int curIndex = 0;
    public MusicPlayService() {
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.v("MusicService", "音乐服务已经绑定");
        return new MusicBinder();
    }
    @Override
    public boolean onUnbind(Intent intent) {
        Log.v("MusicService", "音乐服务已经解除绑定");
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v("MusicService", "音乐服务已经被创建");
        //以下验证是否能读取到SD卡文件
        songList = new SongList();
        Log.v("MusiceService", "这个列表下的所有歌曲" + songList.getMusicList().get(0).toString());
//        mediaPlayer = MediaPlayer.create(this,R.raw.a);
        mediaPlayer = new MediaPlayer();
    }
    class MusicBinder extends Binder{
        public void call_play() throws IOException {
            playMusic(curIndex);
        }
        public void call_playNext() throws IOException {
            playNext();
        }
        public void call_playPrevious() throws IOException {
            playPrevious();
        }
        public String call_getCurrMusicName(){
            return getCurrMusicName();
        }
        public void call_pause(){
            pause();
        }
        public MediaPlayer call_getMediaPlayer(){
            return getMediaPlayer();
        }
    }
    public void playMusic(int musicPo) throws IOException {
        /*if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
        }
        Log.v("MusicService","音乐已经开始播放");//代表的就是音乐控制播放的相关代码*/
        Map<String,Object> currMusic = songList.getMusicList().get(musicPo);
        String musicUrl = (String)currMusic.get("musicAbPath");
        currMusicName = (String)currMusic.get("musicName");
        Log.v("Music","----音乐路径--"+musicUrl);
        Log.v("Music","----音乐名称--"+currMusicName);
        mediaPlayer.reset();
        mediaPlayer.setDataSource(musicUrl);
        mediaPlayer.prepare();
        mediaPlayer.start();
        curIndex = musicPo;
    }
    public void playNext( ) throws IOException {
        int newIndex = 0;
        newIndex = curIndex + 1;
        if (newIndex>songList.getMusicList().size()-1){
            newIndex = 0;
        }
        playMusic(newIndex);
        curIndex = newIndex;
    }
    public void playPrevious() throws IOException {
        int newIndex = 0;
        newIndex = curIndex - 1;
        if (newIndex<0){
            newIndex = songList.getMusicList().size()-1;
        }
        playMusic(newIndex);
        curIndex = newIndex;
    }
    public void pause(){
        if(mediaPlayer.isPlaying()){
            mediaPlayer.pause();
        }
        Log.v("MusicService","音乐已经开始暂停");//控制音乐暂停的相关代码
    }
    public String getCurrMusicName(){
        return currMusicName;
    }
    public MediaPlayer getMediaPlayer(){
        return mediaPlayer;
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:text="music/a.map3"/>

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/musicSeekBar"
        android:padding="10dp"
        android:indeterminate="false" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="上一首"
            android:id="@+id/btn_before"
            android:onClick="lastSong" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放/暂停"
            android:id="@+id/btn_play"
            android:onClick="control_play" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一首"
            android:id="@+id/btn_next"
            android:onClick="nextSong" />

    </LinearLayout>
</LinearLayout>

本人刚接触Android不久 ,很多地方不懂,我也参考了很多资料,可是都不太明白,也尝试了很久都没有出结果,恳求各位大佬以上面代码为例,写一下怎么用进度条实现控制歌曲进度,在此,说声谢谢。

发布了1 篇原创文章 · 获赞 0 · 访问量 80
展开阅读全文

android 视屏播放器 全屏显示进度条 并自动隐藏的问题

11-21

编写了一个播放器,全屏播放,触摸屏幕,显示下方的进度条和控制按钮(默认情况下自动隐藏)这个已经编写成功了。如图 ![图片说明](https://img-ask.csdn.net/upload/201611/21/1479732903_449209.jpg) 但是我现在也想弄一个标题栏显示播放的视频名字,默认情况下隐藏,触摸屏幕时和下方的进度条等一起显示出来,然后出现问题了,上方的标题栏触摸屏幕显示不出来。 部分代码如下 ``` //这个是我上方的标题栏 <LinearLayout android:id="@+id/ll_theme" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:orientation="horizontal" android:visibility="invisible"> <TextView android:id="@+id/tv_theme" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文件标题"/> </LinearLayout> <SurfaceView android:id="@+id/sv_player" android:layout_width="wrap_content" android:layout_height="wrap_content"/> //下方的进度条等 <LinearLayout android:id="@+id/ll_ctrl" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center_vertical" android:orientation="horizontal" android:visibility="invisible"> <ImageButton android:id="@+id/ib_play" android:layout_width="45dp" android:layout_height="45dp" android:onClick="play" android:src="@drawable/ic_action_playback_pause"/> <ImageButton android:id="@+id/ib_forw" android:layout_width="45dp" android:layout_height="45dp" android:onClick="forw" android:src="@drawable/ic_action_playback_forw"/> <SeekBar android:id="@+id/sb" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:max="255"/> </LinearLayout> ``` ```触摸事件 public boolean onTouchEvent(MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_DOWN) { ll_ctrl.setVisibility(View.VISIBLE); ll_theme.setVisibility(View.VISIBLE); new Thread() { public void run() { SystemClock.sleep(3000); runOnUiThread(new Runnable() { @Override public void run() { ll_ctrl.setVisibility(View.INVISIBLE); ll_theme.setVisibility(View.INVISIBLE); } }); } }.start(); } return super.onTouchEvent(event); } ``` 现在怀疑是布局文件出现错误。。请各位大神指点。。。 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览