Java 如何在Android中为mp3文件实现弹出式媒体播放器?

Java 如何在Android中为mp3文件实现弹出式媒体播放器?,java,android,xml,eclipse,android-mediaplayer,Java,Android,Xml,Eclipse,Android Mediaplayer,我制作了一个应用程序,在res\raw文件夹中有20个左右的mp3文件,当按下按钮时,媒体播放,然后再次按下按钮,媒体停止 这工作得很好,但它看起来不是很好,理想情况下,我想要的是一个小的弹出式播放器,出现在屏幕中央,带有搜索栏、暂停按钮和播放歌曲的名称。(很像Google Play音乐应用程序在从文件浏览器打开mp3文件时工作)。我知道我可以点击按钮打开一个使用Google Play Music的意图,但我希望我的应用程序拥有自己的颜色方案,外观非常相似的同一弹出窗口版本 我为其中一首歌曲编写

我制作了一个应用程序,在res\raw文件夹中有20个左右的mp3文件,当按下按钮时,媒体播放,然后再次按下按钮,媒体停止

这工作得很好,但它看起来不是很好,理想情况下,我想要的是一个小的弹出式播放器,出现在屏幕中央,带有搜索栏、暂停按钮和播放歌曲的名称。(很像Google Play音乐应用程序在从文件浏览器打开mp3文件时工作)。我知道我可以点击按钮打开一个使用Google Play Music的意图,但我希望我的应用程序拥有自己的颜色方案,外观非常相似的同一弹出窗口版本

我为其中一首歌曲编写的当前Java代码如下:

package com.lmarshall1995.scoutsongs;

import com.lmarshall1995.scoutsongs.R;
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class Song_AliceTheCamel_Activity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.song_alicethecamel);
        setupNavigationButton();

        Toast toast = Toast.makeText(this, "To be sung in the tune of \n . . .", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();

        final Button back = (Button) findViewById(R.id.back_button);
        back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });

        final Button previous = (Button) findViewById(R.id.previous_button);
        previous.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent previousIntent = new Intent(Song_AliceTheCamel_Activity.this, Song_ZipADeeDooDah_Activity.class);
                Song_AliceTheCamel_Activity.this.startActivity(previousIntent);
                finish();
            }
        });

        final Button next = (Button) findViewById(R.id.next_button);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent nextIntent = new Intent(Song_AliceTheCamel_Activity.this, Song_Bingo_Activity.class);
                Song_AliceTheCamel_Activity.this.startActivity(nextIntent);
                finish();
            }
        });

        final ImageButton play_tune = (ImageButton) findViewById(R.id.play_tune);
        play_tune.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ImageButton play_tune = (ImageButton) findViewById(R.id.play_tune);
                play_tune.setOnClickListener(new View.OnClickListener() {
                    MediaPlayer mp = MediaPlayer.create(Song_AliceTheCamel_Activity.this, R.raw.tune_alicethecamel);
                    public void onClick(View arg0) {
                        if (mp.isPlaying()) {
                            mp.stop();
                            mp.prepareAsync();
                            mp.seekTo(0);
                        } else {
                            mp.start();
                        }
                    }
                });
            }
        });

    }
    private void setupNavigationButton() {}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/song_alicethecamel"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ScoutSongs" >

    <TextView
        android:id="@+id/Song_AliceTheCamel_Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="@string/Song_AliceTheCamel_Title"
        android:textSize="20sp"
        android:textColor="#FFFFFF" />

    <ImageButton
        android:id="@+id/play_tune"
        android:src="@drawable/play_tune"
        android:contentDescription="@string/play_tune"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/Song_AliceTheCamel_Title"
        android:layout_alignTop="@+id/Song_AliceTheCamel_Title" />

    <ScrollView 
        android:id="@+id/Song_AliceTheCamel_Lyrics"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/back_button"
        android:layout_alignLeft="@+id/Song_AliceTheCamel_Title"
        android:layout_alignRight="@+id/play_tune"
        android:layout_below="@+id/play_tune"> 

        <TextView 
            android:id="@+id/Song_A_Lyrics1" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:text="@string/Song_AliceTheCamel_Lyrics"
            android:textColor="#FFFFFF" /> 

    </ScrollView>

    <Button
        android:id="@+id/back_button"
        android:layout_width="65dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/Song_AliceTheCamel_Lyrics"
        android:text="@string/back"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/previous_button"
        android:layout_width="95dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/back_button"
        android:layout_alignBottom="@+id/back_button"
        android:layout_alignLeft="@+id/Song_AliceTheCamel_Lyrics"
        android:text="@string/previous"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/next_button"
        android:layout_width="95dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/previous_button"
        android:layout_alignBottom="@+id/previous_button"
        android:layout_toRightOf="@+id/previous_button"
        android:text="@string/next"
        android:textColor="#FFFFFF" />

</RelativeLayout>
其中一首歌的当前xml代码如下:

package com.lmarshall1995.scoutsongs;

import com.lmarshall1995.scoutsongs.R;
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class Song_AliceTheCamel_Activity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.song_alicethecamel);
        setupNavigationButton();

        Toast toast = Toast.makeText(this, "To be sung in the tune of \n . . .", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();

        final Button back = (Button) findViewById(R.id.back_button);
        back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });

        final Button previous = (Button) findViewById(R.id.previous_button);
        previous.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent previousIntent = new Intent(Song_AliceTheCamel_Activity.this, Song_ZipADeeDooDah_Activity.class);
                Song_AliceTheCamel_Activity.this.startActivity(previousIntent);
                finish();
            }
        });

        final Button next = (Button) findViewById(R.id.next_button);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent nextIntent = new Intent(Song_AliceTheCamel_Activity.this, Song_Bingo_Activity.class);
                Song_AliceTheCamel_Activity.this.startActivity(nextIntent);
                finish();
            }
        });

        final ImageButton play_tune = (ImageButton) findViewById(R.id.play_tune);
        play_tune.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ImageButton play_tune = (ImageButton) findViewById(R.id.play_tune);
                play_tune.setOnClickListener(new View.OnClickListener() {
                    MediaPlayer mp = MediaPlayer.create(Song_AliceTheCamel_Activity.this, R.raw.tune_alicethecamel);
                    public void onClick(View arg0) {
                        if (mp.isPlaying()) {
                            mp.stop();
                            mp.prepareAsync();
                            mp.seekTo(0);
                        } else {
                            mp.start();
                        }
                    }
                });
            }
        });

    }
    private void setupNavigationButton() {}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/song_alicethecamel"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ScoutSongs" >

    <TextView
        android:id="@+id/Song_AliceTheCamel_Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="@string/Song_AliceTheCamel_Title"
        android:textSize="20sp"
        android:textColor="#FFFFFF" />

    <ImageButton
        android:id="@+id/play_tune"
        android:src="@drawable/play_tune"
        android:contentDescription="@string/play_tune"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/Song_AliceTheCamel_Title"
        android:layout_alignTop="@+id/Song_AliceTheCamel_Title" />

    <ScrollView 
        android:id="@+id/Song_AliceTheCamel_Lyrics"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/back_button"
        android:layout_alignLeft="@+id/Song_AliceTheCamel_Title"
        android:layout_alignRight="@+id/play_tune"
        android:layout_below="@+id/play_tune"> 

        <TextView 
            android:id="@+id/Song_A_Lyrics1" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:text="@string/Song_AliceTheCamel_Lyrics"
            android:textColor="#FFFFFF" /> 

    </ScrollView>

    <Button
        android:id="@+id/back_button"
        android:layout_width="65dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/Song_AliceTheCamel_Lyrics"
        android:text="@string/back"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/previous_button"
        android:layout_width="95dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/back_button"
        android:layout_alignBottom="@+id/back_button"
        android:layout_alignLeft="@+id/Song_AliceTheCamel_Lyrics"
        android:text="@string/previous"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/next_button"
        android:layout_width="95dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/previous_button"
        android:layout_alignBottom="@+id/previous_button"
        android:layout_toRightOf="@+id/previous_button"
        android:text="@string/next"
        android:textColor="#FFFFFF" />

</RelativeLayout>

但我更希望它看起来像这样:

任何帮助都将不胜感激,因为我知道这方面的源代码显然将受到保护,目前,我所知道的是我需要为布局创建一个xml文件,我认为这将是寻求指导的一个良好开端


一旦我有了这些信息,我想将背景改为黑色而不是白色,搜索栏改为紫色而不是橙色,并将显示的文本改为歌曲标题,而不是文件名。

将音频播放器实现为一个片段,该片段通过消息传递工具(如LocalBroadcastReceiver或Otto)与后台服务进行通信。然后在应用程序中,当用户想要播放一首歌曲时,他们可以选择它,您可以使用您创建的新片段创建一个DialogFragment。我以前就是这样做的。

谢谢@donn felker。如何将音频播放器实现为片段?最好先了解片段。这是上面的医生:你这个漂亮的人@donn felker。这正是我一直在寻找的。我只是不知道它叫什么。几周来我一直确信这是一个额外的意图,但这只是一个片段。谢谢。:-)