如何在媒体播放器中添加通知代码?

时间:2014-09-15 09:06:48

标签: android android-mediaplayer

当我在后台播放歌曲时,我想在我的媒体播放器上收到通知 对于E.g播放音乐,音乐播放器

公共类MainActivity扩展Activity实现OnPreparedListener,         MediaController.MediaPlayerControl {

private static final String TAG = "AudioPlayer";
ListView mListView;
private MediaPlayer mediaPlayer;
private MediaController mediaController;
ArrayList<Song> songList;
private Handler handler = new Handler();
public static int position = 0;


Intent songIntent = new Intent (android.content.Intent.ACTION_SEND);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnPreparedListener(this);
    mediaController = new MediaController(this);
    mediaController.setPrevNextListeners(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // for next item

            if (position != songList.size() - 1) {
                position++;
            } else {
                position = 0;
            }

            playSong(songList.get(position).getPath());

        }
    }, new View.OnClickListener() {
        public void onClick(View v) {

            // for previous item

            if (position != 0) {
                position--;
            } else {
                position = songList.size() - 1;
            }

            playSong(songList.get(position).getPath());
        }
    });

    songList = new ArrayList<Song>();

    scanDirectory(new File(Environment.getExternalStorageDirectory()
            .getPath()));

    mListView = (ListView) findViewById(R.id.listView1);
    mListView.setAdapter(new ArrayAdapter<Song>(this,
            android.R.layout.simple_list_item_1, songList));
    mListView.setOnItemClickListener(new OnItemClickListener() {

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

            MainActivity.position = position;


            Song song = (Song) parent.getItemAtPosition(position);

            playSong(song.getPath());
        }
    });
}

public void playSong(String path) {

    mediaPlayer.reset();

    try {
        mediaPlayer.setDataSource(path);
        mediaPlayer.prepare();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mediaPlayer.start();
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    mediaController.show();
    return false;
}



public void start() {
    mediaPlayer.start();
}

public void pause() {
    mediaPlayer.pause();
}

public int getDuration() {
    return mediaPlayer.getDuration();
}

public int getCurrentPosition() {
    return mediaPlayer.getCurrentPosition();
}

public void seekTo(int i) {
    mediaPlayer.seekTo(i);
}

public boolean isPlaying() {
    return mediaPlayer.isPlaying();
}

public int getBufferPercentage() {
    return 0;
}

public boolean canPause() {
    return true;
}

public boolean canSeekBackward() {
    return true;
}

public boolean canSeekForward() {
    return true;
}

public void onPrepared(MediaPlayer mediaPlayer) {
    Log.d(TAG, "onPrepared");
    mediaController.setMediaPlayer(this);
    mediaController.setAnchorView(findViewById(R.id.main_audio_view));

    handler.post(new Runnable() {
        public void run() {
            mediaController.setEnabled(true);
            mediaController.show();
        }


    });
}

@Override
protected void onStop() {
    super.onStop();
    mediaController.hide(); 
mediaPlayer.stop(); 
mediaPlayer.release();
}

public void scanDirectory(File path) {

    File[] listFiles = path.listFiles();
    if (listFiles != null && listFiles.length > 0) {
        for (File file : listFiles) {
            if (file.isDirectory()) {
                scanDirectory(file);
            } else {
                addSongToList(file);
            }
        }
    }
}

public void addSongToList(File file) {

    if (file.getName().endsWith(".mp3"))
        songList.add(new Song(file.getName(), file.getPath()));
}

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

}

请给我一些关于它的想法

先谢谢

1 个答案:

答案 0 :(得分:0)

您可以创建类似的通知:

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence title= "Playing";
CharSequence message = "My song";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);

Notification notif = new Notification(R.drawable.disco, "Playing", System.currentTimeMillis());
notif.setLatestEventInfo(context, title, message, contentIntent);
nm.notify(1, notif);

激活/停用有关Android活动生命周期HERE的通知。另请阅读通知HERE的教程。