Android:通知栏中的MediaPlayer

时间:2015-04-01 18:29:03

标签: java android

我已经构建了一个音频播放器,并且只添加了通知按钮。 我有课程:

MusicPlayer(活动) - 启动应用程序时播放音乐的主要活动。 musichelper(class) - 包含所有功能,如下一首歌,MediaPlayer本身,歌曲列表等。 AudioVisualNotification(类),它自己构建通知。 noteAct.java(Activity) - 当按下通知中的按钮时调用它。

我的问题是:我不知道如何在noteAct活动中引用在musichelper类中创建的MediaPlayer而不调用/引用它(这实际上意味着再次构建音乐列表并重新定义MusicPlayer使用的所有变量在同一时间。

我确信这很简单,但我是这个场景的新手。 基本上我想使用musichelper类/函数而无需在noteAct中重新创建它。 这是musichelper的一部分(删除了不相关的部分)

package com.auv.als.audiovisual;

import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by leiti_000 on 3/28/2015.
 */
public class musichelper {
    Context context;
    List<HashMap<String, String>> msx;
    boolean playerCreated = false;
    MediaPlayer mPlayer;
    int songIndex;
    ArrayList<Integer> played = new ArrayList<Integer>();
    boolean shuffle;
    boolean loop;
    boolean isPaused;

    public musichelper(Context ctx){
        this.context = ctx;
    }

    public List<HashMap<String, String>> buildMusic() {
        //Builds List<HashMap<String, String>> and returns it.
        //msx = this list;
        //return songList;
    }

    public void getSong(String ops){
        if(shuffle == true){
            if(ops.equals("prev")){
                int prevSong = played.indexOf(songIndex);
                int pre = prevSong - 1;
                int songtoGet = played.get(pre);
                songIndex = songtoGet;
            }
            else {
                Random r = new Random();
                int rnd = r.nextInt(msx.size());
                if (played.size() == msx.size()) {
                    played.clear();
                } else {
                    while (played.contains(rnd)) {
                        rnd = r.nextInt(msx.size());
                        Log.d("Report", "Repetition occured");
                    }
                }
                played.add(rnd);
                songIndex = rnd;
            }
            }
        else {
            if (ops.equals("prev")) {
                if (songIndex == 0) {
                    songIndex = msx.size() - 1;
                } else {
                    songIndex = songIndex - 1;
                }
            } else if (ops.equals("next")) {
                if (songIndex == msx.size() - 1) {
                    songIndex = 0;
                } else {
                    songIndex = songIndex + 1;
                }
            }
        }
    }


    public MediaPlayer createPlayer(){
            mPlayer = new MediaPlayer();
                mPlayer.setDataSource(context, Uri.parse(msx.get(songIndex).get("data")));
                mPlayer.prepare();
                mPlayer.start();
                playerCreated = true;
        return result;
    }

    //Main function for playing song.
    public String playSong() {
            mPlayer.reset();
            try {
                mPlayer.setDataSource(context, Uri.parse(msx.get(songIndex).get("data")));
                mPlayer.prepare();
            } catch (Exception e) {
                e.printStackTrace();
            }
        mPlayer.start();
        String songTitle = msx.get(songIndex).get("name");
        //addLineRenderer();
        return songTitle;
    }

    public MediaPlayer getmPlayer(){
        return this.mPlayer;
    }

    public void playerToggle(boolean state){
        if(state == false) {
            mPlayer.pause();
            isPaused = true;
        }
        else if(state == true){
            mPlayer.start();
            isPaused = false;
        }
        System.out.println(isPaused);
    }
}

这是AudioVisualNotification

package com.auv.als.audiovisual;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

/**
 * Created by leiti_000 on 4/1/2015.
 */
@SuppressWarnings("deprecation")
public class AudioVisualNotification{
    Context context;
    private NotificationManager nManager;
    private NotificationCompat.Builder nBuilder;
    private RemoteViews remoteView;
    private musichelper msc;

    public AudioVisualNotification(Context ctx){
    this.context = ctx;
    nBuilder = new NotificationCompat.Builder(context)
    .setContentTitle("Visuality")
    .setSmallIcon(R.drawable.logo)
    .setOngoing(true);
    remoteView = new RemoteViews(context.getPackageName(), R.layout.notificationview);

    setListeners(remoteView);
    nBuilder.setContent(remoteView);
        nManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(3, nBuilder.build());

    }
    public void setHelper(musichelper msc){
        this.msc = msc;
    }


    public void setListeners(RemoteViews view){
    Intent previous = new Intent(context, noteAct.class);
    previous.putExtra("action", "prev");
    PendingIntent prevBtn = PendingIntent.getActivity(context, 0, previous, 0);
    view.setOnClickPendingIntent(R.id.backBtn, prevBtn);


    }
    public void notificationCancel(){
        nManager.cancel(3);
    }

}

和noteAct.java

package com.auv.als.audiovisual;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;

/**
 * Created by leiti_000 on 4/1/2015.
 */
public class noteAct extends Activity {
    MediaPlayer mPlayer;
    musichelper msc;



    public noteAct(musichelper msc){
        this.msc = msc;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        String action = (String)getIntent().getExtras().get("action");
        if(action.equals("prev")){
        msc.getSong("prev");
        msc.playSong();

        }



    }


}

正如你所看到的,我尝试了一些东西,但我不明白如何调用noteAct并解析MediaPlayer以便重复使用。

0 个答案:

没有答案