如何在所有活动中控制android中的媒体播放器?

时间:2019-04-14 08:01:34

标签: java android android-mediaplayer

朋友您好,我正在创建一个具有两个活动的mp3播放器,我如何从这两个活动(如播放暂停和进度条)中控制媒体, 当用户单击任何可正常运行的歌曲时,第一个活动具有该歌曲的列表,并在此歌曲列表视图下方,我创建进度,条和媒体控制按钮(例如播放暂停),然后在用户单击此列表时在列表下方单击并返回其包含有布局的广告布局它重定向另一个活动,并且还具有播放暂停和进度条之类的相同功能,如何从两个活动中控制此媒体? 我的代码!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#1e1e2c"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/song_list"/>



  <include
  android:layout_below="@+id/song_title"
      layout="@layout/mediacontroller"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />


</RelativeLayout>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#1e1e2c"
    android:layout_height="match_parent">


    <ImageView
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:id="@+id/rotate"
        android:layout_centerInParent="true"
        android:src="@drawable/musiccirlce"/>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true">

        <ImageButton
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:clickable="true"
            android:focusable="true"
            android:id="@+id/repeat"
            android:visibility="visible"
            android:scaleType="centerCrop"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:src="@drawable/baseline_repeat_white_48dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.023"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.811" />

        <ImageButton
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:clickable="true"
            android:focusable="true"
            android:id="@+id/repeatenable"
            android:visibility="gone"
            android:scaleType="centerCrop"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:src="@drawable/repeatenable"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.023"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.811" />


        <ImageButton
            android:id="@+id/shuffle"
            android:layout_width="20dp"
            android:layout_height="24dp"
            android:visibility="visible"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:scaleType="centerCrop"
            android:src="@drawable/baseline_shuffle_white_48dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.976"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.811" />


        <ImageButton
            android:layout_width="25dp"
            android:id="@+id/shufflenable"
            android:layout_height="25dp"
            android:visibility="gone"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:scaleType="centerCrop"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:src="@drawable/shuffulenabled"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.976"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.811" />
    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="99dp"
        android:layout_alignParentBottom="true">


        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:clickable="false"
            android:background="@color/backgroundColor"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0">

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageButton
                    android:id="@+id/imageButton2"
                    android:layout_width="55dp"
                    android:layout_height="55dp"
                    android:padding="15dp"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="16dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:scaleType="centerCrop"
                    android:src="@mipmap/outline_thumb_up_alt_black_48"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/songtime"
                    app:layout_constraintVertical_bias="0.56"
                    android:layout_marginRight="8dp"
                    android:layout_marginLeft="16dp" />
                <ImageButton
                    android:id="@+id/imageButton2new"
                    android:layout_width="55dp"
                    android:layout_height="55dp"
                    android:padding="15dp"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="16dp"
                    android:visibility="gone"
                    android:clickable="true"
                    android:focusable="true"
                    android:scaleType="centerCrop"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:src="@mipmap/baseline_thumb_up_alt_black_48"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/songtime"
                    app:layout_constraintVertical_bias="0.56"
                    android:layout_marginRight="8dp"
                    android:layout_marginLeft="16dp" />
                <ImageButton
                    android:id="@+id/button"
                    android:layout_width="55dp"
                    android:layout_height="55dp"
                    android:padding="15dp"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginStart="8dp"
                    android:scaleType="centerCrop"
                    android:clickable="true"
                    android:focusable="true"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:src="@mipmap/baseline_thumb_down_alt_black_18"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="1.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/endTime"
                    app:layout_constraintVertical_bias="0.56"
                    android:layout_marginRight="16dp"
                    android:layout_marginLeft="8dp" />
                <ImageButton
                    android:id="@+id/buttontwo"
                    android:layout_width="55dp"
                    android:layout_height="55dp"
                    android:padding="15dp"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginStart="8dp"
                    android:visibility="gone"
                    android:scaleType="centerCrop"
                    android:clickable="true"
                    android:focusable="true"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:src="@mipmap/baseline_thumb_down_alt_black_18"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="1.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/endTime"
                    app:layout_constraintVertical_bias="0.56"
                    android:layout_marginRight="16dp"
                    android:layout_marginLeft="8dp" />
                <TextView
                    android:id="@+id/songtime"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:fontFamily="sans-serif"
                    android:text="1:05"

                    android:textSize="12sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.0" />

                <TextView
                    android:id="@+id/endTime"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:fontFamily="sans-serif"
                    android:text="3:06"

                    android:textSize="12sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="1.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.0" />

                <LinearLayout
                    android:id="@+id/linearLayout5"
                    android:layout_width="55dp"
                    android:layout_height="55dp"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:gravity="center"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.501"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="1.0">

                    <ImageButton
                        android:id="@+id/play_button_main"
                        android:layout_width="55dp"
                        android:layout_height="55dp"
                        android:scaleType="centerCrop"
                        android:clickable="true"
                        android:focusable="true"
                        android:background="?attr/selectableItemBackgroundBorderless"
                        android:src="@drawable/play_button" />
                    <ImageButton
                        android:id="@+id/pause_button_main"
                        android:layout_width="55dp"
                        android:visibility="gone"
                        android:scaleType="centerCrop"
                        android:layout_height="55dp"
                        android:clickable="true"
                        android:focusable="true"
                        android:background="?attr/selectableItemBackgroundBorderless"
                        android:src="@drawable/pause_button" />
                </LinearLayout>





                <ImageButton
                    android:layout_width="55dp"
                    android:id="@+id/pervious"
                    android:layout_height="55dp"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:padding="15dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:scaleType="fitCenter"
                    android:src="@drawable/backword_button"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/linearLayout5"
                    app:layout_constraintHorizontal_bias="0.754"
                    app:layout_constraintStart_toEndOf="@+id/imageButton2"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="1.0" />

                <ImageButton
                    android:id="@+id/next"
                    android:layout_width="55dp"
                    android:layout_height="55dp"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:padding="15dp"
                    android:scaleType="fitCenter"
                    android:src="@drawable/forword_button"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/button"
                    app:layout_constraintHorizontal_bias="0.25"
                    app:layout_constraintStart_toEndOf="@+id/linearLayout5"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="1.0"
                    />
            </android.support.constraint.ConstraintLayout>
        </RelativeLayout>

        <SeekBar
            android:id="@+id/seekBar3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/relativeLayout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/relativeLayout"
            app:layout_constraintVertical_bias="0.75" />
    </android.support.constraint.ConstraintLayout>
</RelativeLayout>



package music.player.musicplayer;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class MainActivity extends AppCompatActivity {
    private ArrayList<Song> songList;
    private ListView songView;
    LinearLayout nextactivity;
    SeekBar seekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        songView = findViewById(R.id.song_list);
        songList = new ArrayList<Song>();
        seekBar=findViewById(R.id.seekBar2);
        getSongList();

        Collections.sort(songList, new Comparator<Song>(){
            public int compare(Song a, Song b){
                return a.getTitle().compareTo(b.getTitle());
            }
        });
        SongAdapter songAdt = new SongAdapter(this, songList);

        songView.setAdapter(songAdt);

        nextactivity=findViewById(R.id.mediacontroller);


        nextactivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);

                startActivity(intent);
            }
        });

        songView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



            }
        });


    }







//song info method retrive
    public void getSongList() {
        //retrieve song info
        ContentResolver musicResolver = getContentResolver();
        Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
        if (musicCursor != null && musicCursor.moveToFirst()) {
            //get columns
            int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
            int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
            int dataColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);


            //add songs to list
            do {
                long thisId = musicCursor.getLong(idColumn);
                String thisTitle = musicCursor.getString(titleColumn);
                String thisArtist = musicCursor.getString(artistColumn);
                String thisPath = musicCursor.getString(dataColumn);

                songList.add(new Song(thisId, thisTitle, thisArtist, thisPath));
            }
            while (musicCursor.moveToNext());
        }
    }
}

package music.player.musicplayer;

public class Song {
    private long id;
    private String title;
    private String artist;
    private String getpath;

    public long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getArtist() {
        return artist;
    }

    public String getGetpath() {
        return getpath;
    }



    public Song(long id, String title, String artist, String getpath ) {
        this.id = id;
        this.title = title;
        this.artist = artist;
        this.getpath = getpath;

    }
}

package music.player.musicplayer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

class SongAdapter extends BaseAdapter{
    private ArrayList<Song> songs;
    private LayoutInflater songInf;

    public SongAdapter(Context c, ArrayList<Song> theSongs){
        songs=theSongs;
        songInf=LayoutInflater.from(c);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return songs.size();
    }


    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       RelativeLayout songLay = (RelativeLayout)songInf.inflate(R.layout.song, parent, false);
        //get title and artist views
        TextView songView = (TextView)songLay.findViewById(R.id.song_title);
        TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);
        //get song using position
        Song currSong = songs.get(position);
        //get title and artist strings
        songView.setText(currSong.getTitle());
        artistView.setText(currSong.getArtist());
        //set position as tag
        songLay.setTag(position);
        return songLay;
    }

    }

这是我的活动图片,您可以了解我实际上想要的东西

activity 1:

活动二:activity 2:

0 个答案:

没有答案