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

标签 java android xml eclipse android-mediaplayer

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

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

我目前拥有的其中一首歌曲的 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() {}
}

其中一首歌曲的当前 xml 代码如下:

<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>

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

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

获得此信息后,我想将背景更改为黑色而不是白色,将搜索栏更改为紫色而不是橙色,并将显示的文本更改为歌曲标题,而不是文件名。

最佳答案

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

关于java - 如何在 Android 中为 mp3 文件实现弹出式媒体播放器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21867386/

相关文章:

java - 内存中的图像消耗的内存远多于其文件大小

c# - 可以使用 XmlAttributes 有选择地 XmlIgnore 吗?

python - 如何从单个流中解析多个 XML 文档?

c# - 如何比较两个xml节点?

java - 如何开始从流中的特定位置通过输入流读取字节?

java - 如何舍入浮点精度,考虑它的大小?

java - 使用 BorderLayout 绘制图形并将其放置在面板内

java - NoSaveStateFramelayout 的问题

java - 删除自定义对象的 ArrayList 中的重复项

android - 在样式声明中指定时不应用 ListView 分隔符