【问题标题】:How to add Notification code in media player?如何在媒体播放器中添加通知代码?
【发布时间】:2014-09-15 09:06:48
【问题描述】:

当我在后台播放歌曲时,我想在媒体播放器上收到通知 例如播放音乐,音乐播放器

公共类 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;
}

}

请给我一些想法

提前致谢

【问题讨论】:

  • 请把问题说清楚。
  • 当您播放列表中的歌曲时,只需将歌曲的名称或 ID 传递给通知即可... ;)
  • 当歌曲在后台播放时,它应该显示正在播放的歌曲以及正在播放或暂停的歌曲
  • 发布你尝试过的代码

标签: android android-mediaplayer


【解决方案1】:

您创建这样的通知:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多